changeset 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
files abcfield.py abctemplate.py dottes.html.learnertune dottes.html.tune dottes.html.tuneindex
diffstat 5 files changed, 26 insertions(+), 22 deletions(-) [+]
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
--- a/abctemplate.py	Wed Oct 11 17:03:29 2017 +0100
+++ b/abctemplate.py	Wed Oct 11 17:09:31 2017 +0100
@@ -21,6 +21,7 @@
 # * name. The file base name. Base filename without extension.
 # * title. The tune title.
 # * subtitle. The tune subtitle (second Title field), if any.
+# * fulltitle. The tune title followed, if it exists, by " (" subtitle ")"
 # * tradition. The Morris tradition the dance tune is from.
 # * composer. The tune composer.
 # * key. The tune key.
@@ -37,7 +38,7 @@
 import pathlib
 import string
 
-from abcfield import getFieldDisplayText
+from abcfield import getFieldDisplayText, getFullTitle
 
 if __name__ == "__main__":
     parser = argparse.ArgumentParser(description='Substitute values from ABC file into template.')
@@ -68,6 +69,7 @@
     vars["name"] = fname
     vars["title"] = getFieldDisplayText(lines, fdir, "T", latex=args.latex)
     vars["subtitle"] = getFieldDisplayText(lines, fdir, "T", n=2, latex=args.latex)
+    vars["fulltitle"] = getFullTitle(lines, fdir, latex=args.latex)
     vars["tradition"] = getFieldDisplayText(lines, fdir, "A", latex=args.latex)
     vars["composer"] = getFieldDisplayText(lines, fdir, "C", latex=args.latex)
     vars["key"] = getFieldDisplayText(lines, fdir, "K", latex=args.latex)
--- a/dottes.html.learnertune	Wed Oct 11 17:03:29 2017 +0100
+++ b/dottes.html.learnertune	Wed Oct 11 17:09:31 2017 +0100
@@ -48,7 +48,7 @@
             </div>
           </div>
         </div>
-        <p>${title} is in the key of ${key}.
+        <p>${fulltitle} is in the key of ${key}.
         <div class="dottes-tune-learner">
           <div class="dottes-tune-learner-speed-column"></div>
           <div class="dottes-tune-learner-play-column"></div>
@@ -149,7 +149,7 @@
             <div class="dottes-tune-footer-learner-booke">
               <a class="dottes-tune-icon-link" href="${name}.html">
                 <img class="dottes-tune-header-image" src="../img/music.png"
-                     alt="Tune dottes page" title="Go to dottes page for ${title}">
+                     alt="Tune dottes page" title="Go to dottes page for ${fulltitle}">
               </a>
               <a class="dottes-tune-icon-link" href="index.html">
                 <img class="dottes-tune-header-image" src="../img/book.png"
--- a/dottes.html.tune	Wed Oct 11 17:03:29 2017 +0100
+++ b/dottes.html.tune	Wed Oct 11 17:09:31 2017 +0100
@@ -84,7 +84,7 @@
             <div class="dottes-tune-footer-booke">
                 <a class="dottes-tune-icon-link" href="learner-${name}.html">
                   <img class="dottes-tune-header-image" src="../img/learner.png"
-                       alt="Learner tune page" title="Go to learner page for ${title}">
+                       alt="Learner tune page" title="Go to learner page for ${fulltitle}">
                 </a>
                 <a class="dottes-tune-icon-link" href="index.html">
                   <img class="dottes-tune-header-image" src="../img/book.png"
--- a/dottes.html.tuneindex	Wed Oct 11 17:03:29 2017 +0100
+++ b/dottes.html.tuneindex	Wed Oct 11 17:09:31 2017 +0100
@@ -1,16 +1,16 @@
 <div class="dottes-tune-list-item">
   <div class="dottes-tune-list-item-link">
-    <a class="dottes-tune-link" href="${name}.html">${title}</a>
+    <a class="dottes-tune-link" href="${name}.html">${fulltitle}</a>
   </div>
   <div class="dottes-tune-list-item-learner-link">
     <a class="dottes-tune-link" href="learner-${name}.html">
-      <img class="dottes-tune-table-image" src="../img/learner.png" alt="Learner">
+      <img class="dottes-tune-table-image" src="../img/learner.png" alt="Learner page" title="Learner page for ${fulltitle}">
     </a>
   </div>
   <div class="dottes-tune-list-item-image">
     <a href="${name}.html">
       <img class="dottes-tune-table-image" src="firstline-${name}.png"
-           alt="${title} first line">
+           alt="${fulltitle} first line" title="${fulltitle} first line">
     </a>
   </div>
 </div>