# HG changeset patch # User Jim Hague # Date 1507738171 -3600 # Node ID 833e6185b6a214edeea77401bb011f1664e484b9 # Parent f9a554e858f321ce0f1970769cb0f2d65a432f51 Add fulltitle to template fields. fulltitle is title + [(" subtitle ")"] if subtitle is present. To do: extend full title to next and prev. this means exposing it in abcfield.py. diff -r f9a554e858f3 -r 833e6185b6a2 abcfield.py --- a/abcfield.py Wed Oct 11 17:03:29 2017 +0100 +++ b/abcfield.py Wed Oct 11 17:09:31 2017 +0100 @@ -181,23 +181,18 @@ 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 @@ 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 @@ 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 -r f9a554e858f3 -r 833e6185b6a2 abctemplate.py --- a/abctemplate.py Wed Oct 11 17:03:29 2017 +0100 +++ b/abctemplate.py Wed Oct 11 17:09:31 2017 +0100 @@ -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 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 @@ 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 -r f9a554e858f3 -r 833e6185b6a2 dottes.html.learnertune --- a/dottes.html.learnertune Wed Oct 11 17:03:29 2017 +0100 +++ b/dottes.html.learnertune Wed Oct 11 17:09:31 2017 +0100 @@ -48,7 +48,7 @@ -

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

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

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