From fd98182250e69c8b23db1d51e08961c62ae0eb84 Mon Sep 17 00:00:00 2001 From: Jim Hague Date: Thu, 12 Oct 2017 11:21:48 +0100 Subject: [PATCH] Move prev/next field reading into abctemplate.py. Fishing all that stuff out in makeWeb.sh was a bit ugly. --- abctemplate.py | 57 +++++++++++++++++++++++++++++++++++--------------- makeWeb.sh | 34 ++++++++++++------------------ 2 files changed, 53 insertions(+), 38 deletions(-) diff --git a/abctemplate.py b/abctemplate.py index b3f7ec2..82b0ea4 100755 --- a/abctemplate.py +++ b/abctemplate.py @@ -40,6 +40,24 @@ import string 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 @@ if __name__ == "__main__": 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] diff --git a/makeWeb.sh b/makeWeb.sh index ae4d3ec..8ac82ec 100755 --- a/makeWeb.sh +++ b/makeWeb.sh @@ -93,25 +93,21 @@ do # 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 @@ do --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