Mercurial > dottes
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" : ("Á", "\\'{A}"), | |
| 13 "'E" : ("É", "\\'{E}"), | |
| 14 "'I" : ("Í", "\\'{I}"), | |
| 15 "'O" : ("Ó", "\\'{O}"), | |
| 16 "'U" : ("Ú", "\\'{U}"), | |
| 17 "'Y" : ("Ý", "\\'{Y}"), | |
| 18 "'a" : ("á", "\\'{a}"), | |
| 19 "'e" : ("é", "\\'{e}"), | |
| 20 "'i" : ("í", "\\'{i}"), | |
| 21 "'o" : ("ó", "\\'{o}"), | |
| 22 "'u" : ("ú", "\\'{u}"), | |
| 23 "'y" : ("ý", "\\'{y}"), | |
| 24 | |
| 25 # Grave accents | |
| 26 "`A" : ("À", "\\`{A}"), | |
| 27 "`E" : ("È", "\\`{E}"), | |
| 28 "`I" : ("Ì", "\\`{I}"), | |
| 29 "`O" : ("Ò", "\\`{O}"), | |
| 30 "`U" : ("Ù", "\\`{U}"), | |
| 31 "`a" : ("à", "\\`{a}"), | |
| 32 "`e" : ("è", "\\`{e}"), | |
| 33 "`i" : ("ì", "\\`{i}"), | |
| 34 "`o" : ("ò", "\\`{o}"), | |
| 35 "`u" : ("ù", "\\`{u}"), | |
| 36 | |
| 37 # Umlauts | |
| 38 "\"A" : ("Ä", "\\\"{A}"), | |
| 39 "\"E" : ("Ë", "\\\"{E}"), | |
| 40 "\"I" : ("Ï", "\\\"{I}"), | |
| 41 "\"O" : ("Ö", "\\\"{O}"), | |
| 42 "\"U" : ("Ü", "\\\"{U}"), | |
| 43 "\"Y" : ("Ÿ", "\\\"{Y}"), | |
| 44 "\"a" : ("ä", "\\\"{a}"), | |
| 45 "\"e" : ("ë", "\\\"{e}"), | |
| 46 "\"i" : ("ï", "\\\"{\i}"), | |
| 47 "\"o" : ("ö", "\\\"{o}"), | |
| 48 "\"u" : ("ü", "\\\"{u}"), | |
| 49 "\"y" : ("ÿ", "\\\"{y}"), | |
| 50 | |
| 51 # Circumflexes | |
| 52 "^A" : ("Â", "\\^{A}"), | |
| 53 "^E" : ("Ê", "\\^{E}"), | |
| 54 "^I" : ("Î", "\\^{I}"), | |
| 55 "^O" : ("Ô", "\\^{O}"), | |
| 56 "^U" : ("Û", "\\^{U}"), | |
| 57 "^a" : ("â", "\\^{a}"), | |
| 58 "^e" : ("ê", "\\^{e}"), | |
| 59 "^i" : ("î", "\\^{\i}"), | |
| 60 "^o" : ("ô", "\\^{o}"), | |
| 61 "^u" : ("û", "\\^{u}"), | |
| 62 | |
| 63 # Tilde | |
| 64 "~A" : ("Ã", "\\~{A}"), | |
| 65 "~N" : ("Ñ", "\\~{N}"), | |
| 66 "~O" : ("Õ", "\\~{O}"), | |
| 67 "~a" : ("ã", "\\~{a}"), | |
| 68 "~n" : ("ñ", "\\~{n}"), | |
| 69 "~o" : ("õ", "\\~{o}"), | |
| 70 | |
| 71 # Cedilla | |
| 72 ",C" : ("Ç", "\\c{C}"), | |
| 73 ",c" : ("ç", "\\c{c}"), | |
| 74 | |
| 75 # Slash | |
| 76 "/O" : ("Ø", "\\O"), | |
| 77 "/o" : ("ø", "\\o"), | |
| 78 | |
| 79 # Ring | |
| 80 "AA" : ("Å", "\\r{A}"), | |
| 81 "aa" : ("å", "\\r{a}"), | |
| 82 | |
| 83 # Ligatures | |
| 84 "AE" : ("Æ", "\\AE"), | |
| 85 "ae" : ("æ", "\\ae"), | |
| 86 "ss" : ("ß", "\\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) |
