comparison abcfield.py @ 593:82e818c41e81

Give ABC file directory when expanding markdown. <foo.abc> specifies a file in the same directory as the file being processed.
author Jim Hague <jim.hague@acm.org>
date Wed, 02 Nov 2016 14:59:31 +0000
parents 9986c67edf91
children 972d3dab1142
comparison
equal deleted inserted replaced
592:9986c67edf91 593:82e818c41e81
166 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)
167 if latex: 167 if latex:
168 res = re.sub(r'\\href{(.*?).abc}', r'\\hyperlink{\1}', res) 168 res = re.sub(r'\\href{(.*?).abc}', r'\\hyperlink{\1}', res)
169 else: 169 else:
170 res = re.sub(r'href="(.*?).abc"', r'href="\1.html"', res) 170 res = re.sub(r'href="(.*?).abc"', r'href="\1.html"', res)
171 return res 171 return res.strip()
172 172
173 # Implement a custom Markdown shorthand for referencing ABC files. 173 # Implement a custom Markdown shorthand for referencing ABC files.
174 # <foo.abc> will expand to ['title of foo'](foo.abc). 174 # <foo.abc> will expand to ['title of foo'](foo.abc).
175 def expandCustomMarkdown(t, latex): 175 def expandCustomMarkdown(t, dir, latex):
176 # Given a match to (foo.abc), return a markdown link to the tune with the 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. 177 # title of the tune as the text of the link.
178 def getTitle(m): 178 def getTitle(m):
179 fname = m.group(1) + ".abc" 179 fname = m.group(1) + ".abc"
180 with pathlib.Path(fname).open() as f: 180 path = pathlib.Path(dir, fname)
181 return "[" + getFieldDisplayText(f, "T", latex) + "](" + fname + ")" 181 with path.open() as f:
182 return "[" + getFieldDisplayText(f, dir, "T", latex) + "](" + fname + ")"
182 return re.sub(r'<(.*?).abc>', getTitle, t) 183 return re.sub(r'<(.*?).abc>', getTitle, t)
183 184
184 # Return the raw text for a given field. Optionally the nth field is taken, 185 # Return the raw text for a given field. Optionally the nth field is taken,
185 # or the field data must start with a designated string to be recognised. 186 # or the field data must start with a designated string to be recognised.
186 def getFieldText(inf, field, n = 1, starts = None): 187 def getFieldText(inf, field, n = 1, starts = None):
210 continue 211 continue
211 res = line 212 res = line
212 return res 213 return res
213 214
214 # Return display text for a given field. 215 # Return display text for a given field.
215 def getFieldDisplayText(inf, field, n = 1, starts = None, latex = False): 216 def getFieldDisplayText(inf, dir, field, n = 1, starts = None, latex = False):
216 res = getFieldText(inf, field, n, starts) 217 res = getFieldText(inf, field, n, starts)
217 if res: 218 if res:
218 res = convertAccents(res, latex) 219 res = convertAccents(res, latex)
219 if field.upper() == "T": 220 if field.upper() == "T":
220 res = convertTitleToDisplay(res) 221 res = convertTitleToDisplay(res)
221 elif field.upper() == "K": 222 elif field.upper() == "K":
222 res = convertKeyToDisplay(res) 223 res = convertKeyToDisplay(res)
223 elif field.upper() in ["H", "N"]: 224 elif field.upper() in ["H", "N"]:
224 res = convertMarkdown(expandCustomMarkdown(res, latex), latex) 225 res = convertMarkdown(expandCustomMarkdown(res, dir, latex), latex)
225 return res 226 return res
226 227
227 if __name__ == "__main__": 228 if __name__ == "__main__":
228 def process(inf, options): 229 def process(inf, dir, options):
229 if options.display: 230 if options.display:
230 line = getFieldDisplayText(inf, options.field, options.index, options.starts, options.latex) 231 line = getFieldDisplayText(inf, dir, options.field, options.index, options.starts, options.latex)
231 else: 232 else:
232 line = getFieldText(inf, options.field, options.index, options.starts) 233 line = getFieldText(inf, options.field, options.index, options.starts)
233 if line: 234 if line:
234 print(line) 235 print(line)
235 return True 236 return True
258 (options, args) = parser.parse_args() 259 (options, args) = parser.parse_args()
259 260
260 res = False 261 res = False
261 if len(args) > 0: 262 if len(args) > 0:
262 for arg in args: 263 for arg in args:
263 try: 264 path = pathlib.Path(arg)
264 inf = open(arg, "r") 265 with path.open() as f:
265 res = res or process(inf, options) 266 res = res or process(f, path.parent, options)
266 finally: 267 else:
267 inf.close() 268 res = process(sys.stdin, ".", options)
268 else:
269 res = process(sys.stdin, options)
270 sys.exit(int(not res)) 269 sys.exit(int(not res))