Mercurial > dottes
diff abcfield.py @ 592:9986c67edf91
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.
author | Jim Hague <jim.hague@acm.org> |
---|---|
date | Wed, 02 Nov 2016 14:06:25 +0000 |
parents | 2face6618bf3 |
children | 82e818c41e81 |
line wrap: on
line diff
--- a/abcfield.py Wed Nov 02 08:36:21 2016 +0000 +++ b/abcfield.py Wed Nov 02 14:06:25 2016 +0000 @@ -18,6 +18,7 @@ # import optparse +import pathlib import re import subprocess import sys @@ -164,11 +165,22 @@ 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) + res = re.sub(r'\\href{(.*?).abc}', r'\\hyperlink{\1}', res) 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 +# 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, # or the field data must start with a designated string to be recognised. def getFieldText(inf, field, n = 1, starts = None): @@ -209,7 +221,7 @@ elif field.upper() == "K": res = convertKeyToDisplay(res) elif field.upper() in ["H", "N"]: - res = convertMarkdown(res, latex) + res = convertMarkdown(expandCustomMarkdown(res, latex), latex) return res if __name__ == "__main__":