diff abcfield.py @ 726:833e6185b6a2 build-default-282

Add fulltitle to template fields. fulltitle is title + [(" subtitle ")"] if subtitle is present. To do: extend full title to next and prev. this means exposing it in abcfield.py.
author Jim Hague <jim.hague@acm.org>
date Wed, 11 Oct 2017 17:09:31 +0100
parents f9a554e858f3
children 772402f5f8ea
line wrap: on
line diff
--- a/abcfield.py	Wed Oct 11 17:03:29 2017 +0100
+++ b/abcfield.py	Wed Oct 11 17:09:31 2017 +0100
@@ -181,23 +181,18 @@
 def expandCustomMarkdown(t, dir, latex):
     # Given a match to (foo.abc), return a markdown link to the tune with the
     # title (and subtitle, if present) of the tune as the text of the link.
-    def getTitle(m):
+    def getTitleLink(m):
         fname = m.group(1) + ".abc"
         path = pathlib.Path(dir, fname)
         with path.open() as f:
-            title = getFieldDisplayText(f, dir, "T", latex=latex)
-            f.seek(0)
-            subtitle = getFieldDisplayText(f, dir, "T", n=2, latex=latex)
-            if len(subtitle) > 0:
-                title = title + " (" + subtitle + ")"
-            return "[" + title + "](" + fname + ")"
-    return re.sub(r'<(.*?).abc>', getTitle, t)
+            return "[" + getFullTitle(f, dir, latex=latex) + "](" + fname + ")"
+    return re.sub(r'<(.*?).abc>', getTitleLink, t)
 
 # Return the raw text for a given field. Optionally the nth field is taken,
 # or the field data must start with a designated string to be recognised.
-def getFieldText(inf, field, n = 1, starts = None):
+def getFieldText(lines, field, n = 1, starts = None):
     res = ""
-    for line in inf:
+    for line in lines:
         line = line.strip()
         if len(line) > 2 and line[1] == ':':
             if line[0] == "+" or (line[0] == field and line[2] == "+"):
@@ -224,8 +219,8 @@
     return res
 
 # Return display text for a given field.
-def getFieldDisplayText(inf, dir, field, n = 1, starts = None, latex = False):
-    res = getFieldText(inf, field, n, starts)
+def getFieldDisplayText(lines, dir, field, n = 1, starts = None, latex = False):
+    res = getFieldText(lines, field, n, starts)
     if res:
         res = convertAccents(res, latex)
         if field.upper() == "T":
@@ -236,12 +231,19 @@
             res = convertMarkdown(expandCustomMarkdown(res, dir, latex), latex)
     return res
 
+# Return full title (title + [" (" + subtitle + ")"] if subtitle exists).
+def getFullTitle(lines, dir, starts = None, latex = False):
+    title = getFieldDisplayText(lines, dir, "T", starts=starts, latex=latex)
+    subtitle = getFieldDisplayText(lines, dir, "T", n=2, starts=starts, latex=latex)
+    return title if len(subtitle) == 0 else title + " (" + subtitle + ")"
+
 if __name__ == "__main__":
-    def process(inf, dir, options):
+    def process(f, dir, options):
+        lines = f.readlines()
         if options.display:
-            line = getFieldDisplayText(inf, dir, options.field, options.index, options.starts, options.latex)
+            line = getFieldDisplayText(lines, dir, options.field, options.index, options.starts, options.latex)
         else:
-            line = getFieldText(inf, options.field, options.index, options.starts)
+            line = getFieldText(lines, options.field, options.index, options.starts)
         if line:
             print(line)
             return True