Mercurial > dottes
changeset 593:82e818c41e81
Give ABC file directory when expanding markdown.
<foo.abc> specifies a file in the same directory as the file being
processed.
author | Jim Hague <jim.hague@acm.org> |
---|---|
date | Wed, 02 Nov 2016 14:59:31 +0000 |
parents | 9986c67edf91 |
children | 60749c792cde |
files | abcfield.py |
diffstat | 1 files changed, 13 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/abcfield.py Wed Nov 02 14:06:25 2016 +0000 +++ b/abcfield.py Wed Nov 02 14:59:31 2016 +0000 @@ -168,17 +168,18 @@ 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 res.strip() # Implement a custom Markdown shorthand for referencing ABC files. # <foo.abc> will expand to ['title of foo'](foo.abc). -def expandCustomMarkdown(t, latex): +def expandCustomMarkdown(t, dir, 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 + ")" + path = pathlib.Path(dir, fname) + with path.open() as f: + return "[" + getFieldDisplayText(f, dir, "T", latex) + "](" + fname + ")" return re.sub(r'<(.*?).abc>', getTitle, t) # Return the raw text for a given field. Optionally the nth field is taken, @@ -212,7 +213,7 @@ return res # Return display text for a given field. -def getFieldDisplayText(inf, field, n = 1, starts = None, latex = False): +def getFieldDisplayText(inf, dir, field, n = 1, starts = None, latex = False): res = getFieldText(inf, field, n, starts) if res: res = convertAccents(res, latex) @@ -221,13 +222,13 @@ elif field.upper() == "K": res = convertKeyToDisplay(res) elif field.upper() in ["H", "N"]: - res = convertMarkdown(expandCustomMarkdown(res, latex), latex) + res = convertMarkdown(expandCustomMarkdown(res, dir, latex), latex) return res if __name__ == "__main__": - def process(inf, options): + def process(inf, dir, options): if options.display: - line = getFieldDisplayText(inf, options.field, options.index, options.starts, options.latex) + line = getFieldDisplayText(inf, dir, options.field, options.index, options.starts, options.latex) else: line = getFieldText(inf, options.field, options.index, options.starts) if line: @@ -260,11 +261,9 @@ res = False if len(args) > 0: for arg in args: - try: - inf = open(arg, "r") - res = res or process(inf, options) - finally: - inf.close() + path = pathlib.Path(arg) + with path.open() as f: + res = res or process(f, path.parent, options) else: - res = process(sys.stdin, options) + res = process(sys.stdin, ".", options) sys.exit(int(not res))