Convert Markdown in N and H fields. Fix up .abc links.

This commit is contained in:
Jim Hague 2016-11-02 08:36:21 +00:00
parent 169520d254
commit d9d4e3c157
1 changed files with 19 additions and 1 deletions

View File

@ -18,6 +18,8 @@
# #
import optparse import optparse
import re
import subprocess
import sys import sys
accentedletters = { accentedletters = {
@ -153,6 +155,20 @@ def convertKeyToDisplay(t):
mode = mode.strip().lower() mode = mode.strip().lower()
return letter + accidental + ' ' + abckeys.get(mode, "Major") 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, # 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. # or the field data must start with a designated string to be recognised.
def getFieldText(inf, field, n = 1, starts = None): 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): def getFieldDisplayText(inf, field, n = 1, starts = None, latex = False):
res = getFieldText(inf, field, n, starts) res = getFieldText(inf, field, n, starts)
if res: if res:
res = convertAccents(res, latex)
if field.upper() == "T": if field.upper() == "T":
res = convertTitleToDisplay(res) res = convertTitleToDisplay(res)
elif field.upper() == "K": elif field.upper() == "K":
res = convertKeyToDisplay(res) res = convertKeyToDisplay(res)
res = convertAccents(res, latex) elif field.upper() in ["H", "N"]:
res = convertMarkdown(res, latex)
return res return res
if __name__ == "__main__": if __name__ == "__main__":