annotate abcfield.py @ 605:3f8a76acf7ad

Added tag build-default-243 for changeset 3b8a457ea3c9
author Jenkins Build Manager <jenkins@cryhavoc.org.uk>
date Fri, 04 Nov 2016 18:52:47 +0000
parents 82e818c41e81
children 972d3dab1142
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
586
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
1 #!/usr/bin/env python3
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2 #
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
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" : ("&Aacute;", "\\'{A}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
29 "'E" : ("&Eacute;", "\\'{E}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
30 "'I" : ("&Iacute;", "\\'{I}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
31 "'O" : ("&Oacute;", "\\'{O}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
32 "'U" : ("&Uacute;", "\\'{U}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
33 "'Y" : ("&Yacute;", "\\'{Y}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
34 "'a" : ("&aacute;", "\\'{a}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
35 "'e" : ("&eacute;", "\\'{e}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
36 "'i" : ("&iacute;", "\\'{i}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
37 "'o" : ("&oacute;", "\\'{o}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
38 "'u" : ("&uacute;", "\\'{u}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
39 "'y" : ("&yacute;", "\\'{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" : ("&Agrave;", "\\`{A}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
43 "`E" : ("&Egrave;", "\\`{E}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
44 "`I" : ("&Igrave;", "\\`{I}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
45 "`O" : ("&Ograve;", "\\`{O}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
46 "`U" : ("&Ugrave;", "\\`{U}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
47 "`a" : ("&agrave;", "\\`{a}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
48 "`e" : ("&egrave;", "\\`{e}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
49 "`i" : ("&igrave;", "\\`{i}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
50 "`o" : ("&ograve;", "\\`{o}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
51 "`u" : ("&ugrave;", "\\`{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" : ("&Auml;", "\\\"{A}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
55 "\"E" : ("&Euml;", "\\\"{E}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
56 "\"I" : ("&Iuml;", "\\\"{I}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
57 "\"O" : ("&Ouml;", "\\\"{O}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
58 "\"U" : ("&Uuml;", "\\\"{U}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
59 "\"Y" : ("&Yuml;", "\\\"{Y}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
60 "\"a" : ("&auml;", "\\\"{a}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
61 "\"e" : ("&euml;", "\\\"{e}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
62 "\"i" : ("&iuml;", "\\\"{\i}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
63 "\"o" : ("&ouml;", "\\\"{o}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
64 "\"u" : ("&uuml;", "\\\"{u}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
65 "\"y" : ("&yuml;", "\\\"{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" : ("&Acirc;", "\\^{A}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
69 "^E" : ("&Ecirc;", "\\^{E}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
70 "^I" : ("&Icirc;", "\\^{I}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
71 "^O" : ("&Ocirc;", "\\^{O}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
72 "^U" : ("&Ucirc;", "\\^{U}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
73 "^a" : ("&acirc;", "\\^{a}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
74 "^e" : ("&ecirc;", "\\^{e}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
75 "^i" : ("&icirc;", "\\^{\i}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
76 "^o" : ("&ocirc;", "\\^{o}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
77 "^u" : ("&ucirc;", "\\^{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" : ("&Atilde;", "\\~{A}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
81 "~N" : ("&Ntilde;", "\\~{N}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
82 "~O" : ("&Otilde;", "\\~{O}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
83 "~a" : ("&atilde;", "\\~{a}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
84 "~n" : ("&ntilde;", "\\~{n}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
85 "~o" : ("&otilde;", "\\~{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" : ("&Ccedil;", "\\c{C}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
89 ",c" : ("&ccedil;", "\\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" : ("&Oslash;", "\\O"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
93 "/o" : ("&oslash;", "\\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" : ("&Aring;", "\\r{A}"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
97 "aa" : ("&aring;", "\\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" : ("&AElig;", "\\AE"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
101 "ae" : ("&aelig;", "\\ae"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
102 "ss" : ("&szlig;", "\\ss"),
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
103 }
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
104
586
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
105 abckeys = {
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
106 "m": "Minor",
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
107 "min": "Minor",
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
108 "mix": "Mixolydian",
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
109 "dor": "Dorian",
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
110 "phr": "Phrygian",
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
111 "lyd": "Lydian",
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
112 "loc": "Locrian",
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
113 }
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
114
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
115 # 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
116 def convertAccents(t, latex=False):
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
117 res = ""
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
118 while True:
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
119 p = t.partition('\\')
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
120 res += p[0]
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
121 if p[1] == "":
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
122 break
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
123 abc = p[2][0:2]
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
124 t = p[2][2:]
581
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
125 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
126 if latex:
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
127 res += accentedletters[abc][1]
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
128 else:
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
129 res += accentedletters[abc][0]
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
130 else:
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
131 res += "\\" + abc
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
132 return res
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
133
586
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
134 # 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
135 def convertTitleToDisplay(t):
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
136 p = t.rpartition(',')
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
137 if p[1] == "":
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
138 return t
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
139 else:
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
140 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
141
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
142 # 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
143 def convertKeyToDisplay(t):
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
144 letter = t[0].upper()
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
145 accidental = ""
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
146 mode = ""
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
147 try:
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
148 accidental = t[1]
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
149 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
150 mode = t[2:]
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
151 else:
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
152 accidental = ""
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
153 mode = t[1:]
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
154 except IndexError:
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
155 pass
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
156 mode = mode.strip().lower()
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
157 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
158
591
2face6618bf3 Convert Markdown in N and H fields. Fix up .abc links.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
159 # 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
160 # 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
161 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
162 if latex:
2face6618bf3 Convert Markdown in N and H fields. Fix up .abc links.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
163 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
164 else:
2face6618bf3 Convert Markdown in N and H fields. Fix up .abc links.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
165 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
166 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
167 if latex:
592
9986c67edf91 Implement custom markdown <foo.abc> to reference tune.
Jim Hague <jim.hague@acm.org>
parents: 591
diff changeset
168 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
169 else:
592
9986c67edf91 Implement custom markdown <foo.abc> to reference tune.
Jim Hague <jim.hague@acm.org>
parents: 591
diff changeset
170 res = re.sub(r'href="(.*?).abc"', r'href="\1.html"', res)
593
82e818c41e81 Give ABC file directory when expanding markdown.
Jim Hague <jim.hague@acm.org>
parents: 592
diff changeset
171 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
172
592
9986c67edf91 Implement custom markdown <foo.abc> to reference tune.
Jim Hague <jim.hague@acm.org>
parents: 591
diff changeset
173 # 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
174 # <foo.abc> will expand to ['title of foo'](foo.abc).
593
82e818c41e81 Give ABC file directory when expanding markdown.
Jim Hague <jim.hague@acm.org>
parents: 592
diff changeset
175 def expandCustomMarkdown(t, dir, latex):
592
9986c67edf91 Implement custom markdown <foo.abc> to reference tune.
Jim Hague <jim.hague@acm.org>
parents: 591
diff changeset
176 # Given a match to (foo.abc), return a markdown link to the tune with the
9986c67edf91 Implement custom markdown <foo.abc> to reference tune.
Jim Hague <jim.hague@acm.org>
parents: 591
diff changeset
177 # title of the tune as the text of the link.
9986c67edf91 Implement custom markdown <foo.abc> to reference tune.
Jim Hague <jim.hague@acm.org>
parents: 591
diff changeset
178 def getTitle(m):
9986c67edf91 Implement custom markdown <foo.abc> to reference tune.
Jim Hague <jim.hague@acm.org>
parents: 591
diff changeset
179 fname = m.group(1) + ".abc"
593
82e818c41e81 Give ABC file directory when expanding markdown.
Jim Hague <jim.hague@acm.org>
parents: 592
diff changeset
180 path = pathlib.Path(dir, fname)
82e818c41e81 Give ABC file directory when expanding markdown.
Jim Hague <jim.hague@acm.org>
parents: 592
diff changeset
181 with path.open() as f:
82e818c41e81 Give ABC file directory when expanding markdown.
Jim Hague <jim.hague@acm.org>
parents: 592
diff changeset
182 return "[" + getFieldDisplayText(f, dir, "T", latex) + "](" + fname + ")"
592
9986c67edf91 Implement custom markdown <foo.abc> to reference tune.
Jim Hague <jim.hague@acm.org>
parents: 591
diff changeset
183 return re.sub(r'<(.*?).abc>', getTitle, t)
9986c67edf91 Implement custom markdown <foo.abc> to reference tune.
Jim Hague <jim.hague@acm.org>
parents: 591
diff changeset
184
586
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
185 # 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
186 # 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
187 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
188 res = ""
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
189 for line in inf:
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
190 line = line.strip()
581
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
191 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
192 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
193 if not res:
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
194 continue
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
195 if line[0] == "+":
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
196 line = line[2:]
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
197 else:
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
198 line = line[3:]
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
199 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
200 else:
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
201 if res:
581
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
202 break
586
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
203 if line[0] == field:
581
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
204 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
205 if starts:
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
206 if line.find(starts) != 0:
581
760d0ae5acea Revise abcfield.py to recognise continuation fields.
Jim Hague <jim.hague@acm.org>
parents: 326
diff changeset
207 continue
586
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
208 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
209 if n > 1:
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
210 n = n - 1
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
211 continue
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
212 res = line
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
213 return res
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
214
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
215 # Return display text for a given field.
593
82e818c41e81 Give ABC file directory when expanding markdown.
Jim Hague <jim.hague@acm.org>
parents: 592
diff changeset
216 def getFieldDisplayText(inf, dir, field, n = 1, starts = None, latex = False):
586
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
217 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
218 if res:
591
2face6618bf3 Convert Markdown in N and H fields. Fix up .abc links.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
219 res = convertAccents(res, latex)
586
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
220 if field.upper() == "T":
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
221 res = convertTitleToDisplay(res)
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
222 elif field.upper() == "K":
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
223 res = convertKeyToDisplay(res)
591
2face6618bf3 Convert Markdown in N and H fields. Fix up .abc links.
Jim Hague <jim.hague@acm.org>
parents: 588
diff changeset
224 elif field.upper() in ["H", "N"]:
593
82e818c41e81 Give ABC file directory when expanding markdown.
Jim Hague <jim.hague@acm.org>
parents: 592
diff changeset
225 res = convertMarkdown(expandCustomMarkdown(res, dir, latex), latex)
586
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
226 return res
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
227
586
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
228 if __name__ == "__main__":
593
82e818c41e81 Give ABC file directory when expanding markdown.
Jim Hague <jim.hague@acm.org>
parents: 592
diff changeset
229 def process(inf, dir, options):
586
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
230 if options.display:
593
82e818c41e81 Give ABC file directory when expanding markdown.
Jim Hague <jim.hague@acm.org>
parents: 592
diff changeset
231 line = getFieldDisplayText(inf, dir, options.field, options.index, options.starts, options.latex)
586
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
232 else:
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
233 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
234 if line:
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
235 print(line)
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
236 return True
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
237 else:
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
238 return False
110
a63e39fc3410 Add basic diacritic handling for LaTeX and web.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
239
586
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
240 # 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
241 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
242 " 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
243 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
244 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
245 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
246 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
247 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
248 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
249 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
250 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
251 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
252 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
253 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
254 metavar="INDEX")
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
255 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
256 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
257 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
258 metavar="CONTENT")
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
259 (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
260
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
261 res = False
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
262 if len(args) > 0:
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
263 for arg in args:
593
82e818c41e81 Give ABC file directory when expanding markdown.
Jim Hague <jim.hague@acm.org>
parents: 592
diff changeset
264 path = pathlib.Path(arg)
82e818c41e81 Give ABC file directory when expanding markdown.
Jim Hague <jim.hague@acm.org>
parents: 592
diff changeset
265 with path.open() as f:
82e818c41e81 Give ABC file directory when expanding markdown.
Jim Hague <jim.hague@acm.org>
parents: 592
diff changeset
266 res = res or process(f, path.parent, options)
586
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
267 else:
593
82e818c41e81 Give ABC file directory when expanding markdown.
Jim Hague <jim.hague@acm.org>
parents: 592
diff changeset
268 res = process(sys.stdin, ".", options)
586
daa3b76bd11f More abcfield.py updates and mark it Python 3.
Jim Hague <jim.hague@acm.org>
parents: 584
diff changeset
269 sys.exit(int(not res))