Implement custom markdown <foo.abc> to reference tune.

Also ensure filename matches are not greedy, and so work if there are
two filenames on a line.
This commit is contained in:
Jim Hague 2016-11-02 14:06:25 +00:00
parent d9d4e3c157
commit c1c7d23c0b
1 changed files with 15 additions and 3 deletions

View File

@ -18,6 +18,7 @@
# #
import optparse import optparse
import pathlib
import re import re
import subprocess import subprocess
import sys import sys
@ -164,11 +165,22 @@ def convertMarkdown(t, latex):
target = "--to=html" target = "--to=html"
res = subprocess.check_output(['pandoc', '--from=markdown', target], input=t, universal_newlines=True) res = subprocess.check_output(['pandoc', '--from=markdown', target], input=t, universal_newlines=True)
if latex: if latex:
res = re.sub(r'\\href{(.*).abc}', r'\\hyperlink{\1}', res) res = re.sub(r'\\href{(.*?).abc}', r'\\hyperlink{\1}', res)
else: else:
res = re.sub(r'href="(.*).abc"', r'href="\1.html"', res) res = re.sub(r'href="(.*?).abc"', r'href="\1.html"', res)
return res return res
# Implement a custom Markdown shorthand for referencing ABC files.
# <foo.abc> will expand to ['title of foo'](foo.abc).
def expandCustomMarkdown(t, latex):
# 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"
with pathlib.Path(fname).open() as f:
return "[" + getFieldDisplayText(f, "T", latex) + "](" + fname + ")"
return re.sub(r'<(.*?).abc>', getTitle, t)
# 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):
@ -209,7 +221,7 @@ def getFieldDisplayText(inf, field, n = 1, starts = None, latex = False):
elif field.upper() == "K": elif field.upper() == "K":
res = convertKeyToDisplay(res) res = convertKeyToDisplay(res)
elif field.upper() in ["H", "N"]: elif field.upper() in ["H", "N"]:
res = convertMarkdown(res, latex) res = convertMarkdown(expandCustomMarkdown(res, latex), latex)
return res return res
if __name__ == "__main__": if __name__ == "__main__":