139 lines
3.9 KiB
Python
Executable File
139 lines
3.9 KiB
Python
Executable File
#!/usr/bin/env python
|
|
#
|
|
# Extact a text field (title, by default) from a .abc file, and print it out
|
|
# formatted for use in LaTeX or HTML.
|
|
#
|
|
|
|
import optparse
|
|
import sys
|
|
|
|
accentedletters = {
|
|
# Acute accents
|
|
"'A" : ("Á", "\\'{A}"),
|
|
"'E" : ("É", "\\'{E}"),
|
|
"'I" : ("Í", "\\'{I}"),
|
|
"'O" : ("Ó", "\\'{O}"),
|
|
"'U" : ("Ú", "\\'{U}"),
|
|
"'Y" : ("Ý", "\\'{Y}"),
|
|
"'a" : ("á", "\\'{a}"),
|
|
"'e" : ("é", "\\'{e}"),
|
|
"'i" : ("í", "\\'{i}"),
|
|
"'o" : ("ó", "\\'{o}"),
|
|
"'u" : ("ú", "\\'{u}"),
|
|
"'y" : ("ý", "\\'{y}"),
|
|
|
|
# Grave accents
|
|
"`A" : ("À", "\\`{A}"),
|
|
"`E" : ("È", "\\`{E}"),
|
|
"`I" : ("Ì", "\\`{I}"),
|
|
"`O" : ("Ò", "\\`{O}"),
|
|
"`U" : ("Ù", "\\`{U}"),
|
|
"`a" : ("à", "\\`{a}"),
|
|
"`e" : ("è", "\\`{e}"),
|
|
"`i" : ("ì", "\\`{i}"),
|
|
"`o" : ("ò", "\\`{o}"),
|
|
"`u" : ("ù", "\\`{u}"),
|
|
|
|
# Umlauts
|
|
"\"A" : ("Ä", "\\\"{A}"),
|
|
"\"E" : ("Ë", "\\\"{E}"),
|
|
"\"I" : ("Ï", "\\\"{I}"),
|
|
"\"O" : ("Ö", "\\\"{O}"),
|
|
"\"U" : ("Ü", "\\\"{U}"),
|
|
"\"Y" : ("Ÿ", "\\\"{Y}"),
|
|
"\"a" : ("ä", "\\\"{a}"),
|
|
"\"e" : ("ë", "\\\"{e}"),
|
|
"\"i" : ("ï", "\\\"{\i}"),
|
|
"\"o" : ("ö", "\\\"{o}"),
|
|
"\"u" : ("ü", "\\\"{u}"),
|
|
"\"y" : ("ÿ", "\\\"{y}"),
|
|
|
|
# Circumflexes
|
|
"^A" : ("Â", "\\^{A}"),
|
|
"^E" : ("Ê", "\\^{E}"),
|
|
"^I" : ("Î", "\\^{I}"),
|
|
"^O" : ("Ô", "\\^{O}"),
|
|
"^U" : ("Û", "\\^{U}"),
|
|
"^a" : ("â", "\\^{a}"),
|
|
"^e" : ("ê", "\\^{e}"),
|
|
"^i" : ("î", "\\^{\i}"),
|
|
"^o" : ("ô", "\\^{o}"),
|
|
"^u" : ("û", "\\^{u}"),
|
|
|
|
# Tilde
|
|
"~A" : ("Ã", "\\~{A}"),
|
|
"~N" : ("Ñ", "\\~{N}"),
|
|
"~O" : ("Õ", "\\~{O}"),
|
|
"~a" : ("ã", "\\~{a}"),
|
|
"~n" : ("ñ", "\\~{n}"),
|
|
"~o" : ("õ", "\\~{o}"),
|
|
|
|
# Cedilla
|
|
",C" : ("Ç", "\\c{C}"),
|
|
",c" : ("ç", "\\c{c}"),
|
|
|
|
# Slash
|
|
"/O" : ("Ø", "\\O"),
|
|
"/o" : ("ø", "\\o"),
|
|
|
|
# Ring
|
|
"AA" : ("Å", "\\r{A}"),
|
|
"aa" : ("å", "\\r{a}"),
|
|
|
|
# Ligatures
|
|
"AE" : ("Æ", "\\AE"),
|
|
"ae" : ("æ", "\\ae"),
|
|
"ss" : ("ß", "\\ss"),
|
|
}
|
|
|
|
def convertTitle(t, options):
|
|
res = ""
|
|
while True:
|
|
p = t.partition('\\')
|
|
res += p[0]
|
|
if p[1] == "":
|
|
break
|
|
abc = p[2][0:2]
|
|
t = p[2][2:]
|
|
if (options.html or options.latex) and abc in accentedletters:
|
|
if options.html:
|
|
res += accentedletters[abc][0]
|
|
else:
|
|
res += accentedletters[abc][1]
|
|
else:
|
|
res += "\\" + abc
|
|
return res
|
|
|
|
def process(inf, options):
|
|
for line in inf:
|
|
line = line.strip()
|
|
if len(line) > 2 and line[0] == options.field and line[1] == ':':
|
|
print(convertTitle(line[2:].strip(), options))
|
|
break
|
|
|
|
parser = optparse.OptionParser(usage="usage: %prog [options] [filename]\n\n"
|
|
" Extract field data from ABC file.")
|
|
parser.add_option("-f", "--field", dest="field", default="T",
|
|
help="extract the field FIELD", metavar="FIELD")
|
|
parser.add_option("-m", "--html", dest="html",
|
|
action="store_true", default=False,
|
|
help="format output for HTML")
|
|
parser.add_option("-l", "--latex", dest="latex",
|
|
action="store_true", default=False,
|
|
help="format ouput for LaTeX")
|
|
(options, args) = parser.parse_args()
|
|
|
|
if options.html and options.latex:
|
|
sys.exit("You must choose one of HTML or LaTeX output")
|
|
|
|
if len(args) > 0:
|
|
for arg in args:
|
|
try:
|
|
inf = open(arg, "r")
|
|
process(inf, options)
|
|
finally:
|
|
inf.close()
|
|
else:
|
|
process(sys.stdin, options)
|
|
sys.exit(0)
|