From d9d4e3c15721c30216b880e03aa75b73402e1a0a Mon Sep 17 00:00:00 2001 From: Jim Hague Date: Wed, 2 Nov 2016 08:36:21 +0000 Subject: [PATCH] Convert Markdown in N and H fields. Fix up .abc links. --- abcfield.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/abcfield.py b/abcfield.py index b0aec83..6cc88ab 100755 --- a/abcfield.py +++ b/abcfield.py @@ -18,6 +18,8 @@ # import optparse +import re +import subprocess import sys accentedletters = { @@ -153,6 +155,20 @@ def convertKeyToDisplay(t): 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 getFieldText(inf, field, n = 1, starts = None): 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__":