2016-10-31 23:48:45 +00:00
|
|
|
#!/usr/bin/env python3
|
2012-03-15 14:32:53 +00:00
|
|
|
#
|
2012-04-21 19:47:30 +01:00
|
|
|
# Extact a text field (title, by default) from a .abc file, and print it out
|
2016-10-29 19:32:53 +01:00
|
|
|
# with any ABC accented characters converted to HTML (default) or Latex.
|
2016-10-31 23:48:45 +00:00
|
|
|
#
|
|
|
|
# Optionally rearrange a field into display format:
|
|
|
|
# * In Title fields, change 'sort' form such as 'Exploding Potato, The'
|
|
|
|
# to display format 'The Exploding Potato'.
|
|
|
|
# * In Key fields, translate the ABC key representation to full text,
|
|
|
|
# e.g. G#dor becomes G# Dorian.
|
|
|
|
#
|
|
|
|
# Recognise continuation header fields and print those too. The ABC standard
|
|
|
|
# defines continuation fields as starting ':+'. Regrettably none of the tools
|
|
|
|
# I am using the Booke recognise that syntax, so I am adopting a Booke
|
|
|
|
# convention of '<header>:+' *also* being a continuation. Note that a
|
|
|
|
# continuation is a distinct line in the field value; the value has a line
|
|
|
|
# break between it and the previous line.
|
2012-03-15 14:32:53 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
import optparse
|
2016-11-02 14:06:25 +00:00
|
|
|
import pathlib
|
2016-11-02 08:36:21 +00:00
|
|
|
import re
|
|
|
|
import subprocess
|
2012-03-15 14:32:53 +00:00
|
|
|
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"),
|
|
|
|
}
|
|
|
|
|
2016-10-31 23:48:45 +00:00
|
|
|
abckeys = {
|
|
|
|
"m": "Minor",
|
|
|
|
"min": "Minor",
|
|
|
|
"mix": "Mixolydian",
|
|
|
|
"dor": "Dorian",
|
|
|
|
"phr": "Phrygian",
|
|
|
|
"lyd": "Lydian",
|
|
|
|
"loc": "Locrian",
|
|
|
|
}
|
|
|
|
|
|
|
|
# Convert ABC accented chars to HTML entities or LaTex.
|
|
|
|
def convertAccents(t, latex=False):
|
2012-03-15 14:32:53 +00:00
|
|
|
res = ""
|
|
|
|
while True:
|
|
|
|
p = t.partition('\\')
|
|
|
|
res += p[0]
|
|
|
|
if p[1] == "":
|
|
|
|
break
|
|
|
|
abc = p[2][0:2]
|
|
|
|
t = p[2][2:]
|
2016-10-29 19:32:53 +01:00
|
|
|
if abc in accentedletters:
|
2016-10-31 23:48:45 +00:00
|
|
|
if latex:
|
2012-03-15 14:32:53 +00:00
|
|
|
res += accentedletters[abc][1]
|
2016-10-31 23:48:45 +00:00
|
|
|
else:
|
|
|
|
res += accentedletters[abc][0]
|
2012-03-15 14:32:53 +00:00
|
|
|
else:
|
|
|
|
res += "\\" + abc
|
|
|
|
return res
|
|
|
|
|
2016-10-31 23:48:45 +00:00
|
|
|
# Convert Title fields from sort to display, so Bat, The->The Bat.
|
|
|
|
def convertTitleToDisplay(t):
|
|
|
|
p = t.rpartition(',')
|
|
|
|
if p[1] == "":
|
|
|
|
return t
|
|
|
|
else:
|
|
|
|
return p[2].strip() + " " + p[0].strip()
|
|
|
|
|
|
|
|
# Convert Key field from ABC to display, so G#dor->G# Dorian.
|
|
|
|
def convertKeyToDisplay(t):
|
|
|
|
letter = t[0].upper()
|
|
|
|
accidental = ""
|
|
|
|
mode = ""
|
|
|
|
try:
|
|
|
|
accidental = t[1]
|
|
|
|
if accidental == '#' or accidental == 'b':
|
|
|
|
mode = t[2:]
|
|
|
|
else:
|
|
|
|
accidental = ""
|
|
|
|
mode = t[1:]
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
mode = mode.strip().lower()
|
|
|
|
return letter + accidental + ' ' + abckeys.get(mode, "Major")
|
|
|
|
|
2016-11-02 08:36:21 +00:00
|
|
|
# Convert input string from Markdown to HTML or LaTeX. Fix up link
|
|
|
|
# targets so any 'foo.abc' target links to the tune with that name.
|
|
|
|
def convertMarkdown(t, latex):
|
|
|
|
if latex:
|
|
|
|
target = "--to=latex"
|
|
|
|
else:
|
|
|
|
target = "--to=html"
|
|
|
|
res = subprocess.check_output(['pandoc', '--from=markdown', target], input=t, universal_newlines=True)
|
|
|
|
if latex:
|
2016-11-02 14:06:25 +00:00
|
|
|
res = re.sub(r'\\href{(.*?).abc}', r'\\hyperlink{\1}', res)
|
2016-11-02 08:36:21 +00:00
|
|
|
else:
|
2016-11-02 14:06:25 +00:00
|
|
|
res = re.sub(r'href="(.*?).abc"', r'href="\1.html"', res)
|
2016-11-02 14:59:31 +00:00
|
|
|
return res.strip()
|
2016-11-02 08:36:21 +00:00
|
|
|
|
2016-11-02 14:06:25 +00:00
|
|
|
# Implement a custom Markdown shorthand for referencing ABC files.
|
|
|
|
# <foo.abc> will expand to ['title of foo'](foo.abc).
|
2016-11-02 14:59:31 +00:00
|
|
|
def expandCustomMarkdown(t, dir, latex):
|
2016-11-02 14:06:25 +00:00
|
|
|
# Given a match to (foo.abc), return a markdown link to the tune with the
|
|
|
|
# title of the tune as the text of the link.
|
|
|
|
def getTitle(m):
|
|
|
|
fname = m.group(1) + ".abc"
|
2016-11-02 14:59:31 +00:00
|
|
|
path = pathlib.Path(dir, fname)
|
|
|
|
with path.open() as f:
|
|
|
|
return "[" + getFieldDisplayText(f, dir, "T", latex) + "](" + fname + ")"
|
2016-11-02 14:06:25 +00:00
|
|
|
return re.sub(r'<(.*?).abc>', getTitle, t)
|
|
|
|
|
2016-10-31 23:48:45 +00:00
|
|
|
# Return the raw text for a given field. Optionally the nth field is taken,
|
|
|
|
# or the field data must start with a designated string to be recognised.
|
|
|
|
def getFieldText(inf, field, n = 1, starts = None):
|
2016-11-02 00:21:18 +00:00
|
|
|
res = ""
|
2012-03-15 14:32:53 +00:00
|
|
|
for line in inf:
|
|
|
|
line = line.strip()
|
2016-10-29 19:32:53 +01:00
|
|
|
if len(line) > 2 and line[1] == ':':
|
2016-10-31 23:48:45 +00:00
|
|
|
if line[0] == "+" or (line[0] == field and line[2] == "+"):
|
|
|
|
if not res:
|
2013-08-04 22:09:25 +01:00
|
|
|
continue
|
2016-10-31 23:48:45 +00:00
|
|
|
if line[0] == "+":
|
|
|
|
line = line[2:]
|
2016-10-29 19:32:53 +01:00
|
|
|
else:
|
2016-10-31 23:48:45 +00:00
|
|
|
line = line[3:]
|
|
|
|
res = res + '\n' + line.strip()
|
|
|
|
else:
|
|
|
|
if res:
|
|
|
|
break
|
|
|
|
if line[0] == field:
|
2016-10-29 19:32:53 +01:00
|
|
|
line = line[2:].strip()
|
2016-10-31 23:48:45 +00:00
|
|
|
if starts:
|
|
|
|
if line.find(starts) != 0:
|
2016-10-29 19:32:53 +01:00
|
|
|
continue
|
2016-10-31 23:48:45 +00:00
|
|
|
line = line[len(starts):].strip()
|
|
|
|
if n > 1:
|
|
|
|
n = n - 1
|
|
|
|
continue
|
|
|
|
res = line
|
|
|
|
return res
|
|
|
|
|
|
|
|
# Return display text for a given field.
|
2016-11-02 14:59:31 +00:00
|
|
|
def getFieldDisplayText(inf, dir, field, n = 1, starts = None, latex = False):
|
2016-10-31 23:48:45 +00:00
|
|
|
res = getFieldText(inf, field, n, starts)
|
|
|
|
if res:
|
2016-11-02 08:36:21 +00:00
|
|
|
res = convertAccents(res, latex)
|
2016-10-31 23:48:45 +00:00
|
|
|
if field.upper() == "T":
|
|
|
|
res = convertTitleToDisplay(res)
|
|
|
|
elif field.upper() == "K":
|
|
|
|
res = convertKeyToDisplay(res)
|
2016-11-02 08:36:21 +00:00
|
|
|
elif field.upper() in ["H", "N"]:
|
2016-11-02 14:59:31 +00:00
|
|
|
res = convertMarkdown(expandCustomMarkdown(res, dir, latex), latex)
|
2016-10-31 23:48:45 +00:00
|
|
|
return res
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2016-11-02 14:59:31 +00:00
|
|
|
def process(inf, dir, options):
|
2016-10-31 23:48:45 +00:00
|
|
|
if options.display:
|
2016-11-02 14:59:31 +00:00
|
|
|
line = getFieldDisplayText(inf, dir, options.field, options.index, options.starts, options.latex)
|
2016-10-31 23:48:45 +00:00
|
|
|
else:
|
|
|
|
line = getFieldText(inf, options.field, options.index, options.starts)
|
|
|
|
if line:
|
|
|
|
print(line)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# execute only if run as a script
|
|
|
|
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("-d", "--display", dest="display",
|
|
|
|
action="store_true", default=False,
|
|
|
|
help="convert to display text")
|
|
|
|
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=None,
|
|
|
|
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:
|
2016-11-02 14:59:31 +00:00
|
|
|
path = pathlib.Path(arg)
|
|
|
|
with path.open() as f:
|
|
|
|
res = res or process(f, path.parent, options)
|
2016-10-31 23:48:45 +00:00
|
|
|
else:
|
2016-11-02 14:59:31 +00:00
|
|
|
res = process(sys.stdin, ".", options)
|
2016-10-31 23:48:45 +00:00
|
|
|
sys.exit(int(not res))
|