Mercurial > dottes
annotate abcfield.py @ 588:afc031477784
Replace sed substitution with Python templating for HTML and LaTeX output.
author | Jim Hague <jim.hague@acm.org> |
---|---|
date | Wed, 02 Nov 2016 00:21:18 +0000 |
parents | daa3b76bd11f |
children | 2face6618bf3 |
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 |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
20 import optparse |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
21 import sys |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
22 |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
23 accentedletters = { |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
24 # Acute accents |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
25 "'A" : ("Á", "\\'{A}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
26 "'E" : ("É", "\\'{E}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
27 "'I" : ("Í", "\\'{I}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
28 "'O" : ("Ó", "\\'{O}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
29 "'U" : ("Ú", "\\'{U}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
30 "'Y" : ("Ý", "\\'{Y}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
31 "'a" : ("á", "\\'{a}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
32 "'e" : ("é", "\\'{e}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
33 "'i" : ("í", "\\'{i}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
34 "'o" : ("ó", "\\'{o}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
35 "'u" : ("ú", "\\'{u}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
36 "'y" : ("ý", "\\'{y}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
37 |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
38 # Grave accents |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
39 "`A" : ("À", "\\`{A}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
40 "`E" : ("È", "\\`{E}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
41 "`I" : ("Ì", "\\`{I}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
42 "`O" : ("Ò", "\\`{O}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
43 "`U" : ("Ù", "\\`{U}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
44 "`a" : ("à", "\\`{a}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
45 "`e" : ("è", "\\`{e}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
46 "`i" : ("ì", "\\`{i}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
47 "`o" : ("ò", "\\`{o}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
48 "`u" : ("ù", "\\`{u}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
49 |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
50 # Umlauts |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
51 "\"A" : ("Ä", "\\\"{A}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
52 "\"E" : ("Ë", "\\\"{E}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
53 "\"I" : ("Ï", "\\\"{I}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
54 "\"O" : ("Ö", "\\\"{O}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
55 "\"U" : ("Ü", "\\\"{U}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
56 "\"Y" : ("Ÿ", "\\\"{Y}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
57 "\"a" : ("ä", "\\\"{a}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
58 "\"e" : ("ë", "\\\"{e}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
59 "\"i" : ("ï", "\\\"{\i}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
60 "\"o" : ("ö", "\\\"{o}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
61 "\"u" : ("ü", "\\\"{u}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
62 "\"y" : ("ÿ", "\\\"{y}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
63 |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
64 # Circumflexes |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
65 "^A" : ("Â", "\\^{A}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
66 "^E" : ("Ê", "\\^{E}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
67 "^I" : ("Î", "\\^{I}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
68 "^O" : ("Ô", "\\^{O}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
69 "^U" : ("Û", "\\^{U}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
70 "^a" : ("â", "\\^{a}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
71 "^e" : ("ê", "\\^{e}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
72 "^i" : ("î", "\\^{\i}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
73 "^o" : ("ô", "\\^{o}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
74 "^u" : ("û", "\\^{u}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
75 |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
76 # Tilde |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
77 "~A" : ("Ã", "\\~{A}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
78 "~N" : ("Ñ", "\\~{N}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
79 "~O" : ("Õ", "\\~{O}"), |
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 |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
84 # Cedilla |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
85 ",C" : ("Ç", "\\c{C}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
86 ",c" : ("ç", "\\c{c}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
87 |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
88 # Slash |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
89 "/O" : ("Ø", "\\O"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
90 "/o" : ("ø", "\\o"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
91 |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
92 # Ring |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
93 "AA" : ("Å", "\\r{A}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
94 "aa" : ("å", "\\r{a}"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
95 |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
96 # Ligatures |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
97 "AE" : ("Æ", "\\AE"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
98 "ae" : ("æ", "\\ae"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
99 "ss" : ("ß", "\\ss"), |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
100 } |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
101 |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
102 abckeys = { |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
103 "m": "Minor", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
104 "min": "Minor", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
105 "mix": "Mixolydian", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
106 "dor": "Dorian", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
107 "phr": "Phrygian", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
108 "lyd": "Lydian", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
109 "loc": "Locrian", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
110 } |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
111 |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
112 # 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
|
113 def convertAccents(t, latex=False): |
110
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
114 res = "" |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
115 while True: |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
116 p = t.partition('\\') |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
117 res += p[0] |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
118 if p[1] == "": |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
119 break |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
120 abc = p[2][0:2] |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
121 t = p[2][2:] |
581
760d0ae5acea
Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents:
326
diff
changeset
|
122 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
|
123 if latex: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
124 res += accentedletters[abc][1] |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
125 else: |
110
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
126 res += accentedletters[abc][0] |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
127 else: |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
128 res += "\\" + abc |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
129 return res |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
130 |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
131 # Convert Title fields from sort to display, so Bat, The->The Bat. |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
132 def convertTitleToDisplay(t): |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
133 p = t.rpartition(',') |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
134 if p[1] == "": |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
135 return t |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
136 else: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
137 return p[2].strip() + " " + p[0].strip() |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
138 |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
139 # 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
|
140 def convertKeyToDisplay(t): |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
141 letter = t[0].upper() |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
142 accidental = "" |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
143 mode = "" |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
144 try: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
145 accidental = t[1] |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
146 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
|
147 mode = t[2:] |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
148 else: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
149 accidental = "" |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
150 mode = t[1:] |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
151 except IndexError: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
152 pass |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
153 mode = mode.strip().lower() |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
154 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
|
155 |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
156 # 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
|
157 # or the field data must start with a designated string to be recognised. |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
158 def getFieldText(inf, 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
|
159 res = "" |
110
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
160 for line in inf: |
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
161 line = line.strip() |
581
760d0ae5acea
Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents:
326
diff
changeset
|
162 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
|
163 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
|
164 if not res: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
165 continue |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
166 if line[0] == "+": |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
167 line = line[2:] |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
168 else: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
169 line = line[3:] |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
170 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
|
171 else: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
172 if res: |
581
760d0ae5acea
Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents:
326
diff
changeset
|
173 break |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
174 if line[0] == field: |
581
760d0ae5acea
Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents:
326
diff
changeset
|
175 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
|
176 if starts: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
177 if line.find(starts) != 0: |
581
760d0ae5acea
Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents:
326
diff
changeset
|
178 continue |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
179 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
|
180 if n > 1: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
181 n = n - 1 |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
182 continue |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
183 res = line |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
184 return res |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
185 |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
186 # Return display text for a given field. |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
187 def getFieldDisplayText(inf, field, n = 1, starts = None, latex = False): |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
188 res = getFieldText(inf, field, n, starts) |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
189 if res: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
190 if field.upper() == "T": |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
191 res = convertTitleToDisplay(res) |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
192 elif field.upper() == "K": |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
193 res = convertKeyToDisplay(res) |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
194 res = convertAccents(res, latex) |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
195 return res |
110
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
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 if __name__ == "__main__": |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
198 def process(inf, options): |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
199 if options.display: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
200 line = getFieldDisplayText(inf, options.field, options.index, options.starts, options.latex) |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
201 else: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
202 line = getFieldText(inf, options.field, options.index, options.starts) |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
203 if line: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
204 print(line) |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
205 return True |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
206 else: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
207 return False |
110
a63e39fc3410
Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff
changeset
|
208 |
586
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
209 # execute only if run as a script |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
210 parser = optparse.OptionParser(usage="usage: %prog [options] [filename]\n\n" |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
211 " Extract field data from ABC file.") |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
212 parser.add_option("-f", "--field", dest="field", default="T", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
213 help="extract the field FIELD", metavar="FIELD") |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
214 parser.add_option("-l", "--latex", dest="latex", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
215 action="store_true", default=False, |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
216 help="convert special characters for LaTeX") |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
217 parser.add_option("-d", "--display", dest="display", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
218 action="store_true", default=False, |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
219 help="convert to display text") |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
220 parser.add_option("-n", "--index", dest="index", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
221 action="store", type="int", default=1, |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
222 help="report INDEXth value [default: %default]", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
223 metavar="INDEX") |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
224 parser.add_option("-s", "--starts", dest="starts", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
225 action="store", type="string", default=None, |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
226 help="report only if line starts CONTENT and remove CONTENT", |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
227 metavar="CONTENT") |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
228 (options, args) = parser.parse_args() |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
229 |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
230 res = False |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
231 if len(args) > 0: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
232 for arg in args: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
233 try: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
234 inf = open(arg, "r") |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
235 res = res or process(inf, options) |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
236 finally: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
237 inf.close() |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
238 else: |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
239 res = process(sys.stdin, options) |
daa3b76bd11f
More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents:
584
diff
changeset
|
240 sys.exit(int(not res)) |