Mercurial > dottes
annotate abcfield.py @ 961:fc2b75f6c284 build-default-349
Go to separate left and right double quote special characters.
" works in HTML, but LaTeX was using right double quotes everywhere.
author | Jim Hague <jim.hague@acm.org> |
---|---|
date | Wed, 07 Aug 2019 10:12:35 +0100 |
parents | d6e2a281cceb |
children | e4d31e094d24 |
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 # |
122
295ba8275ab4
Make output larger where possible.
Jim Hague <jim.hague@laicatc.com>
parents:
110
diff
changeset
|
3 # Extact a text field (title, by default) from a .abc file, and print it out |
581
760d0ae5acea
Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents:
326
diff
changeset
|
4 # with 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 # |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
6 # Optionally rearrange a field into display format: |
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 # |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
19 |
733
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
20 import argparse |
592
9986c67edf91
Implement custom markdown <foo.abc> to reference tune.
Jim Hague <jim.hague@acm.org>
parents:
591
diff
changeset
|
21 import pathlib |
591
2face6618bf3
Convert Markdown in N and H fields. Fix up .abc links.
Jim Hague <jim.hague@acm.org>
parents:
588
diff
changeset
|
22 import re |
2face6618bf3
Convert Markdown in N and H fields. Fix up .abc links.
Jim Hague <jim.hague@acm.org>
parents:
588
diff
changeset
|
23 import subprocess |
110
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
24 import sys |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
25 |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
26 accentedletters = { |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
27 # Acute accents |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
28 "'A" : ("Á", "\\'{A}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
29 "'E" : ("É", "\\'{E}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
30 "'I" : ("Í", "\\'{I}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
31 "'O" : ("Ó", "\\'{O}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
32 "'U" : ("Ú", "\\'{U}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
33 "'Y" : ("Ý", "\\'{Y}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
34 "'a" : ("á", "\\'{a}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
35 "'e" : ("é", "\\'{e}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
36 "'i" : ("í", "\\'{i}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
37 "'o" : ("ó", "\\'{o}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
38 "'u" : ("ú", "\\'{u}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
39 "'y" : ("ý", "\\'{y}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
40 |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
41 # Grave accents |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
42 "`A" : ("À", "\\`{A}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
43 "`E" : ("È", "\\`{E}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
44 "`I" : ("Ì", "\\`{I}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
45 "`O" : ("Ò", "\\`{O}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
46 "`U" : ("Ù", "\\`{U}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
47 "`a" : ("à", "\\`{a}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
48 "`e" : ("è", "\\`{e}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
49 "`i" : ("ì", "\\`{i}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
50 "`o" : ("ò", "\\`{o}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
51 "`u" : ("ù", "\\`{u}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
52 |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
53 # Umlauts |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
54 "\"A" : ("Ä", "\\\"{A}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
55 "\"E" : ("Ë", "\\\"{E}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
56 "\"I" : ("Ï", "\\\"{I}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
57 "\"O" : ("Ö", "\\\"{O}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
58 "\"U" : ("Ü", "\\\"{U}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
59 "\"Y" : ("Ÿ", "\\\"{Y}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
60 "\"a" : ("ä", "\\\"{a}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
61 "\"e" : ("ë", "\\\"{e}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
62 "\"i" : ("ï", "\\\"{\i}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
63 "\"o" : ("ö", "\\\"{o}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
64 "\"u" : ("ü", "\\\"{u}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
65 "\"y" : ("ÿ", "\\\"{y}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
66 |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
67 # Circumflexes |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
68 "^A" : ("Â", "\\^{A}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
69 "^E" : ("Ê", "\\^{E}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
70 "^I" : ("Î", "\\^{I}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
71 "^O" : ("Ô", "\\^{O}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
72 "^U" : ("Û", "\\^{U}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
73 "^a" : ("â", "\\^{a}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
74 "^e" : ("ê", "\\^{e}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
75 "^i" : ("î", "\\^{\i}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
76 "^o" : ("ô", "\\^{o}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
77 "^u" : ("û", "\\^{u}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
78 |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
79 # Tilde |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
80 "~A" : ("Ã", "\\~{A}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
81 "~N" : ("Ñ", "\\~{N}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
82 "~O" : ("Õ", "\\~{O}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
83 "~a" : ("ã", "\\~{a}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
84 "~n" : ("ñ", "\\~{n}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
85 "~o" : ("õ", "\\~{o}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
86 |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
87 # Cedilla |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
88 ",C" : ("Ç", "\\c{C}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
89 ",c" : ("ç", "\\c{c}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
90 |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
91 # Slash |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
92 "/O" : ("Ø", "\\O"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
93 "/o" : ("ø", "\\o"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
94 |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
95 # Ring |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
96 "AA" : ("Å", "\\r{A}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
97 "aa" : ("å", "\\r{a}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
98 |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
99 # Ligatures |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
100 "AE" : ("Æ", "\\AE"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
101 "ae" : ("æ", "\\ae"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
102 "ss" : ("ß", "\\ss"), |
725
f9a554e858f3
Provide quoting for single and double quotes.
Jim Hague <jim.hague@acm.org>
parents:
722
diff
changeset
|
103 |
f9a554e858f3
Provide quoting for single and double quotes.
Jim Hague <jim.hague@acm.org>
parents:
722
diff
changeset
|
104 # Quote marks |
f9a554e858f3
Provide quoting for single and double quotes.
Jim Hague <jim.hague@acm.org>
parents:
722
diff
changeset
|
105 "''" : ("'", "'"), |
961
fc2b75f6c284
Go to separate left and right double quote special characters.
Jim Hague <jim.hague@acm.org>
parents:
943
diff
changeset
|
106 "'`" : ("“", "``"), |
fc2b75f6c284
Go to separate left and right double quote special characters.
Jim Hague <jim.hague@acm.org>
parents:
943
diff
changeset
|
107 "'\"" : ("”", "''"), |
110
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
108 } |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
109 |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
110 abckeys = { |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
111 "m": "Minor", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
112 "min": "Minor", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
113 "mix": "Mixolydian", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
114 "dor": "Dorian", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
115 "phr": "Phrygian", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
116 "lyd": "Lydian", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
117 "loc": "Locrian", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
118 } |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
119 |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
120 # Convert ABC accented chars to HTML entities or LaTex. |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
121 def convertAccents(t, latex=False): |
110
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
122 res = "" |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
123 while True: |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
124 p = t.partition('\\') |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
125 res += p[0] |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
126 if p[1] == "": |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
127 break |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
128 abc = p[2][0:2] |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
129 t = p[2][2:] |
581
760d0ae5acea
Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents:
326
diff
changeset
|
130 if abc in accentedletters: |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
131 if latex: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
132 res += accentedletters[abc][1] |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
133 else: |
110
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
134 res += accentedletters[abc][0] |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
135 else: |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
136 res += "\\" + abc |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
137 return res |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
138 |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
139 # Convert Title fields from sort to display, so Bat, The->The Bat. |
846
6bd946700312
Only the first title should be converted to from sort to display order.
Jim Hague <jim.hague@acm.org>
parents:
735
diff
changeset
|
140 # This only happens for the main title, i.e. the first title in a tune. |
6bd946700312
Only the first title should be converted to from sort to display order.
Jim Hague <jim.hague@acm.org>
parents:
735
diff
changeset
|
141 # Subtitles are not affected, as they don't influence sort order. |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
142 def convertTitleToDisplay(t): |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
143 p = t.rpartition(',') |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
144 if p[1] == "": |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
145 return t |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
146 else: |
701
972d3dab1142
Nerys next: L'Inconnu de Limoise.
Jim Hague <jim.hague@acm.org>
parents:
593
diff
changeset
|
147 first = p[2].strip() |
972d3dab1142
Nerys next: L'Inconnu de Limoise.
Jim Hague <jim.hague@acm.org>
parents:
593
diff
changeset
|
148 second = p[0].strip() |
972d3dab1142
Nerys next: L'Inconnu de Limoise.
Jim Hague <jim.hague@acm.org>
parents:
593
diff
changeset
|
149 return (first + " " if first.isalnum() else first) + second |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
150 |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
151 # Convert Key field from ABC to display, so G#dor->G# Dorian. |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
152 def convertKeyToDisplay(t): |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
153 letter = t[0].upper() |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
154 accidental = "" |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
155 mode = "" |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
156 try: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
157 accidental = t[1] |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
158 if accidental == '#' or accidental == 'b': |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
159 mode = t[2:] |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
160 else: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
161 accidental = "" |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
162 mode = t[1:] |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
163 except IndexError: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
164 pass |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
165 mode = mode.strip().lower() |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
166 return letter + accidental + ' ' + abckeys.get(mode, "Major") |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
167 |
591
2face6618bf3
Convert Markdown in N and H fields. Fix up .abc links.
Jim Hague <jim.hague@acm.org>
parents:
588
diff
changeset
|
168 # Convert input string from Markdown to HTML or LaTeX. Fix up link |
2face6618bf3
Convert Markdown in N and H fields. Fix up .abc links.
Jim Hague <jim.hague@acm.org>
parents:
588
diff
changeset
|
169 # targets so any 'foo.abc' target links to the tune with that name. |
2face6618bf3
Convert Markdown in N and H fields. Fix up .abc links.
Jim Hague <jim.hague@acm.org>
parents:
588
diff
changeset
|
170 def convertMarkdown(t, latex): |
2face6618bf3
Convert Markdown in N and H fields. Fix up .abc links.
Jim Hague <jim.hague@acm.org>
parents:
588
diff
changeset
|
171 if latex: |
2face6618bf3
Convert Markdown in N and H fields. Fix up .abc links.
Jim Hague <jim.hague@acm.org>
parents:
588
diff
changeset
|
172 target = "--to=latex" |
2face6618bf3
Convert Markdown in N and H fields. Fix up .abc links.
Jim Hague <jim.hague@acm.org>
parents:
588
diff
changeset
|
173 else: |
2face6618bf3
Convert Markdown in N and H fields. Fix up .abc links.
Jim Hague <jim.hague@acm.org>
parents:
588
diff
changeset
|
174 target = "--to=html" |
2face6618bf3
Convert Markdown in N and H fields. Fix up .abc links.
Jim Hague <jim.hague@acm.org>
parents:
588
diff
changeset
|
175 res = subprocess.check_output(['pandoc', '--from=markdown', target], input=t, universal_newlines=True) |
2face6618bf3
Convert Markdown in N and H fields. Fix up .abc links.
Jim Hague <jim.hague@acm.org>
parents:
588
diff
changeset
|
176 if latex: |
592
9986c67edf91
Implement custom markdown <foo.abc> to reference tune.
Jim Hague <jim.hague@acm.org>
parents:
591
diff
changeset
|
177 res = re.sub(r'\\href{(.*?).abc}', r'\\hyperlink{\1}', res) |
591
2face6618bf3
Convert Markdown in N and H fields. Fix up .abc links.
Jim Hague <jim.hague@acm.org>
parents:
588
diff
changeset
|
178 else: |
943 | 179 res = re.sub(r'href="(.*?).abc"', r'href="../\1/index.html"', res) |
593
82e818c41e81
Give ABC file directory when expanding markdown.
Jim Hague <jim.hague@acm.org>
parents:
592
diff
changeset
|
180 return res.strip() |
591
2face6618bf3
Convert Markdown in N and H fields. Fix up .abc links.
Jim Hague <jim.hague@acm.org>
parents:
588
diff
changeset
|
181 |
592
9986c67edf91
Implement custom markdown <foo.abc> to reference tune.
Jim Hague <jim.hague@acm.org>
parents:
591
diff
changeset
|
182 # Implement a custom Markdown shorthand for referencing ABC files. |
9986c67edf91
Implement custom markdown <foo.abc> to reference tune.
Jim Hague <jim.hague@acm.org>
parents:
591
diff
changeset
|
183 # <foo.abc> will expand to ['title of foo'](foo.abc). |
735
68f926e61d16
Revise Markdown handling of character entities.
Jim Hague <jim.hague@acm.org>
parents:
734
diff
changeset
|
184 def expandCustomMarkdown(t, dir): |
592
9986c67edf91
Implement custom markdown <foo.abc> to reference tune.
Jim Hague <jim.hague@acm.org>
parents:
591
diff
changeset
|
185 # Given a match to (foo.abc), return a markdown link to the tune with the |
722
f8ab14cc9d8d
Add previous and next tune links to website.
Jim Hague <jim.hague@acm.org>
parents:
716
diff
changeset
|
186 # title (and subtitle, if present) of the tune as the text of the link. |
735
68f926e61d16
Revise Markdown handling of character entities.
Jim Hague <jim.hague@acm.org>
parents:
734
diff
changeset
|
187 # Because we're going through Markdown, character entities must be |
68f926e61d16
Revise Markdown handling of character entities.
Jim Hague <jim.hague@acm.org>
parents:
734
diff
changeset
|
188 # HTML. Pandoc will convert them to UTF-8. |
726
833e6185b6a2
Add fulltitle to template fields.
Jim Hague <jim.hague@acm.org>
parents:
725
diff
changeset
|
189 def getTitleLink(m): |
592
9986c67edf91
Implement custom markdown <foo.abc> to reference tune.
Jim Hague <jim.hague@acm.org>
parents:
591
diff
changeset
|
190 fname = m.group(1) + ".abc" |
593
82e818c41e81
Give ABC file directory when expanding markdown.
Jim Hague <jim.hague@acm.org>
parents:
592
diff
changeset
|
191 path = pathlib.Path(dir, fname) |
82e818c41e81
Give ABC file directory when expanding markdown.
Jim Hague <jim.hague@acm.org>
parents:
592
diff
changeset
|
192 with path.open() as f: |
728
9b562923ac71
Fix full title text in markdown - getFullTitle() needs a list of lines.
Jim Hague <jim.hague@acm.org>
parents:
727
diff
changeset
|
193 lines = f.readlines() |
735
68f926e61d16
Revise Markdown handling of character entities.
Jim Hague <jim.hague@acm.org>
parents:
734
diff
changeset
|
194 return "[" + getFullTitle(lines, dir) + "](" + fname + ")" |
726
833e6185b6a2
Add fulltitle to template fields.
Jim Hague <jim.hague@acm.org>
parents:
725
diff
changeset
|
195 return re.sub(r'<(.*?).abc>', getTitleLink, t) |
592
9986c67edf91
Implement custom markdown <foo.abc> to reference tune.
Jim Hague <jim.hague@acm.org>
parents:
591
diff
changeset
|
196 |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
197 # Return the raw text for a given field. Optionally the nth field is taken, |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
198 # or the field data must start with a designated string to be recognised. |
726
833e6185b6a2
Add fulltitle to template fields.
Jim Hague <jim.hague@acm.org>
parents:
725
diff
changeset
|
199 def getFieldText(lines, field, n = 1, starts = None): |
588
afc031477784
Replace sed substitution with Python templating for HTML and LaTeX output.
Jim Hague <jim.hague@acm.org>
parents:
586
diff
changeset
|
200 res = "" |
726
833e6185b6a2
Add fulltitle to template fields.
Jim Hague <jim.hague@acm.org>
parents:
725
diff
changeset
|
201 for line in lines: |
110
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
202 line = line.strip() |
581
760d0ae5acea
Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents:
326
diff
changeset
|
203 if len(line) > 2 and line[1] == ':': |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
204 if line[0] == "+" or (line[0] == field and line[2] == "+"): |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
205 if not res: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
206 continue |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
207 if line[0] == "+": |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
208 line = line[2:] |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
209 else: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
210 line = line[3:] |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
211 res = res + '\n' + line.strip() |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
212 else: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
213 if res: |
581
760d0ae5acea
Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents:
326
diff
changeset
|
214 break |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
215 if line[0] == field: |
581
760d0ae5acea
Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents:
326
diff
changeset
|
216 line = line[2:].strip() |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
217 if starts: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
218 if line.find(starts) != 0: |
581
760d0ae5acea
Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents:
326
diff
changeset
|
219 continue |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
220 line = line[len(starts):].strip() |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
221 if n > 1: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
222 n = n - 1 |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
223 continue |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
224 res = line |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
225 return res |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
226 |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
227 # Return display text for a given field. |
726
833e6185b6a2
Add fulltitle to template fields.
Jim Hague <jim.hague@acm.org>
parents:
725
diff
changeset
|
228 def getFieldDisplayText(lines, dir, field, n = 1, starts = None, latex = False): |
833e6185b6a2
Add fulltitle to template fields.
Jim Hague <jim.hague@acm.org>
parents:
725
diff
changeset
|
229 res = getFieldText(lines, field, n, starts) |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
230 if res: |
735
68f926e61d16
Revise Markdown handling of character entities.
Jim Hague <jim.hague@acm.org>
parents:
734
diff
changeset
|
231 # Fields that go through Markdown must have HTML entities. |
68f926e61d16
Revise Markdown handling of character entities.
Jim Hague <jim.hague@acm.org>
parents:
734
diff
changeset
|
232 mdfield = field.upper() in ['H', 'N']; |
68f926e61d16
Revise Markdown handling of character entities.
Jim Hague <jim.hague@acm.org>
parents:
734
diff
changeset
|
233 res = convertAccents(res, False if mdfield else latex) |
846
6bd946700312
Only the first title should be converted to from sort to display order.
Jim Hague <jim.hague@acm.org>
parents:
735
diff
changeset
|
234 if field.upper() == "T" and n == 1: |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
235 res = convertTitleToDisplay(res) |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
236 elif field.upper() == "K": |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
237 res = convertKeyToDisplay(res) |
735
68f926e61d16
Revise Markdown handling of character entities.
Jim Hague <jim.hague@acm.org>
parents:
734
diff
changeset
|
238 elif mdfield: |
68f926e61d16
Revise Markdown handling of character entities.
Jim Hague <jim.hague@acm.org>
parents:
734
diff
changeset
|
239 res = convertMarkdown(expandCustomMarkdown(res, dir), latex) |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
240 return res |
110
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
241 |
726
833e6185b6a2
Add fulltitle to template fields.
Jim Hague <jim.hague@acm.org>
parents:
725
diff
changeset
|
242 # Return full title (title + [" (" + subtitle + ")"] if subtitle exists). |
833e6185b6a2
Add fulltitle to template fields.
Jim Hague <jim.hague@acm.org>
parents:
725
diff
changeset
|
243 def getFullTitle(lines, dir, starts = None, latex = False): |
833e6185b6a2
Add fulltitle to template fields.
Jim Hague <jim.hague@acm.org>
parents:
725
diff
changeset
|
244 title = getFieldDisplayText(lines, dir, "T", starts=starts, latex=latex) |
833e6185b6a2
Add fulltitle to template fields.
Jim Hague <jim.hague@acm.org>
parents:
725
diff
changeset
|
245 subtitle = getFieldDisplayText(lines, dir, "T", n=2, starts=starts, latex=latex) |
833e6185b6a2
Add fulltitle to template fields.
Jim Hague <jim.hague@acm.org>
parents:
725
diff
changeset
|
246 return title if len(subtitle) == 0 else title + " (" + subtitle + ")" |
833e6185b6a2
Add fulltitle to template fields.
Jim Hague <jim.hague@acm.org>
parents:
725
diff
changeset
|
247 |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
248 if __name__ == "__main__": |
733
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
249 def process(f, dir, args): |
726
833e6185b6a2
Add fulltitle to template fields.
Jim Hague <jim.hague@acm.org>
parents:
725
diff
changeset
|
250 lines = f.readlines() |
733
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
251 if args.display: |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
252 if args.field.upper() == "FT": |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
253 line = getFullTitle(lines, dir, args.starts, args.latex) |
727
772402f5f8ea
Provide full titles for next and previous tunes in web pages.
Jim Hague <jim.hague@acm.org>
parents:
726
diff
changeset
|
254 else: |
733
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
255 line = getFieldDisplayText(lines, dir, args.field, args.index, args.starts, args.latex) |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
256 else: |
733
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
257 if args.field.upper() == "FT": |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
258 args.field = "T" |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
259 line = getFieldText(lines, args.field, args.index, args.starts) |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
260 if line: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
261 print(line) |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
262 return True |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
263 else: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
264 return False |
110
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
265 |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
266 # execute only if run as a script |
733
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
267 parser = argparse.ArgumentParser(description="Extract field data from ABC file.") |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
268 parser.add_argument("-f", "--field", dest="field", default="T", |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
269 help=("extract the given field [default: %(default)s]. " |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
270 "Field FT is special; it returns the full title " |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
271 "- the title followed by subtitle in () if " |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
272 "present - for display text, or just the title " |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
273 "for non-display text.")) |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
274 parser.add_argument("-l", "--latex", dest="latex", |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
275 action="store_true", default=False, |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
276 help="convert special characters for LaTeX (default HTML)") |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
277 parser.add_argument("-d", "--display", dest="display", |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
278 action="store_true", default=False, |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
279 help=("convert to display text. Convert accents to " |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
280 "LaTeX or HTML, in titles convert 'Tune, The' to " |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
281 "'The Tune', convert keys to full key name, " |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
282 "and expand Markdown in notes and history.")) |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
283 parser.add_argument("-n", "--index", dest="index", |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
284 action="store", type=int, default=1, |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
285 help="report INDEXth value [default: %(default)s]") |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
286 parser.add_argument("-s", "--starts", dest="starts", |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
287 action="store", default=None, |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
288 help=("report only if line starts with CONTENT " |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
289 "and remove CONTENT"), |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
290 metavar="CONTENT") |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
291 parser.add_argument('input', type=argparse.FileType('r'), |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
292 help='input ABC file') |
841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
Jim Hague <jim.hague@acm.org>
parents:
728
diff
changeset
|
293 args = parser.parse_args() |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
294 |
734
e896cf93fe98
Remove vestiges of support for stdin input to abcfield.py.
Jim Hague <jim.hague@acm.org>
parents:
733
diff
changeset
|
295 path = pathlib.Path(args.input.name) |
e896cf93fe98
Remove vestiges of support for stdin input to abcfield.py.
Jim Hague <jim.hague@acm.org>
parents:
733
diff
changeset
|
296 with path.open() as f: |
e896cf93fe98
Remove vestiges of support for stdin input to abcfield.py.
Jim Hague <jim.hague@acm.org>
parents:
733
diff
changeset
|
297 res = process(f, path.parent, args) |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
298 sys.exit(int(not res)) |