Mercurial > dottes
annotate abctemplate.py @ 1035:889ae46036fb
Add Inisheer.
author | Jim Hague <jim.hague@acm.org> |
---|---|
date | Wed, 19 Feb 2020 13:30:24 +0000 |
parents | c81a1ed21877 |
children |
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 |
732
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
43 def getFileData(f, latex): |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
44 res = {} |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
45 input_path = pathlib.Path(args.input.name) |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
46 res["name"] = input_path.stem |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
47 fdir = input_path.parent |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
48 |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
49 lines = f.readlines() |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
50 res["title"] = getFieldDisplayText(lines, fdir, "T", latex=latex) |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
51 res["subtitle"] = getFieldDisplayText(lines, fdir, "T", n=2, latex=latex) |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
52 res["fulltitle"] = getFullTitle(lines, fdir, latex=latex) |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
53 res["tradition"] = getFieldDisplayText(lines, fdir, "A", latex=latex) |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
54 res["composer"] = getFieldDisplayText(lines, fdir, "C", latex=latex) |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
55 res["key"] = getFieldDisplayText(lines, fdir, "K", latex=latex) |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
56 res["parts"] = getFieldDisplayText(lines, fdir, "P", latex=latex) |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
57 res["notes"] = getFieldDisplayText(lines, fdir, "N", starts="Dottes:", latex=latex) |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
58 res["history"] = getFieldDisplayText(lines, fdir, "H", starts="Dottes:", latex=latex) |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
59 return res |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
60 |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
61 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
|
62 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 required=True, |
afc031477784
Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents:
586
diff
changeset
|
69 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
|
70 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
|
71 default=[], help='define var=value items for templater') |
732
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
72 parser.add_argument('-n', '--next', dest='nextfile', |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
73 type=argparse.FileType('r'), |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
74 help='next tune file') |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
75 parser.add_argument('-p', '--prev', dest='prevfile', |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
76 type=argparse.FileType('r'), |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
77 help='previous tune file') |
588
afc031477784
Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents:
586
diff
changeset
|
78 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
|
79 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
|
80 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
|
81 |
732
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
82 fields = getFileData(args.input, args.latex) |
588
afc031477784
Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents:
586
diff
changeset
|
83 |
732
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
84 vars = {} |
594
60749c792cde
Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents:
588
diff
changeset
|
85 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
|
86 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
|
87 |
732
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
88 for name in ["name", "title", "subtitle", "fulltitle", "tradition", |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
89 "composer", "key", "parts", "notes", "history"]: |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
90 vars[name] = fields[name] |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
91 |
594
60749c792cde
Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents:
588
diff
changeset
|
92 if vars["notes"]: |
60749c792cde
Replace Change and Credit with Markdown-enabled Notes section.
Jim Hague <jim.hague@acm.org>
parents:
588
diff
changeset
|
93 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
|
94 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
|
95 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
|
96 |
732
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
97 if args.nextfile: |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
98 fields = getFileData(args.nextfile, args.latex) |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
99 for name in ["name", "title", "subtitle", "fulltitle"]: |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
100 vars["next" + name] = fields[name] if args.nextfile else "" |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
101 |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
102 if args.prevfile: |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
103 fields = getFileData(args.prevfile, args.latex) |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
104 for name in ["name", "title", "subtitle", "fulltitle"]: |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
105 vars["prev" + name] = fields[name] if args.prevfile else "" |
c81a1ed21877
Move prev/next field reading into abctemplate.py.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
106 |
588
afc031477784
Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents:
586
diff
changeset
|
107 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
|
108 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
|
109 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
|
110 |
afc031477784
Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents:
586
diff
changeset
|
111 print(string.Template(args.template.read()).substitute(vars)) |