diff --git a/songbook-makeindex.py b/songbook-makeindex.py index e2af3e02..d5252469 100755 --- a/songbook-makeindex.py +++ b/songbook-makeindex.py @@ -14,6 +14,8 @@ import os.path import glob import re from optparse import OptionParser +import title_sort +import locale # Pattern set to ignore latex command in title prefix keywordPattern = re.compile(r"^%(\w+)\s?(\w*)") @@ -59,7 +61,7 @@ class index: def idxBlockToStr(self, letter, entries): str = '\\begin{idxblock}{'+letter+'}'+'\n' - for key in sorted(entries.keys()): + for key in sorted(entries.keys(), key=title_sort.sortkey): str += self.entryToStr(key, entries[key]) str += '\\end{idxblock}'+'\n' return str @@ -98,6 +100,7 @@ def usage(exitCode=None): sys.exit(exitCode) def main(): + locale.setlocale(locale.LC_ALL, '') usage = "usage: %prog [options] FILE" parser = OptionParser(usage) parser.add_option("-o", "--output", dest="filename", diff --git a/songbook.py b/songbook.py index 7e556f4d..3cec5929 100755 --- a/songbook.py +++ b/songbook.py @@ -7,6 +7,8 @@ import os.path import glob import re import json +import locale +import titlesort def matchRegexp(reg, iterable): return [ m.group(1) for m in (reg.match(l) for l in iterable) if m ] @@ -94,7 +96,8 @@ def makeTexFile(sb, output): # output songslist if songs == "all": songs = map(lambda x: x[6:], glob.glob('songs/*/*.sg')) - songs.sort() + songs.sort(key=title_sort.sortkey) + if len(songs) > 0: out.write(formatDefinition('songslist', songslist(songs))) out.write('\\makeatother\n') @@ -150,6 +153,7 @@ def usage(): print "No usage information yet." def main(): + locale.setlocale(locale.LC_ALL, '') # set script locale to match user's try: opts, args = getopt.getopt(sys.argv[1:], "hs:o:d", diff --git a/title_sort.py b/title_sort.py new file mode 100644 index 00000000..af4743e8 --- /dev/null +++ b/title_sort.py @@ -0,0 +1,29 @@ +#coding:utf8 + +import re +import warnings +import locale + +iecPattern = re.compile(r"\IeC {\\(.*?)}") +replacePattern = { + '`A': 'À', + 'oe ': 'œ', + "'e" : 'é', + "'o" : 'ó', + "c C" : 'ç', +} + +def sortkey(value): + ''' + From a title, return something usable for sorting. It handles locale (but + don't forget to call locale.setlocale(locale.LC_ALL, '')). It also try to + handle the sort with crappy latex escape sequences. Some chars may not be + handled by this function, so add them to *replacePattern* dictionnary. + ''' + def repl(match): + try: + return replacePattern[match.group(1)] + except KeyError: + warnings.warn("Error, no match to replace %s in %s. You should add it in the coresponding table in title_solt.py" % (match.group(0), match.group(1))) + + return locale.strxfrm(iecPattern.sub(repl, value))