2016-11-02 00:21:18 +00:00
|
|
|
#!/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 '<header>:+' *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.
|
2017-10-08 17:06:09 +01:00
|
|
|
# * tradition. The Morris tradition the dance tune is from.
|
2016-11-02 00:21:18 +00:00
|
|
|
# * 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)
|
2016-11-02 15:02:06 +00:00
|
|
|
fname = input_path.stem
|
|
|
|
fdir = input_path.parent
|
2016-11-02 00:21:18 +00:00
|
|
|
|
|
|
|
vars = dict()
|
2016-11-02 15:02:06 +00:00
|
|
|
vars["notesvisibility"] = "no"
|
2016-11-02 18:36:31 +00:00
|
|
|
vars["historyvisibility"] = "no"
|
2016-11-02 00:21:18 +00:00
|
|
|
|
2016-11-02 15:02:06 +00:00
|
|
|
vars["name"] = fname
|
|
|
|
vars["title"] = getFieldDisplayText(lines, fdir, "T", latex=args.latex)
|
|
|
|
vars["subtitle"] = getFieldDisplayText(lines, fdir, "T", n=2, latex=args.latex)
|
2017-10-08 17:06:09 +01:00
|
|
|
vars["tradition"] = getFieldDisplayText(lines, fdir, "A", latex=args.latex)
|
2016-11-02 15:02:06 +00:00
|
|
|
vars["composer"] = getFieldDisplayText(lines, fdir, "C", latex=args.latex)
|
|
|
|
vars["key"] = getFieldDisplayText(lines, fdir, "K", latex=args.latex)
|
|
|
|
vars["notes"] = getFieldDisplayText(lines, fdir, "N", starts="Dottes:", latex=args.latex)
|
|
|
|
if vars["notes"]:
|
|
|
|
vars["notesvisibility"] = "yes"
|
2016-11-02 18:36:31 +00:00
|
|
|
vars["history"] = getFieldDisplayText(lines, fdir, "H", starts="Dottes:", latex=args.latex)
|
|
|
|
if vars["history"]:
|
|
|
|
vars["historyvisibility"] = "yes"
|
2016-11-02 00:21:18 +00:00
|
|
|
|
|
|
|
for val in args.values:
|
|
|
|
keyval = val.partition("=")
|
|
|
|
vars[keyval[0]] = keyval[2]
|
|
|
|
|
|
|
|
print(string.Template(args.template.read()).substitute(vars))
|