Mercurial > dottes
comparison abcfield.py @ 726:833e6185b6a2 build-default-282
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.
author | Jim Hague <jim.hague@acm.org> |
---|---|
date | Wed, 11 Oct 2017 17:09:31 +0100 |
parents | f9a554e858f3 |
children | 772402f5f8ea |
comparison
equal
deleted
inserted
replaced
725:f9a554e858f3 | 726:833e6185b6a2 |
---|---|
179 # Implement a custom Markdown shorthand for referencing ABC files. | 179 # Implement a custom Markdown shorthand for referencing ABC files. |
180 # <foo.abc> will expand to ['title of foo'](foo.abc). | 180 # <foo.abc> will expand to ['title of foo'](foo.abc). |
181 def expandCustomMarkdown(t, dir, latex): | 181 def expandCustomMarkdown(t, dir, latex): |
182 # Given a match to (foo.abc), return a markdown link to the tune with the | 182 # Given a match to (foo.abc), return a markdown link to the tune with the |
183 # title (and subtitle, if present) of the tune as the text of the link. | 183 # title (and subtitle, if present) of the tune as the text of the link. |
184 def getTitle(m): | 184 def getTitleLink(m): |
185 fname = m.group(1) + ".abc" | 185 fname = m.group(1) + ".abc" |
186 path = pathlib.Path(dir, fname) | 186 path = pathlib.Path(dir, fname) |
187 with path.open() as f: | 187 with path.open() as f: |
188 title = getFieldDisplayText(f, dir, "T", latex=latex) | 188 return "[" + getFullTitle(f, dir, latex=latex) + "](" + fname + ")" |
189 f.seek(0) | 189 return re.sub(r'<(.*?).abc>', getTitleLink, t) |
190 subtitle = getFieldDisplayText(f, dir, "T", n=2, latex=latex) | |
191 if len(subtitle) > 0: | |
192 title = title + " (" + subtitle + ")" | |
193 return "[" + title + "](" + fname + ")" | |
194 return re.sub(r'<(.*?).abc>', getTitle, t) | |
195 | 190 |
196 # Return the raw text for a given field. Optionally the nth field is taken, | 191 # Return the raw text for a given field. Optionally the nth field is taken, |
197 # or the field data must start with a designated string to be recognised. | 192 # or the field data must start with a designated string to be recognised. |
198 def getFieldText(inf, field, n = 1, starts = None): | 193 def getFieldText(lines, field, n = 1, starts = None): |
199 res = "" | 194 res = "" |
200 for line in inf: | 195 for line in lines: |
201 line = line.strip() | 196 line = line.strip() |
202 if len(line) > 2 and line[1] == ':': | 197 if len(line) > 2 and line[1] == ':': |
203 if line[0] == "+" or (line[0] == field and line[2] == "+"): | 198 if line[0] == "+" or (line[0] == field and line[2] == "+"): |
204 if not res: | 199 if not res: |
205 continue | 200 continue |
222 continue | 217 continue |
223 res = line | 218 res = line |
224 return res | 219 return res |
225 | 220 |
226 # Return display text for a given field. | 221 # Return display text for a given field. |
227 def getFieldDisplayText(inf, dir, field, n = 1, starts = None, latex = False): | 222 def getFieldDisplayText(lines, dir, field, n = 1, starts = None, latex = False): |
228 res = getFieldText(inf, field, n, starts) | 223 res = getFieldText(lines, field, n, starts) |
229 if res: | 224 if res: |
230 res = convertAccents(res, latex) | 225 res = convertAccents(res, latex) |
231 if field.upper() == "T": | 226 if field.upper() == "T": |
232 res = convertTitleToDisplay(res) | 227 res = convertTitleToDisplay(res) |
233 elif field.upper() == "K": | 228 elif field.upper() == "K": |
234 res = convertKeyToDisplay(res) | 229 res = convertKeyToDisplay(res) |
235 elif field.upper() in ["H", "N"]: | 230 elif field.upper() in ["H", "N"]: |
236 res = convertMarkdown(expandCustomMarkdown(res, dir, latex), latex) | 231 res = convertMarkdown(expandCustomMarkdown(res, dir, latex), latex) |
237 return res | 232 return res |
238 | 233 |
234 # Return full title (title + [" (" + subtitle + ")"] if subtitle exists). | |
235 def getFullTitle(lines, dir, starts = None, latex = False): | |
236 title = getFieldDisplayText(lines, dir, "T", starts=starts, latex=latex) | |
237 subtitle = getFieldDisplayText(lines, dir, "T", n=2, starts=starts, latex=latex) | |
238 return title if len(subtitle) == 0 else title + " (" + subtitle + ")" | |
239 | |
239 if __name__ == "__main__": | 240 if __name__ == "__main__": |
240 def process(inf, dir, options): | 241 def process(f, dir, options): |
242 lines = f.readlines() | |
241 if options.display: | 243 if options.display: |
242 line = getFieldDisplayText(inf, dir, options.field, options.index, options.starts, options.latex) | 244 line = getFieldDisplayText(lines, dir, options.field, options.index, options.starts, options.latex) |
243 else: | 245 else: |
244 line = getFieldText(inf, options.field, options.index, options.starts) | 246 line = getFieldText(lines, options.field, options.index, options.starts) |
245 if line: | 247 if line: |
246 print(line) | 248 print(line) |
247 return True | 249 return True |
248 else: | 250 else: |
249 return False | 251 return False |