|
|
@ -8,17 +8,18 @@ from a file generated by the latex compilation of the songbook (.sxd). |
|
|
|
""" |
|
|
|
|
|
|
|
import locale |
|
|
|
import unidecode |
|
|
|
import re |
|
|
|
|
|
|
|
from patacrep import authors |
|
|
|
from patacrep import encoding |
|
|
|
from patacrep.latex import latex2unicode |
|
|
|
|
|
|
|
EOL = u"\n" |
|
|
|
EOL = "\n" |
|
|
|
|
|
|
|
# Pattern set to ignore latex command in title prefix |
|
|
|
KEYWORD_PATTERN = re.compile(ur"^%(\w+)\s?(.*)$", re.LOCALE) |
|
|
|
FIRST_LETTER_PATTERN = re.compile(ur"^(?:\{?\\\w+\}?)*[^\w]*(\w)", re.LOCALE) |
|
|
|
KEYWORD_PATTERN = re.compile(r"^%(\w+)\s?(.*)$", re.LOCALE) |
|
|
|
FIRST_LETTER_PATTERN = re.compile(r"^(?:\{?\\\w+\}?)*[^\w]*(\w)", re.LOCALE) |
|
|
|
|
|
|
|
|
|
|
|
def process_sxd(filename): |
|
|
@ -77,13 +78,13 @@ class Index(object): |
|
|
|
except AttributeError: |
|
|
|
# classify as number all the non letter characters |
|
|
|
letter = "0" |
|
|
|
if re.match(ur'\d', letter): |
|
|
|
if re.match(r'\d', letter): |
|
|
|
letter = '0-9' |
|
|
|
return letter.upper() |
|
|
|
|
|
|
|
def add_keyword(self, key, word): |
|
|
|
"""Add 'word' to self.keywords[key].""" |
|
|
|
if not key in self.keywords.keys(): |
|
|
|
if not key in self.keywords: |
|
|
|
self.keywords[key] = [] |
|
|
|
self.keywords[key].append(word) |
|
|
|
|
|
|
@ -93,7 +94,7 @@ class Index(object): |
|
|
|
if 'prefix' in self.keywords: |
|
|
|
for prefix in self.keywords['prefix']: |
|
|
|
self.prefix_patterns.append(re.compile( |
|
|
|
ur"^({prefix})(\b|\\)(\s*.*)$".format(prefix=prefix), |
|
|
|
r"^({prefix})(\b|\\)(\s*.*)$".format(prefix=prefix), |
|
|
|
re.LOCALE |
|
|
|
)) |
|
|
|
|
|
|
@ -107,12 +108,12 @@ class Index(object): |
|
|
|
similar method with processing. |
|
|
|
""" |
|
|
|
first = self.get_first_letter(key[0]) |
|
|
|
if not first in self.data.keys(): |
|
|
|
if not first in self.data: |
|
|
|
self.data[first] = dict() |
|
|
|
if not key in self.data[first].keys(): |
|
|
|
if not key in self.data[first]: |
|
|
|
self.data[first][key] = { |
|
|
|
'sortingkey': [ |
|
|
|
encoding.unidecode(latex2unicode(item)).lower() |
|
|
|
unidecode.unidecode(latex2unicode(item)).lower() |
|
|
|
for item in key |
|
|
|
], |
|
|
|
'entries': [], |
|
|
@ -150,26 +151,26 @@ class Index(object): |
|
|
|
@staticmethod |
|
|
|
def ref_to_str(ref): |
|
|
|
"""Return the LaTeX code corresponding to the reference.""" |
|
|
|
return ur'\hyperlink{{{0[link]}}}{{{0[num]}}}'.format(ref) |
|
|
|
return r'\hyperlink{{{0[link]}}}{{{0[num]}}}'.format(ref) |
|
|
|
|
|
|
|
def key_to_str(self, key): |
|
|
|
"""Convert the key (title or author) to the LaTeX command rendering it. |
|
|
|
|
|
|
|
""" |
|
|
|
if self.indextype == "AUTHOR": |
|
|
|
return ur"\indexauthor{{{first}}}{{{last}}}".format( |
|
|
|
return r"\indexauthor{{{first}}}{{{last}}}".format( |
|
|
|
first=key[1], |
|
|
|
last=key[0], |
|
|
|
) |
|
|
|
|
|
|
|
if self.indextype == "TITLE": |
|
|
|
return ur"\indextitle{{{0[1]}}}{{{0[0]}}}".format(key) |
|
|
|
return r"\indextitle{{{0[1]}}}{{{0[0]}}}".format(key) |
|
|
|
|
|
|
|
def entry_to_str(self, key, entry): |
|
|
|
"""Return the LaTeX code corresponding to the entry.""" |
|
|
|
return unicode(ur'\idxentry{{{0}}}{{{1}}}' + EOL).format( |
|
|
|
return (r'\idxentry{{{0}}}{{{1}}}' + EOL).format( |
|
|
|
self.key_to_str(key), |
|
|
|
ur'\\'.join([self.ref_to_str(ref) for ref in entry]), |
|
|
|
r'\\'.join([self.ref_to_str(ref) for ref in entry]), |
|
|
|
) |
|
|
|
|
|
|
|
def idxblock_to_str(self, letter, entries): |
|
|
@ -185,10 +186,10 @@ class Index(object): |
|
|
|
for item |
|
|
|
in entries[key]['sortingkey'] |
|
|
|
] |
|
|
|
string = ur'\begin{idxblock}{' + letter + '}' + EOL |
|
|
|
string = r'\begin{idxblock}{' + letter + '}' + EOL |
|
|
|
for key in sorted(entries, key=sortkey): |
|
|
|
string += self.entry_to_str(key, entries[key]['entries']) |
|
|
|
string += ur'\end{idxblock}' + EOL |
|
|
|
string += r'\end{idxblock}' + EOL |
|
|
|
return string |
|
|
|
|
|
|
|
def entries_to_str(self): |
|
|
|