changeset 110:a63e39fc3410

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.
author Jim Hague <jim.hague@acm.org>
date Thu, 15 Mar 2012 14:32:53 +0000
parents 7cc689f59710
children 4e2b25cfad99
files Session/PolskaFranLovstabruk.abc abctitle.py makeBooke.sh makeWeb.sh
diffstat 4 files changed, 140 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/Session/PolskaFranLovstabruk.abc	Wed Mar 14 15:56:48 2012 +0000
+++ b/Session/PolskaFranLovstabruk.abc	Thu Mar 15 14:32:53 2012 +0000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/abctitle.py	Thu Mar 15 14:32:53 2012 +0000
@@ -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" : ("&Aacute;", "\\'{A}"),
+    "'E" : ("&Eacute;", "\\'{E}"),
+    "'I" : ("&Iacute;", "\\'{I}"),
+    "'O" : ("&Oacute;", "\\'{O}"),
+    "'U" : ("&Uacute;", "\\'{U}"),
+    "'Y" : ("&Yacute;", "\\'{Y}"),
+    "'a" : ("&aacute;", "\\'{a}"),
+    "'e" : ("&eacute;", "\\'{e}"),
+    "'i" : ("&iacute;", "\\'{i}"),
+    "'o" : ("&oacute;", "\\'{o}"),
+    "'u" : ("&uacute;", "\\'{u}"),
+    "'y" : ("&yacute;", "\\'{y}"),
+
+    # Grave accents
+    "`A" : ("&Agrave;", "\\`{A}"),
+    "`E" : ("&Egrave;", "\\`{E}"),
+    "`I" : ("&Igrave;", "\\`{I}"),
+    "`O" : ("&Ograve;", "\\`{O}"),
+    "`U" : ("&Ugrave;", "\\`{U}"),
+    "`a" : ("&agrave;", "\\`{a}"),
+    "`e" : ("&egrave;", "\\`{e}"),
+    "`i" : ("&igrave;", "\\`{i}"),
+    "`o" : ("&ograve;", "\\`{o}"),
+    "`u" : ("&ugrave;", "\\`{u}"),
+
+    # Umlauts
+    "\"A" : ("&Auml;", "\\\"{A}"),
+    "\"E" : ("&Euml;", "\\\"{E}"),
+    "\"I" : ("&Iuml;", "\\\"{I}"),
+    "\"O" : ("&Ouml;", "\\\"{O}"),
+    "\"U" : ("&Uuml;", "\\\"{U}"),
+    "\"Y" : ("&Yuml;", "\\\"{Y}"),
+    "\"a" : ("&auml;", "\\\"{a}"),
+    "\"e" : ("&euml;", "\\\"{e}"),
+    "\"i" : ("&iuml;", "\\\"{\i}"),
+    "\"o" : ("&ouml;", "\\\"{o}"),
+    "\"u" : ("&uuml;", "\\\"{u}"),
+    "\"y" : ("&yuml;", "\\\"{y}"),
+
+    # Circumflexes
+    "^A" : ("&Acirc;", "\\^{A}"),
+    "^E" : ("&Ecirc;", "\\^{E}"),
+    "^I" : ("&Icirc;", "\\^{I}"),
+    "^O" : ("&Ocirc;", "\\^{O}"),
+    "^U" : ("&Ucirc;", "\\^{U}"),
+    "^a" : ("&acirc;", "\\^{a}"),
+    "^e" : ("&ecirc;", "\\^{e}"),
+    "^i" : ("&icirc;", "\\^{\i}"),
+    "^o" : ("&ocirc;", "\\^{o}"),
+    "^u" : ("&ucirc;", "\\^{u}"),
+
+    # Tilde
+    "~A" : ("&Atilde;", "\\~{A}"),
+    "~N" : ("&Ntilde;", "\\~{N}"),
+    "~O" : ("&Otilde;", "\\~{O}"),
+    "~a" : ("&atilde;", "\\~{a}"),
+    "~n" : ("&ntilde;", "\\~{n}"),
+    "~o" : ("&otilde;", "\\~{o}"),
+
+    # Cedilla
+    ",C" : ("&Ccedil;", "\\c{C}"),
+    ",c" : ("&ccedil;", "\\c{c}"),
+
+    # Slash
+    "/O" : ("&Oslash;", "\\O"),
+    "/o" : ("&oslash;", "\\o"),
+
+    # Ring
+    "AA" : ("&Aring;", "\\r{A}"),
+    "aa" : ("&aring;", "\\r{a}"),
+
+    # Ligatures
+    "AE" : ("&AElig;", "\\AE"),
+    "ae" : ("&aelig;", "\\ae"),
+    "ss" : ("&szlig;", "\\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)
--- a/makeBooke.sh	Wed Mar 14 15:56:48 2012 +0000
+++ b/makeBooke.sh	Thu Mar 15 14:32:53 2012 +0000
@@ -42,7 +42,7 @@
 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 @@
 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
--- a/makeWeb.sh	Wed Mar 14 15:56:48 2012 +0000
+++ b/makeWeb.sh	Thu Mar 15 14:32:53 2012 +0000
@@ -37,7 +37,7 @@
 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.