From 68c0306dd8dcbed453155a8286a436f9582efc36 Mon Sep 17 00:00:00 2001 From: spalax Date: Sun, 10 Mar 2013 14:55:34 +0100 Subject: [PATCH] add support for titleprefixword command --- songbook-makeindex.py | 12 ++++++++++-- songbook.py | 22 +++++++++++++++++++--- sortindex.py | 1 + templates/minimal.tmpl | 5 ++++- templates/patacrep.tmpl | 5 ++++- 5 files changed, 38 insertions(+), 7 deletions(-) diff --git a/songbook-makeindex.py b/songbook-makeindex.py index e11401d0..494b9889 100755 --- a/songbook-makeindex.py +++ b/songbook-makeindex.py @@ -18,7 +18,7 @@ import sortindex import locale # Pattern set to ignore latex command in title prefix -keywordPattern = re.compile(r"^%(\w+)\s?(\w*)") +keywordPattern = re.compile(r"^%(\w+)\s?(.*)$") firstLetterPattern = re.compile(r"^(?:\{?\\\w+\}?)*[^\w]*(\w)") class index: @@ -37,9 +37,17 @@ class index: self.keywords[key].append(word) def compileKeywords(self): - pass + self.prefix_patterns = [] + if 'prefix' in self.keywords: + for prefix in self.keywords['prefix']: + self.prefix_patterns.append(re.compile(r"^(%s)\b\s*(.*)$" % prefix)) def add(self, key, number, link): + for pattern in self.prefix_patterns: + match = pattern.match(key) + if match: + key = "%s (%s)" % (match.group(2), match.group(1)) + break # Only one match per key (first, key) = self.filter(key) if not self.data.has_key(first): self.data[first] = dict() diff --git a/songbook.py b/songbook.py index 41b4b3df..6d3ec838 100755 --- a/songbook.py +++ b/songbook.py @@ -50,7 +50,16 @@ def makeCoverCache(library): def matchRegexp(reg, iterable): return [ m.group(1) for m in (reg.match(l) for l in iterable) if m ] -def songslist(library, songs): +def unprefixed(title, prefixes): + """Remove the first prefix of the list in the beginning of title (if any). + """ + for prefix in prefixes: + match = re.compile(r"^(%s)\b\s*(.*)$" % prefix).match(title) + if match: + return match.group(2) + return title + +def songslist(library, songs, prefixes): song_objects = [] for s in songs: path = library + 'songs/' + s @@ -65,7 +74,7 @@ def songslist(library, songs): album = '' song_objects.append(Song(title, artist, album, path)) - song_objects = sorted(song_objects, key=lambda x: locale.strxfrm(x.title)) + song_objects = sorted(song_objects, key=lambda x: locale.strxfrm(unprefixed(x.title, prefixes))) song_objects = sorted(song_objects, key=lambda x: locale.strxfrm(x.album)) song_objects = sorted(song_objects, key=lambda x: locale.strxfrm(x.artist)) @@ -122,6 +131,8 @@ def makeTexFile(sb, library, output): # default value template = "patacrep.tmpl" songs = [] + titleprefixwords = "" + prefixes = [] # parse the songbook data if "template" in sb: @@ -130,6 +141,11 @@ def makeTexFile(sb, library, output): if "songs" in sb: songs = sb["songs"] del sb["songs"] + if "titleprefixwords" in sb: + prefixes = sb["titleprefixwords"] + for prefix in sb["titleprefixwords"]: + titleprefixwords += "\\titleprefixword{%s}\n" % prefix + sb["titleprefixwords"] = titleprefixwords parameters = parseTemplate("templates/"+template) @@ -152,7 +168,7 @@ def makeTexFile(sb, library, output): songs = map(lambda x: x[len(library) + 6:], recursiveFind(os.path.join(library, 'songs'), '*.sg')) if len(songs) > 0: - out.write(formatDefinition('songslist', songslist(library, songs))) + out.write(formatDefinition('songslist', songslist(library, songs, prefixes))) out.write('\\makeatother\n') # output template diff --git a/sortindex.py b/sortindex.py index dd3d5d50..4333e5f2 100644 --- a/sortindex.py +++ b/sortindex.py @@ -28,6 +28,7 @@ replacePattern = { '~n' : 'ñ', "c C" : 'Ç', "c c" : 'ç', + "textquoteright" : "'", } def sortkey(value): diff --git a/templates/minimal.tmpl b/templates/minimal.tmpl index f3f7f688..175c2da4 100644 --- a/templates/minimal.tmpl +++ b/templates/minimal.tmpl @@ -29,7 +29,8 @@ %%: {"name":"lang", "description":"Language", "default":"english"}, %%: {"name":"instruments", "description":"Instruments", "type":"flag", "values":["guitar","ukulele"], "join":",", "mandatory":true, "default":["guitar"]}, %%: {"name":"bookoptions", "description":"Options", "type":"flag", "values":["diagram","importantdiagramonly","lilypond","pictures","tabs","repeatchords","onesongperpage"], "join":",", "mandatory":true, "default":["pictures"]}, -%%: {"name":"mainfontsize", "description":"Font Size", "type":"font", "default":"10"} +%%: {"name":"mainfontsize", "description":"Font Size", "type":"font", "default":"10"}, +%%: {"name":"titleprefixwords", "description":"Ignore some words in the beginning of song titles"} %%:] %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % begin document @@ -49,6 +50,8 @@ \fi% } +\gettitleprefixwords + \nosongnumbers \pagestyle{empty} diff --git a/templates/patacrep.tmpl b/templates/patacrep.tmpl index cd149748..89e1e919 100644 --- a/templates/patacrep.tmpl +++ b/templates/patacrep.tmpl @@ -42,7 +42,8 @@ %%: {"name":"mainfontsize", "description":"Font Size", "type":"font", "default":"10"}, %%: {"name":"songnumberbgcolor", "description":"Number Shade", "type":"color", "default":"#D1E4AE"}, %%: {"name":"notebgcolor", "description":"Note Shade", "type":"color", "default":"#D1E4AE"}, -%%: {"name":"indexbgcolor", "description":"Index Shade", "type":"color", "default":"#D1E4AE"} +%%: {"name":"indexbgcolor", "description":"Index Shade", "type":"color", "default":"#D1E4AE"}, +%%: {"name":"titleprefixwords", "description":"Ignore some words in the beginning of song titles"} %%:] %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % begin document @@ -84,6 +85,8 @@ \renewcommand{\notebgcolor}{NoteBgColor} \renewcommand{\idxbgcolor}{IndexBgColor} +\gettitleprefixwords + \pagestyle{empty} % Customization of the page appearance