annotate abctemplate.py @ 638:776a455efe47 build-default-252

Automated merge with ssh://hg.cryhavoc.org.uk/dottes
author Jim Hague <jim.hague@acm.org>
date Mon, 07 Nov 2016 20:34:45 +0000
parents ed823ea54c83
children 7da70e8900ab
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)
594
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
59 fname = input_path.stem
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
60 fdir = input_path.parent
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
61
588
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
62 vars = dict()
594
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
63 vars["notesvisibility"] = "no"
598
ed823ea54c83 Add Dottes History to notes. Currently appears after notes with a gap.
Jim Hague <jim.hague@acm.org>
parents: 594
diff changeset
64 vars["historyvisibility"] = "no"
588
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
65
594
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
66 vars["name"] = fname
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
67 vars["title"] = getFieldDisplayText(lines, fdir, "T", latex=args.latex)
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
68 vars["subtitle"] = getFieldDisplayText(lines, fdir, "T", n=2, latex=args.latex)
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
69 vars["composer"] = getFieldDisplayText(lines, fdir, "C", latex=args.latex)
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
70 vars["key"] = getFieldDisplayText(lines, fdir, "K", latex=args.latex)
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
71 vars["notes"] = getFieldDisplayText(lines, fdir, "N", starts="Dottes:", latex=args.latex)
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
72 if vars["notes"]:
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
73 vars["notesvisibility"] = "yes"
598
ed823ea54c83 Add Dottes History to notes. Currently appears after notes with a gap.
Jim Hague <jim.hague@acm.org>
parents: 594
diff changeset
74 vars["history"] = getFieldDisplayText(lines, fdir, "H", starts="Dottes:", latex=args.latex)
ed823ea54c83 Add Dottes History to notes. Currently appears after notes with a gap.
Jim Hague <jim.hague@acm.org>
parents: 594
diff changeset
75 if vars["history"]:
ed823ea54c83 Add Dottes History to notes. Currently appears after notes with a gap.
Jim Hague <jim.hague@acm.org>
parents: 594
diff changeset
76 vars["historyvisibility"] = "yes"
588
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
77
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
78 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
79 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
80 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
81
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
82 print(string.Template(args.template.read()).substitute(vars))