diff --git a/abcfield.py b/abcfield.py index 692d229..895e703 100755 --- a/abcfield.py +++ b/abcfield.py @@ -181,23 +181,18 @@ def convertMarkdown(t, latex): def expandCustomMarkdown(t, dir, latex): # Given a match to (foo.abc), return a markdown link to the tune with the # title (and subtitle, if present) of the tune as the text of the link. - def getTitle(m): + def getTitleLink(m): fname = m.group(1) + ".abc" path = pathlib.Path(dir, fname) with path.open() as f: - title = getFieldDisplayText(f, dir, "T", latex=latex) - f.seek(0) - subtitle = getFieldDisplayText(f, dir, "T", n=2, latex=latex) - if len(subtitle) > 0: - title = title + " (" + subtitle + ")" - return "[" + title + "](" + fname + ")" - return re.sub(r'<(.*?).abc>', getTitle, t) + return "[" + getFullTitle(f, dir, latex=latex) + "](" + fname + ")" + return re.sub(r'<(.*?).abc>', getTitleLink, 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): +def getFieldText(lines, field, n = 1, starts = None): res = "" - for line in inf: + for line in lines: line = line.strip() if len(line) > 2 and line[1] == ':': if line[0] == "+" or (line[0] == field and line[2] == "+"): @@ -224,8 +219,8 @@ def getFieldText(inf, field, n = 1, starts = None): return res # Return display text for a given field. -def getFieldDisplayText(inf, dir, field, n = 1, starts = None, latex = False): - res = getFieldText(inf, field, n, starts) +def getFieldDisplayText(lines, dir, field, n = 1, starts = None, latex = False): + res = getFieldText(lines, field, n, starts) if res: res = convertAccents(res, latex) if field.upper() == "T": @@ -236,12 +231,19 @@ def getFieldDisplayText(inf, dir, field, n = 1, starts = None, latex = False): res = convertMarkdown(expandCustomMarkdown(res, dir, latex), latex) return res +# Return full title (title + [" (" + subtitle + ")"] if subtitle exists). +def getFullTitle(lines, dir, starts = None, latex = False): + title = getFieldDisplayText(lines, dir, "T", starts=starts, latex=latex) + subtitle = getFieldDisplayText(lines, dir, "T", n=2, starts=starts, latex=latex) + return title if len(subtitle) == 0 else title + " (" + subtitle + ")" + if __name__ == "__main__": - def process(inf, dir, options): + def process(f, dir, options): + lines = f.readlines() if options.display: - line = getFieldDisplayText(inf, dir, options.field, options.index, options.starts, options.latex) + line = getFieldDisplayText(lines, dir, options.field, options.index, options.starts, options.latex) else: - line = getFieldText(inf, options.field, options.index, options.starts) + line = getFieldText(lines, options.field, options.index, options.starts) if line: print(line) return True diff --git a/abctemplate.py b/abctemplate.py index 43aa265..b3f7ec2 100755 --- a/abctemplate.py +++ b/abctemplate.py @@ -21,6 +21,7 @@ # * name. The file base name. Base filename without extension. # * title. The tune title. # * subtitle. The tune subtitle (second Title field), if any. +# * fulltitle. The tune title followed, if it exists, by " (" subtitle ")" # * tradition. The Morris tradition the dance tune is from. # * composer. The tune composer. # * key. The tune key. @@ -37,7 +38,7 @@ import argparse import pathlib import string -from abcfield import getFieldDisplayText +from abcfield import getFieldDisplayText, getFullTitle if __name__ == "__main__": parser = argparse.ArgumentParser(description='Substitute values from ABC file into template.') @@ -68,6 +69,7 @@ if __name__ == "__main__": vars["name"] = fname vars["title"] = getFieldDisplayText(lines, fdir, "T", latex=args.latex) vars["subtitle"] = getFieldDisplayText(lines, fdir, "T", n=2, latex=args.latex) + vars["fulltitle"] = getFullTitle(lines, fdir, latex=args.latex) vars["tradition"] = getFieldDisplayText(lines, fdir, "A", latex=args.latex) vars["composer"] = getFieldDisplayText(lines, fdir, "C", latex=args.latex) vars["key"] = getFieldDisplayText(lines, fdir, "K", latex=args.latex) diff --git a/dottes.html.learnertune b/dottes.html.learnertune index dc01ee6..8fce74f 100644 --- a/dottes.html.learnertune +++ b/dottes.html.learnertune @@ -48,7 +48,7 @@ -

${title} is in the key of ${key}. +

${fulltitle} is in the key of ${key}.

@@ -149,7 +149,7 @@