Give ABC file directory when expanding markdown.
<foo.abc> specifies a file in the same directory as the file being processed.
This commit is contained in:
parent
c1c7d23c0b
commit
945454da9d
27
abcfield.py
27
abcfield.py
|
@ -168,17 +168,18 @@ def convertMarkdown(t, 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.strip()
|
||||||
|
|
||||||
# Implement a custom Markdown shorthand for referencing ABC files.
|
# Implement a custom Markdown shorthand for referencing ABC files.
|
||||||
# <foo.abc> will expand to ['title of foo'](foo.abc).
|
# <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
|
# 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.
|
# title of the tune as the text of the link.
|
||||||
def getTitle(m):
|
def getTitle(m):
|
||||||
fname = m.group(1) + ".abc"
|
fname = m.group(1) + ".abc"
|
||||||
with pathlib.Path(fname).open() as f:
|
path = pathlib.Path(dir, fname)
|
||||||
return "[" + getFieldDisplayText(f, "T", latex) + "](" + fname + ")"
|
with path.open() as f:
|
||||||
|
return "[" + getFieldDisplayText(f, dir, "T", latex) + "](" + fname + ")"
|
||||||
return re.sub(r'<(.*?).abc>', getTitle, t)
|
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,
|
||||||
|
@ -212,7 +213,7 @@ def getFieldText(inf, field, n = 1, starts = None):
|
||||||
return res
|
return res
|
||||||
|
|
||||||
# Return display text for a given field.
|
# 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)
|
res = getFieldText(inf, field, n, starts)
|
||||||
if res:
|
if res:
|
||||||
res = convertAccents(res, latex)
|
res = convertAccents(res, latex)
|
||||||
|
@ -221,13 +222,13 @@ 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(expandCustomMarkdown(res, latex), latex)
|
res = convertMarkdown(expandCustomMarkdown(res, dir, latex), latex)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
def process(inf, options):
|
def process(inf, dir, options):
|
||||||
if options.display:
|
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:
|
else:
|
||||||
line = getFieldText(inf, options.field, options.index, options.starts)
|
line = getFieldText(inf, options.field, options.index, options.starts)
|
||||||
if line:
|
if line:
|
||||||
|
@ -260,11 +261,9 @@ if __name__ == "__main__":
|
||||||
res = False
|
res = False
|
||||||
if len(args) > 0:
|
if len(args) > 0:
|
||||||
for arg in args:
|
for arg in args:
|
||||||
try:
|
path = pathlib.Path(arg)
|
||||||
inf = open(arg, "r")
|
with path.open() as f:
|
||||||
res = res or process(inf, options)
|
res = res or process(f, path.parent, options)
|
||||||
finally:
|
|
||||||
inf.close()
|
|
||||||
else:
|
else:
|
||||||
res = process(sys.stdin, options)
|
res = process(sys.stdin, ".", options)
|
||||||
sys.exit(int(not res))
|
sys.exit(int(not res))
|
||||||
|
|
Loading…
Reference in New Issue