Mercurial > dottes
comparison abctemplate.py @ 588:afc031477784
Replace sed substitution with Python templating for HTML and LaTeX output.
| author | Jim Hague <jim.hague@acm.org> |
|---|---|
| date | Wed, 02 Nov 2016 00:21:18 +0000 |
| parents | abcfield.py@daa3b76bd11f |
| children | 60749c792cde |
comparison
equal
deleted
inserted
replaced
| 587:1b79867b4f35 | 588:afc031477784 |
|---|---|
| 1 #!/usr/bin/env python3 | |
| 2 # | |
| 3 # Fill in a template with data from fields in an ABC file. | |
| 4 # Fields have any ABC accented characters converted to HTML (default) or Latex. | |
| 5 # | |
| 6 # Rearrange some field contents into display format: | |
| 7 # * In Title fields, change 'sort' form such as 'Exploding Potato, The' | |
| 8 # to display format 'The Exploding Potato'. | |
| 9 # * In Key fields, translate the ABC key representation to full text, | |
| 10 # e.g. G#dor becomes G# Dorian. | |
| 11 # | |
| 12 # Recognise continuation header fields and print those too. The ABC standard | |
| 13 # defines continuation fields as starting ':+'. Regrettably none of the tools | |
| 14 # I am using the Booke recognise that syntax, so I am adopting a Booke | |
| 15 # convention of '<header>:+' *also* being a continuation. Note that a | |
| 16 # continuation is a distinct line in the field value; the value has a line | |
| 17 # break between it and the previous line. | |
| 18 # | |
| 19 # Templates are read from file, and are in Python standard library format. | |
| 20 # The following values are substituted: | |
| 21 # * name. The file base name. Base filename without extension. | |
| 22 # * title. The tune title. | |
| 23 # * subtitle. The tune subtitle (second Title field), if any. | |
| 24 # * composer. The tune composer. | |
| 25 # * key. The tune key. | |
| 26 # * changefile. The name of the 'change' file, if any. | |
| 27 # * changename. The change file base name. | |
| 28 # * changetitle. The change file tune title. | |
| 29 # * changevisibility. "yes" if there's a change value, otherwise "no". | |
| 30 # * credit. The 'credit' value. | |
| 31 # * creditvisibility. "yes" if there's a credit value, otherwise "no". | |
| 32 # | |
| 33 | |
| 34 import argparse | |
| 35 import pathlib | |
| 36 import string | |
| 37 | |
| 38 from abcfield import getFieldDisplayText | |
| 39 | |
| 40 if __name__ == "__main__": | |
| 41 parser = argparse.ArgumentParser(description='Substitute values from ABC file into template.') | |
| 42 parser.add_argument('-l', '--latex', dest='latex', | |
| 43 action='store_true', | |
| 44 help='output LaTeX formatted values (default is HTML)') | |
| 45 parser.add_argument('-t', '--template', dest='template', | |
| 46 type=argparse.FileType('r'), | |
| 47 required=True, | |
| 48 help='template file') | |
| 49 parser.add_argument('-v', '--value', dest='values', action="append", | |
| 50 default=[], help='define var=value items for templater') | |
| 51 parser.add_argument('input', type=argparse.FileType('r'), | |
| 52 help='input ABC file') | |
| 53 args = parser.parse_args() | |
| 54 | |
| 55 with args.input as f: | |
| 56 lines = f.readlines() | |
| 57 | |
| 58 input_path = pathlib.Path(args.input.name) | |
| 59 | |
| 60 vars = dict() | |
| 61 vars["changename"] = "" | |
| 62 vars["changetitle"] = "" | |
| 63 vars["changevisibility"] = "no" | |
| 64 vars["creditvisibility"] = "no" | |
| 65 | |
| 66 vars["name"] = input_path.stem | |
| 67 vars["title"] = getFieldDisplayText(lines, "T", latex=args.latex) | |
| 68 vars["subtitle"] = getFieldDisplayText(lines, "T", n=2, latex=args.latex) | |
| 69 vars["composer"] = getFieldDisplayText(lines, "C", latex=args.latex) | |
| 70 vars["key"] = getFieldDisplayText(lines, "K", latex=args.latex) | |
| 71 vars["changefile"] = getFieldDisplayText(lines, "N", starts="Change:", latex=args.latex) | |
| 72 vars["credit"] = getFieldDisplayText(lines, "N", starts="Credit:", latex=args.latex) | |
| 73 | |
| 74 if vars["changefile"]: | |
| 75 vars["changevisibility"] = "yes" | |
| 76 vars["changename"] = pathlib.Path(vars["changefile"]).stem | |
| 77 cf = pathlib.Path(input_path.parent, vars["changefile"]) | |
| 78 with cf.open() as f: | |
| 79 vars["changetitle"] = getFieldDisplayText(f, "T", latex=args.latex) | |
| 80 | |
| 81 if vars["credit"]: | |
| 82 vars["creditvisibility"] = "yes" | |
| 83 | |
| 84 for val in args.values: | |
| 85 keyval = val.partition("=") | |
| 86 vars[keyval[0]] = keyval[2] | |
| 87 | |
| 88 print(string.Template(args.template.read()).substitute(vars)) |
