From 262ee6c3e4b15a9d545cb8468e6216b546004fbb Mon Sep 17 00:00:00 2001 From: Jim Hague Date: Wed, 2 Nov 2016 00:21:18 +0000 Subject: [PATCH] Replace sed substitution with Python templating for HTML and LaTeX output. --- abcfield.py | 2 +- abctemplate.py | 88 +++++++++++++++++++++++++++++++++++++++ dottes.html.learnertune | 60 +++++++++++++------------- dottes.html.tune | 42 +++++++++---------- dottes.html.tuneindex | 10 ++--- dottes.tex.firstline-tune | 1 + dottes.tex.tune | 1 + makeBookeTunePages.sh | 19 +-------- makeWeb.sh | 55 +++--------------------- 9 files changed, 155 insertions(+), 123 deletions(-) create mode 100755 abctemplate.py create mode 100644 dottes.tex.firstline-tune create mode 100644 dottes.tex.tune diff --git a/abcfield.py b/abcfield.py index e8f1501..b0aec83 100755 --- a/abcfield.py +++ b/abcfield.py @@ -156,7 +156,7 @@ def convertKeyToDisplay(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): - res = None + res = "" for line in inf: line = line.strip() if len(line) > 2 and line[1] == ':': diff --git a/abctemplate.py b/abctemplate.py new file mode 100755 index 0000000..52e0be2 --- /dev/null +++ b/abctemplate.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python3 +# +# Fill in a template with data from fields in an ABC file. +# Fields have any ABC accented characters converted to HTML (default) or Latex. +# +# Rearrange some field contents into display format: +# * In Title fields, change 'sort' form such as 'Exploding Potato, The' +# to display format 'The Exploding Potato'. +# * In Key fields, translate the ABC key representation to full text, +# e.g. G#dor becomes G# Dorian. +# +# Recognise continuation header fields and print those too. The ABC standard +# defines continuation fields as starting ':+'. Regrettably none of the tools +# I am using the Booke recognise that syntax, so I am adopting a Booke +# convention of '
:+' *also* being a continuation. Note that a +# continuation is a distinct line in the field value; the value has a line +# break between it and the previous line. +# +# Templates are read from file, and are in Python standard library format. +# The following values are substituted: +# * name. The file base name. Base filename without extension. +# * title. The tune title. +# * subtitle. The tune subtitle (second Title field), if any. +# * composer. The tune composer. +# * key. The tune key. +# * changefile. The name of the 'change' file, if any. +# * changename. The change file base name. +# * changetitle. The change file tune title. +# * changevisibility. "yes" if there's a change value, otherwise "no". +# * credit. The 'credit' value. +# * creditvisibility. "yes" if there's a credit value, otherwise "no". +# + +import argparse +import pathlib +import string + +from abcfield import getFieldDisplayText + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='Substitute values from ABC file into template.') + parser.add_argument('-l', '--latex', dest='latex', + action='store_true', + help='output LaTeX formatted values (default is HTML)') + parser.add_argument('-t', '--template', dest='template', + type=argparse.FileType('r'), + required=True, + help='template file') + parser.add_argument('-v', '--value', dest='values', action="append", + default=[], help='define var=value items for templater') + parser.add_argument('input', type=argparse.FileType('r'), + help='input ABC file') + args = parser.parse_args() + + with args.input as f: + lines = f.readlines() + + input_path = pathlib.Path(args.input.name) + + vars = dict() + vars["changename"] = "" + vars["changetitle"] = "" + vars["changevisibility"] = "no" + vars["creditvisibility"] = "no" + + vars["name"] = input_path.stem + vars["title"] = getFieldDisplayText(lines, "T", latex=args.latex) + vars["subtitle"] = getFieldDisplayText(lines, "T", n=2, latex=args.latex) + vars["composer"] = getFieldDisplayText(lines, "C", latex=args.latex) + vars["key"] = getFieldDisplayText(lines, "K", latex=args.latex) + vars["changefile"] = getFieldDisplayText(lines, "N", starts="Change:", latex=args.latex) + vars["credit"] = getFieldDisplayText(lines, "N", starts="Credit:", latex=args.latex) + + if vars["changefile"]: + vars["changevisibility"] = "yes" + vars["changename"] = pathlib.Path(vars["changefile"]).stem + cf = pathlib.Path(input_path.parent, vars["changefile"]) + with cf.open() as f: + vars["changetitle"] = getFieldDisplayText(f, "T", latex=args.latex) + + if vars["credit"]: + vars["creditvisibility"] = "yes" + + for val in args.values: + keyval = val.partition("=") + vars[keyval[0]] = keyval[2] + + print(string.Template(args.template.read()).substitute(vars)) diff --git a/dottes.html.learnertune b/dottes.html.learnertune index a1baff2..2b7045f 100644 --- a/dottes.html.learnertune +++ b/dottes.html.learnertune @@ -2,7 +2,7 @@ - Cry Havoc tunes - learning @TITLE@ + Cry Havoc tunes - learning ${title} @@ -33,7 +33,7 @@
- + Dottes @@ -43,15 +43,15 @@
-

@TITLE@

-

@SUBTITLE@

+

${title}

+

${subtitle}

- @COMPOSER@ + ${composer}
-

@TITLE@ is in the key of @KEY@. +

${title} is in the key of ${key}.