comparison abcfield.py @ 122:295ba8275ab4

Make output larger where possible. This is done by a) reducing the page margins b) compressing the heading and removing all but Notes: fields. The latter produced vertically smaller tune PDFs that will scale wider. In the process, modify abctitle.py to extract the first of any header field, and move formatting for a single tune into a .fmt file.
author Jim Hague <jim.hague@laicatc.com>
date Sat, 21 Apr 2012 19:47:30 +0100
parents abctitle.py@a63e39fc3410
children eedf65564226
comparison
equal deleted inserted replaced
121:586631d3b9be 122:295ba8275ab4
1 #!/usr/bin/env python
2 #
3 # Extact a text field (title, by default) from a .abc file, and print it out
4 # formatted for use in LaTeX or HTML.
5 #
6
7 import optparse
8 import sys
9
10 accentedletters = {
11 # Acute accents
12 "'A" : ("&Aacute;", "\\'{A}"),
13 "'E" : ("&Eacute;", "\\'{E}"),
14 "'I" : ("&Iacute;", "\\'{I}"),
15 "'O" : ("&Oacute;", "\\'{O}"),
16 "'U" : ("&Uacute;", "\\'{U}"),
17 "'Y" : ("&Yacute;", "\\'{Y}"),
18 "'a" : ("&aacute;", "\\'{a}"),
19 "'e" : ("&eacute;", "\\'{e}"),
20 "'i" : ("&iacute;", "\\'{i}"),
21 "'o" : ("&oacute;", "\\'{o}"),
22 "'u" : ("&uacute;", "\\'{u}"),
23 "'y" : ("&yacute;", "\\'{y}"),
24
25 # Grave accents
26 "`A" : ("&Agrave;", "\\`{A}"),
27 "`E" : ("&Egrave;", "\\`{E}"),
28 "`I" : ("&Igrave;", "\\`{I}"),
29 "`O" : ("&Ograve;", "\\`{O}"),
30 "`U" : ("&Ugrave;", "\\`{U}"),
31 "`a" : ("&agrave;", "\\`{a}"),
32 "`e" : ("&egrave;", "\\`{e}"),
33 "`i" : ("&igrave;", "\\`{i}"),
34 "`o" : ("&ograve;", "\\`{o}"),
35 "`u" : ("&ugrave;", "\\`{u}"),
36
37 # Umlauts
38 "\"A" : ("&Auml;", "\\\"{A}"),
39 "\"E" : ("&Euml;", "\\\"{E}"),
40 "\"I" : ("&Iuml;", "\\\"{I}"),
41 "\"O" : ("&Ouml;", "\\\"{O}"),
42 "\"U" : ("&Uuml;", "\\\"{U}"),
43 "\"Y" : ("&Yuml;", "\\\"{Y}"),
44 "\"a" : ("&auml;", "\\\"{a}"),
45 "\"e" : ("&euml;", "\\\"{e}"),
46 "\"i" : ("&iuml;", "\\\"{\i}"),
47 "\"o" : ("&ouml;", "\\\"{o}"),
48 "\"u" : ("&uuml;", "\\\"{u}"),
49 "\"y" : ("&yuml;", "\\\"{y}"),
50
51 # Circumflexes
52 "^A" : ("&Acirc;", "\\^{A}"),
53 "^E" : ("&Ecirc;", "\\^{E}"),
54 "^I" : ("&Icirc;", "\\^{I}"),
55 "^O" : ("&Ocirc;", "\\^{O}"),
56 "^U" : ("&Ucirc;", "\\^{U}"),
57 "^a" : ("&acirc;", "\\^{a}"),
58 "^e" : ("&ecirc;", "\\^{e}"),
59 "^i" : ("&icirc;", "\\^{\i}"),
60 "^o" : ("&ocirc;", "\\^{o}"),
61 "^u" : ("&ucirc;", "\\^{u}"),
62
63 # Tilde
64 "~A" : ("&Atilde;", "\\~{A}"),
65 "~N" : ("&Ntilde;", "\\~{N}"),
66 "~O" : ("&Otilde;", "\\~{O}"),
67 "~a" : ("&atilde;", "\\~{a}"),
68 "~n" : ("&ntilde;", "\\~{n}"),
69 "~o" : ("&otilde;", "\\~{o}"),
70
71 # Cedilla
72 ",C" : ("&Ccedil;", "\\c{C}"),
73 ",c" : ("&ccedil;", "\\c{c}"),
74
75 # Slash
76 "/O" : ("&Oslash;", "\\O"),
77 "/o" : ("&oslash;", "\\o"),
78
79 # Ring
80 "AA" : ("&Aring;", "\\r{A}"),
81 "aa" : ("&aring;", "\\r{a}"),
82
83 # Ligatures
84 "AE" : ("&AElig;", "\\AE"),
85 "ae" : ("&aelig;", "\\ae"),
86 "ss" : ("&szlig;", "\\ss"),
87 }
88
89 def convertTitle(t, options):
90 res = ""
91 while True:
92 p = t.partition('\\')
93 res += p[0]
94 if p[1] == "":
95 break
96 abc = p[2][0:2]
97 t = p[2][2:]
98 if (options.html or options.latex) and abc in accentedletters:
99 if options.html:
100 res += accentedletters[abc][0]
101 else:
102 res += accentedletters[abc][1]
103 else:
104 res += "\\" + abc
105 return res
106
107 def process(inf, options):
108 for line in inf:
109 line = line.strip()
110 if len(line) > 2 and line[0] == options.field and line[1] == ':':
111 print(convertTitle(line[2:].strip(), options))
112 break
113
114 parser = optparse.OptionParser(usage="usage: %prog [options] [filename]\n\n"
115 " Extract field data from ABC file.")
116 parser.add_option("-f", "--field", dest="field", default="T",
117 help="extract the field FIELD", metavar="FIELD")
118 parser.add_option("-m", "--html", dest="html",
119 action="store_true", default=False,
120 help="format output for HTML")
121 parser.add_option("-l", "--latex", dest="latex",
122 action="store_true", default=False,
123 help="format ouput for LaTeX")
124 (options, args) = parser.parse_args()
125
126 if options.html and options.latex:
127 sys.exit("You must choose one of HTML or LaTeX output")
128
129 if len(args) > 0:
130 for arg in args:
131 try:
132 inf = open(arg, "r")
133 process(inf, options)
134 finally:
135 inf.close()
136 else:
137 process(sys.stdin, options)
138 sys.exit(0)