changeset 732:c81a1ed21877

Move prev/next field reading into abctemplate.py. Fishing all that stuff out in makeWeb.sh was a bit ugly.
author Jim Hague <jim.hague@acm.org>
date Thu, 12 Oct 2017 11:21:48 +0100
parents bb4aa5799d91
children 841f7c19cb55
files abctemplate.py makeWeb.sh
diffstat 2 files changed, 53 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/abctemplate.py	Wed Oct 11 23:14:30 2017 +0100
+++ b/abctemplate.py	Thu Oct 12 11:21:48 2017 +0100
@@ -40,6 +40,24 @@
 
 from abcfield import getFieldDisplayText, getFullTitle
 
+def getFileData(f, latex):
+    res = {}
+    input_path = pathlib.Path(args.input.name)
+    res["name"] = input_path.stem
+    fdir = input_path.parent
+
+    lines = f.readlines()
+    res["title"] = getFieldDisplayText(lines, fdir, "T", latex=latex)
+    res["subtitle"] = getFieldDisplayText(lines, fdir, "T", n=2, latex=latex)
+    res["fulltitle"] = getFullTitle(lines, fdir, latex=latex)
+    res["tradition"] = getFieldDisplayText(lines, fdir, "A", latex=latex)
+    res["composer"] = getFieldDisplayText(lines, fdir, "C", latex=latex)
+    res["key"] = getFieldDisplayText(lines, fdir, "K", latex=latex)
+    res["parts"] = getFieldDisplayText(lines, fdir, "P", latex=latex)
+    res["notes"] = getFieldDisplayText(lines, fdir, "N", starts="Dottes:", latex=latex)
+    res["history"] = getFieldDisplayText(lines, fdir, "H", starts="Dottes:", latex=latex)
+    return res
+
 if __name__ == "__main__":
     parser = argparse.ArgumentParser(description='Substitute values from ABC file into template.')
     parser.add_argument('-l', '--latex', dest='latex',
@@ -51,36 +69,41 @@
                         help='template file')
     parser.add_argument('-v', '--value', dest='values', action="append",
                         default=[], help='define var=value items for templater')
+    parser.add_argument('-n', '--next', dest='nextfile',
+                        type=argparse.FileType('r'),
+                        help='next tune file')
+    parser.add_argument('-p', '--prev', dest='prevfile',
+                        type=argparse.FileType('r'),
+                        help='previous tune file')
     parser.add_argument('input', type=argparse.FileType('r'),
                         help='input ABC file')
     args = parser.parse_args()
 
-    with args.input as f:
-        lines = f.readlines()
+    fields = getFileData(args.input, args.latex)
 
-    input_path = pathlib.Path(args.input.name)
-    fname = input_path.stem
-    fdir = input_path.parent
-
-    vars = dict()
+    vars = {}
     vars["notesvisibility"] = "no"
     vars["historyvisibility"] = "no"
 
-    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)
-    vars["parts"] = getFieldDisplayText(lines, fdir, "P", latex=args.latex)
-    vars["notes"] = getFieldDisplayText(lines, fdir, "N", starts="Dottes:", latex=args.latex)
+    for name in ["name", "title", "subtitle", "fulltitle", "tradition",
+                 "composer", "key", "parts", "notes", "history"]:
+        vars[name] = fields[name]
+
     if vars["notes"]:
         vars["notesvisibility"] = "yes"
-    vars["history"] = getFieldDisplayText(lines, fdir, "H", starts="Dottes:", latex=args.latex)
     if vars["history"]:
         vars["historyvisibility"] = "yes"
 
+    if args.nextfile:
+        fields = getFileData(args.nextfile, args.latex)
+    for name in ["name", "title", "subtitle", "fulltitle"]:
+        vars["next" + name] = fields[name] if args.nextfile else ""
+
+    if args.prevfile:
+        fields = getFileData(args.prevfile, args.latex)
+    for name in ["name", "title", "subtitle", "fulltitle"]:
+        vars["prev" + name] = fields[name] if args.prevfile else ""
+
     for val in args.values:
         keyval = val.partition("=")
         vars[keyval[0]] = keyval[2]
--- a/makeWeb.sh	Wed Oct 11 23:14:30 2017 +0100
+++ b/makeWeb.sh	Thu Oct 12 11:21:48 2017 +0100
@@ -93,25 +93,21 @@
     # Get date and time of last change to tune.
     lastchanged=`hg log --limit 1 --template "{date|shortdate}" $masterbookedir/${name}.abc`
 
-    # Get previous and next tune page names and titles.
+    # Get previous and next tunes, if available.
+    prevarg=""
     prevpage=""
-    prevtitle=""
-    prevfulltitle=""
+    nextarg=""
     nextpage=""
-    nexttitle=""
-    nextfulltitle=""
 
     if [ $i -gt 0 ]; then
         prev=${filenames[$((i - 1))]}
         prevpage=`basename $prev .abc`.html
-        prevtitle=`./abcfield.py --display --field="T" $prev`
-        prevfulltitle=`./abcfield.py --display --field="FT" $prev`
+        prevarg="--prev $prev"
     fi
     if [ $i -lt $((nofiles - 1)) ]; then
         next=${filenames[$((i + 1))]}
         nextpage=`basename $next .abc`.html
-        nexttitle=`./abcfield.py --display --field="T" $next`
-        nextfulltitle=`./abcfield.py --display --field="FT" $next`
+        nextarg="--next $next"
     fi
 
     # Generate the tune web page.
@@ -122,21 +118,17 @@
         --value "masterbooke=${masterbooke}" \
         --value "lastchanged=${lastchanged}" \
         --value "prevpage=${prevpage}" \
-        --value "prevtitle=${prevtitle}" \
-        --value "prevfulltitle=${prevfulltitle}" \
         --value "nextpage=${nextpage}" \
-        --value "nexttitle=${nexttitle}" \
-        --value "nextfulltitle=${nextfulltitle}" \
-        --template dottes.html.tune $filename > $webdir/$tunepage
+        ${prevarg} ${nextarg} \
+        --template dottes.html.tune \
+        $filename > $webdir/$tunepage
     $dir/abctemplate.py \
         --value "masterbooke=${masterbooke}" \
         --value "lastchanged=${lastchanged}" \
-        --value "prevpage=learner-${prevpage}" \
-        --value "prevtitle=${prevtitle}" \
-        --value "prevfulltitle=${prevfulltitle}" \
-        --value "nextpage=learner-${nextpage}" \
-        --value "nexttitle=${nexttitle}" \
-        --value "nextfulltitle=${nextfulltitle}" \
-        --template dottes.html.learnertune $filename > $webdir/$learnerpage
+        --value "prevpage=${prevpage}" \
+        --value "nextpage=${nextpage}" \
+        ${prevarg} ${nextarg} \
+        --template dottes.html.learnertune \
+        $filename > $webdir/$learnerpage
     $dir/abctemplate.py --template dottes.html.tuneindex $filename >> $webdir/$tunelist
 done