annotate abctemplate.py @ 708:7da70e8900ab build-default-278

First attempt at adding Tradition below subtitle in web and print.
author Jim Hague <jim.hague@acm.org>
date Sun, 08 Oct 2017 17:06:09 +0100
parents ed823ea54c83
children 7e9d5852c802
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.
708
7da70e8900ab First attempt at adding Tradition below subtitle in web and print.
Jim Hague <jim.hague@acm.org>
parents: 598
diff changeset
24 # * tradition. The Morris tradition the dance tune is from.
588
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
25 # * 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
26 # * 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
27 # * 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
28 # * 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
29 # * 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
30 # * 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
31 # * 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
32 # * 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
33 #
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
34
588
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
35 import argparse
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
36 import pathlib
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
37 import string
586
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
38
588
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
39 from abcfield import getFieldDisplayText
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
40
586
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
41 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
42 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
43 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
44 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
45 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
46 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
47 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
48 required=True,
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
49 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
50 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
51 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
52 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
53 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
54 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
55
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
56 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
57 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
58
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
59 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
60 fname = input_path.stem
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
61 fdir = input_path.parent
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
62
588
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
63 vars = dict()
594
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
64 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
65 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
66
594
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
67 vars["name"] = fname
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
68 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
69 vars["subtitle"] = getFieldDisplayText(lines, fdir, "T", n=2, latex=args.latex)
708
7da70e8900ab First attempt at adding Tradition below subtitle in web and print.
Jim Hague <jim.hague@acm.org>
parents: 598
diff changeset
70 vars["tradition"] = getFieldDisplayText(lines, fdir, "A", latex=args.latex)
594
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
71 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
72 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
73 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
74 if vars["notes"]:
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
75 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
76 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
77 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
78 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
79
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
80 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
81 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
82 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
83
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
84 print(string.Template(args.template.read()).substitute(vars))