comparison abctemplate.py @ 732:c81a1ed21877

Move prev/next field reading into abctemplate.py. Fishing all that stuff out in makeWeb.sh was a bit ugly.
author Jim Hague <jim.hague@acm.org>
date Thu, 12 Oct 2017 11:21:48 +0100
parents 833e6185b6a2
children
comparison
equal deleted inserted replaced
731:bb4aa5799d91 732:c81a1ed21877
38 import pathlib 38 import pathlib
39 import string 39 import string
40 40
41 from abcfield import getFieldDisplayText, getFullTitle 41 from abcfield import getFieldDisplayText, getFullTitle
42 42
43 def getFileData(f, latex):
44 res = {}
45 input_path = pathlib.Path(args.input.name)
46 res["name"] = input_path.stem
47 fdir = input_path.parent
48
49 lines = f.readlines()
50 res["title"] = getFieldDisplayText(lines, fdir, "T", latex=latex)
51 res["subtitle"] = getFieldDisplayText(lines, fdir, "T", n=2, latex=latex)
52 res["fulltitle"] = getFullTitle(lines, fdir, latex=latex)
53 res["tradition"] = getFieldDisplayText(lines, fdir, "A", latex=latex)
54 res["composer"] = getFieldDisplayText(lines, fdir, "C", latex=latex)
55 res["key"] = getFieldDisplayText(lines, fdir, "K", latex=latex)
56 res["parts"] = getFieldDisplayText(lines, fdir, "P", latex=latex)
57 res["notes"] = getFieldDisplayText(lines, fdir, "N", starts="Dottes:", latex=latex)
58 res["history"] = getFieldDisplayText(lines, fdir, "H", starts="Dottes:", latex=latex)
59 return res
60
43 if __name__ == "__main__": 61 if __name__ == "__main__":
44 parser = argparse.ArgumentParser(description='Substitute values from ABC file into template.') 62 parser = argparse.ArgumentParser(description='Substitute values from ABC file into template.')
45 parser.add_argument('-l', '--latex', dest='latex', 63 parser.add_argument('-l', '--latex', dest='latex',
46 action='store_true', 64 action='store_true',
47 help='output LaTeX formatted values (default is HTML)') 65 help='output LaTeX formatted values (default is HTML)')
49 type=argparse.FileType('r'), 67 type=argparse.FileType('r'),
50 required=True, 68 required=True,
51 help='template file') 69 help='template file')
52 parser.add_argument('-v', '--value', dest='values', action="append", 70 parser.add_argument('-v', '--value', dest='values', action="append",
53 default=[], help='define var=value items for templater') 71 default=[], help='define var=value items for templater')
72 parser.add_argument('-n', '--next', dest='nextfile',
73 type=argparse.FileType('r'),
74 help='next tune file')
75 parser.add_argument('-p', '--prev', dest='prevfile',
76 type=argparse.FileType('r'),
77 help='previous tune file')
54 parser.add_argument('input', type=argparse.FileType('r'), 78 parser.add_argument('input', type=argparse.FileType('r'),
55 help='input ABC file') 79 help='input ABC file')
56 args = parser.parse_args() 80 args = parser.parse_args()
57 81
58 with args.input as f: 82 fields = getFileData(args.input, args.latex)
59 lines = f.readlines()
60 83
61 input_path = pathlib.Path(args.input.name) 84 vars = {}
62 fname = input_path.stem
63 fdir = input_path.parent
64
65 vars = dict()
66 vars["notesvisibility"] = "no" 85 vars["notesvisibility"] = "no"
67 vars["historyvisibility"] = "no" 86 vars["historyvisibility"] = "no"
68 87
69 vars["name"] = fname 88 for name in ["name", "title", "subtitle", "fulltitle", "tradition",
70 vars["title"] = getFieldDisplayText(lines, fdir, "T", latex=args.latex) 89 "composer", "key", "parts", "notes", "history"]:
71 vars["subtitle"] = getFieldDisplayText(lines, fdir, "T", n=2, latex=args.latex) 90 vars[name] = fields[name]
72 vars["fulltitle"] = getFullTitle(lines, fdir, latex=args.latex) 91
73 vars["tradition"] = getFieldDisplayText(lines, fdir, "A", latex=args.latex)
74 vars["composer"] = getFieldDisplayText(lines, fdir, "C", latex=args.latex)
75 vars["key"] = getFieldDisplayText(lines, fdir, "K", latex=args.latex)
76 vars["parts"] = getFieldDisplayText(lines, fdir, "P", latex=args.latex)
77 vars["notes"] = getFieldDisplayText(lines, fdir, "N", starts="Dottes:", latex=args.latex)
78 if vars["notes"]: 92 if vars["notes"]:
79 vars["notesvisibility"] = "yes" 93 vars["notesvisibility"] = "yes"
80 vars["history"] = getFieldDisplayText(lines, fdir, "H", starts="Dottes:", latex=args.latex)
81 if vars["history"]: 94 if vars["history"]:
82 vars["historyvisibility"] = "yes" 95 vars["historyvisibility"] = "yes"
96
97 if args.nextfile:
98 fields = getFileData(args.nextfile, args.latex)
99 for name in ["name", "title", "subtitle", "fulltitle"]:
100 vars["next" + name] = fields[name] if args.nextfile else ""
101
102 if args.prevfile:
103 fields = getFileData(args.prevfile, args.latex)
104 for name in ["name", "title", "subtitle", "fulltitle"]:
105 vars["prev" + name] = fields[name] if args.prevfile else ""
83 106
84 for val in args.values: 107 for val in args.values:
85 keyval = val.partition("=") 108 keyval = val.partition("=")
86 vars[keyval[0]] = keyval[2] 109 vars[keyval[0]] = keyval[2]
87 110