# HG changeset patch # User Jim Hague # Date 1478075781 0 # Node ID 2face6618bf32ffd9c16f544c9c5ee68c7e5a884 # Parent a6fc091a8c5edf6201e5056886c9e8ad8a0fc7c9 Convert Markdown in N and H fields. Fix up .abc links. diff -r a6fc091a8c5e -r 2face6618bf3 abcfield.py --- a/abcfield.py Wed Nov 02 01:12:42 2016 +0000 +++ b/abcfield.py Wed Nov 02 08:36:21 2016 +0000 @@ -18,6 +18,8 @@ # import optparse +import re +import subprocess import sys accentedletters = { @@ -153,6 +155,20 @@ mode = mode.strip().lower() return letter + accidental + ' ' + abckeys.get(mode, "Major") +# 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: + res = re.sub(r'\\href{(.*).abc}', r'\\hyperlink{\1}', res) + else: + res = re.sub(r'href="(.*).abc"', r'href="\1.html"', res) + return res + # 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): @@ -187,11 +203,13 @@ def getFieldDisplayText(inf, field, n = 1, starts = None, latex = False): res = getFieldText(inf, field, n, starts) if res: + res = convertAccents(res, latex) if field.upper() == "T": res = convertTitleToDisplay(res) elif field.upper() == "K": res = convertKeyToDisplay(res) - res = convertAccents(res, latex) + elif field.upper() in ["H", "N"]: + res = convertMarkdown(res, latex) return res if __name__ == "__main__":