diff abctemplate.py @ 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 833e6185b6a2
children
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]