comparison abcfield.py @ 592:9986c67edf91

Implement custom markdown <foo.abc> to reference tune. Also ensure filename matches are not greedy, and so work if there are two filenames on a line.
author Jim Hague <jim.hague@acm.org>
date Wed, 02 Nov 2016 14:06:25 +0000
parents 2face6618bf3
children 82e818c41e81
comparison
equal deleted inserted replaced
591:2face6618bf3 592:9986c67edf91
16 # continuation is a distinct line in the field value; the value has a line 16 # continuation is a distinct line in the field value; the value has a line
17 # break between it and the previous line. 17 # break between it and the previous line.
18 # 18 #
19 19
20 import optparse 20 import optparse
21 import pathlib
21 import re 22 import re
22 import subprocess 23 import subprocess
23 import sys 24 import sys
24 25
25 accentedletters = { 26 accentedletters = {
162 target = "--to=latex" 163 target = "--to=latex"
163 else: 164 else:
164 target = "--to=html" 165 target = "--to=html"
165 res = subprocess.check_output(['pandoc', '--from=markdown', target], input=t, universal_newlines=True) 166 res = subprocess.check_output(['pandoc', '--from=markdown', target], input=t, universal_newlines=True)
166 if latex: 167 if latex:
167 res = re.sub(r'\\href{(.*).abc}', r'\\hyperlink{\1}', res) 168 res = re.sub(r'\\href{(.*?).abc}', r'\\hyperlink{\1}', res)
168 else: 169 else:
169 res = re.sub(r'href="(.*).abc"', r'href="\1.html"', res) 170 res = re.sub(r'href="(.*?).abc"', r'href="\1.html"', res)
170 return res 171 return res
172
173 # Implement a custom Markdown shorthand for referencing ABC files.
174 # <foo.abc> will expand to ['title of foo'](foo.abc).
175 def expandCustomMarkdown(t, latex):
176 # Given a match to (foo.abc), return a markdown link to the tune with the
177 # title of the tune as the text of the link.
178 def getTitle(m):
179 fname = m.group(1) + ".abc"
180 with pathlib.Path(fname).open() as f:
181 return "[" + getFieldDisplayText(f, "T", latex) + "](" + fname + ")"
182 return re.sub(r'<(.*?).abc>', getTitle, t)
171 183
172 # Return the raw text for a given field. Optionally the nth field is taken, 184 # Return the raw text for a given field. Optionally the nth field is taken,
173 # or the field data must start with a designated string to be recognised. 185 # or the field data must start with a designated string to be recognised.
174 def getFieldText(inf, field, n = 1, starts = None): 186 def getFieldText(inf, field, n = 1, starts = None):
175 res = "" 187 res = ""
207 if field.upper() == "T": 219 if field.upper() == "T":
208 res = convertTitleToDisplay(res) 220 res = convertTitleToDisplay(res)
209 elif field.upper() == "K": 221 elif field.upper() == "K":
210 res = convertKeyToDisplay(res) 222 res = convertKeyToDisplay(res)
211 elif field.upper() in ["H", "N"]: 223 elif field.upper() in ["H", "N"]:
212 res = convertMarkdown(res, latex) 224 res = convertMarkdown(expandCustomMarkdown(res, latex), latex)
213 return res 225 return res
214 226
215 if __name__ == "__main__": 227 if __name__ == "__main__":
216 def process(inf, options): 228 def process(inf, options):
217 if options.display: 229 if options.display: