view abcfield.py @ 584:696c461c8dc0

Set exit status on abcfield.py to indicate if anything found.
author Jim Hague <jim.hague@acm.org>
date Sat, 29 Oct 2016 23:18:45 +0100
parents 760d0ae5acea
children daa3b76bd11f
line wrap: on
line source

#!/usr/bin/env python
#
# Extact a text field (title, by default) from a .abc file, and print it out
# with any ABC accented characters converted to HTML (default) or Latex.
# Recognise continuation fields and print those too.
#

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 convertField(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 abc in accentedletters:
            if options.html:
                res += accentedletters[abc][0]
            else:
                res += accentedletters[abc][1]
        else:
            res += "\\" + abc
    return res

def process(inf, options):
    n = options.index
    found = False
    for line in inf:
        line = line.strip()
        if len(line) > 2 and line[1] == ':':
            if found:
                if line[0] != '+':
                    break
                line = line[2:].strip()
            elif line[0] == options.field:
                if n > 1:
                    n = n - 1
                    continue
                else:
                    line = line[2:].strip()
                    if len(options.starts) > 0:
                        if line.find(options.starts) == 0:
                            line = line[len(options.starts):].strip()
                        else:
                            continue
            else:
                continue
            found = True
            print(convertField(line, options))
    return found

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("-l", "--latex", dest="latex",
                  action="store_true", default=False,
                  help="convert special characters for LaTeX")
parser.add_option("-n", "--index", dest="index",
                  action="store", type="int", default=1,
                  help="report INDEXth value [default: %default]",
                  metavar="INDEX")
parser.add_option("-s", "--starts", dest="starts",
                  action="store", type="string", default="",
                  help="report only if line starts CONTENT and remove CONTENT",
                  metavar="CONTENT")
(options, args) = parser.parse_args()

res = False
if len(args) > 0:
    for arg in args:
        try:
            inf = open(arg, "r")
            res = res or process(inf, options)
        finally:
            inf.close()
else:
    res = process(sys.stdin, options)
sys.exit(int(not res))