From 19e9f59ff2e78cd18bdbc1ae97949c70f3a81662 Mon Sep 17 00:00:00 2001 From: Jim Hague Date: Thu, 15 Mar 2012 14:32:53 +0000 Subject: [PATCH] Add basic diacritic handling for LaTeX and web. It would be nice if everyone used UTF-8, but it doesn't look like that works at present. --- Session/PolskaFranLovstabruk.abc | 2 +- abctitle.py | 136 +++++++++++++++++++++++++++++++ makeBooke.sh | 4 +- makeWeb.sh | 2 +- 4 files changed, 140 insertions(+), 4 deletions(-) create mode 100755 abctitle.py diff --git a/Session/PolskaFranLovstabruk.abc b/Session/PolskaFranLovstabruk.abc index 7f39814..4e5576e 100644 --- a/Session/PolskaFranLovstabruk.abc +++ b/Session/PolskaFranLovstabruk.abc @@ -1,5 +1,5 @@ X: 1 -T: Polska fran Lovstabruk +T: Polska fr\aan L\"ovstabruk C: efter Ceylon Wallin M: 3/4 L: 1/8 diff --git a/abctitle.py b/abctitle.py new file mode 100755 index 0000000..f6cbe60 --- /dev/null +++ b/abctitle.py @@ -0,0 +1,136 @@ +#!/usr/bin/env python +# +# Extact the first tune title from a .abc data, and print it out +# formatted for use in LaTeX. +# + +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 line[0:2] == "T:": + print(convertTitle(line[2:].strip(), options)) + break + +parser = optparse.OptionParser(usage="usage: %prog [options] [filename]\n\n" + " Extract title from ABC file.") +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) diff --git a/makeBooke.sh b/makeBooke.sh index 4a73865..18712b4 100755 --- a/makeBooke.sh +++ b/makeBooke.sh @@ -42,7 +42,7 @@ cp dottes.tex.header $builddir/$output find $booke -name "*.abc" | sort | while read filename do - title=`grep "^T:" $filename | head -1 | sed -e "s/^T: *//"` + title=`$dir/abctitle.py --latex $filename` name=`basename $filename .abc` echo -E "\begin{center}" >> $builddir/$output echo -E "\includegraphics[width=\textwidth]{$graphicsdir/$name}" >> $builddir/$output @@ -55,7 +55,7 @@ cat dottes.tex.firstlines >> $builddir/$output find $booke -name "*.abc" | sort | while read filename do - title=`grep "^T:" $filename | head -1 | sed -e "s/^T: *//"` + title=`$dir/abctitle.py --latex $filename` name=`basename $filename .abc` echo -E "$title & \raisebox{-.25\height}{\includegraphics[width=0.6\textwidth]{$graphicsdir/firstline-$name}} \\\\" >> $builddir/$output done diff --git a/makeWeb.sh b/makeWeb.sh index 90362fe..6535c1c 100755 --- a/makeWeb.sh +++ b/makeWeb.sh @@ -37,7 +37,7 @@ cp $1.pdf $1-booklet.pdf $webdir find $booke -name "*.abc" | sort | while read filename do - title=`grep "^T:" $filename | head -1 | sed -e "s/^T: *//"` + title=`$dir/abctitle.py --html $filename` name=`basename $filename .abc` # Copy tune PDF from common graphics.