annotate abctemplate.py @ 591:2face6618bf3

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