annotate abctemplate.py @ 726:833e6185b6a2 build-default-282

Add fulltitle to template fields. fulltitle is title + [(" subtitle ")"] if subtitle is present. To do: extend full title to next and prev. this means exposing it in abcfield.py.
author Jim Hague <jim.hague@acm.org>
date Wed, 11 Oct 2017 17:09:31 +0100
parents 7e9d5852c802
children c81a1ed21877
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.
726
833e6185b6a2 Add fulltitle to template fields.
Jim Hague <jim.hague@acm.org>
parents: 712
diff changeset
24 # * fulltitle. The tune title followed, if it exists, by " (" subtitle ")"
708
7da70e8900ab First attempt at adding Tradition below subtitle in web and print.
Jim Hague <jim.hague@acm.org>
parents: 598
diff changeset
25 # * 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
26 # * 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
27 # * key. The tune key.
712
7e9d5852c802 Show parts at top left of tune.
Jim Hague <jim.hague@acm.org>
parents: 708
diff changeset
28 # * parts. The tune parts order (A(AB2)3 etc.).
588
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
29 # * 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
30 # * 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
31 # * 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
32 # * 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
33 # * 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
34 # * 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
35 #
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
36
588
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
37 import argparse
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
38 import pathlib
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
39 import string
586
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
40
726
833e6185b6a2 Add fulltitle to template fields.
Jim Hague <jim.hague@acm.org>
parents: 712
diff changeset
41 from abcfield import getFieldDisplayText, getFullTitle
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
42
586
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
43 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
44 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
45 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
46 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
47 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
48 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
49 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
50 required=True,
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
51 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
52 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
53 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
54 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
55 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
56 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
57
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
58 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
59 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
60
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
61 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
62 fname = input_path.stem
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
63 fdir = input_path.parent
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
64
588
afc031477784 Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents: 586
diff changeset
65 vars = dict()
594
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
66 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
67 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
68
594
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
69 vars["name"] = fname
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
70 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
71 vars["subtitle"] = getFieldDisplayText(lines, fdir, "T", n=2, latex=args.latex)
726
833e6185b6a2 Add fulltitle to template fields.
Jim Hague <jim.hague@acm.org>
parents: 712
diff changeset
72 vars["fulltitle"] = getFullTitle(lines, fdir, 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
73 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
74 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
75 vars["key"] = getFieldDisplayText(lines, fdir, "K", latex=args.latex)
712
7e9d5852c802 Show parts at top left of tune.
Jim Hague <jim.hague@acm.org>
parents: 708
diff changeset
76 vars["parts"] = getFieldDisplayText(lines, fdir, "P", latex=args.latex)
594
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
77 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
78 if vars["notes"]:
60749c792cde Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
79 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
80 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
81 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
82 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
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))