diff abcfield.py @ 591:2face6618bf3

Convert Markdown in N and H fields. Fix up .abc links.
author Jim Hague <jim.hague@acm.org>
date Wed, 02 Nov 2016 08:36:21 +0000
parents afc031477784
children 9986c67edf91
line wrap: on
line diff
--- a/abcfield.py	Wed Nov 02 01:12:42 2016 +0000
+++ b/abcfield.py	Wed Nov 02 08:36:21 2016 +0000
@@ -18,6 +18,8 @@
 #
 
 import optparse
+import re
+import subprocess
 import sys
 
 accentedletters = {
@@ -153,6 +155,20 @@
     mode = mode.strip().lower()
     return letter + accidental + ' ' + abckeys.get(mode, "Major")
 
+# Convert input string from Markdown to HTML or LaTeX. Fix up link
+# targets so any 'foo.abc' target links to the tune with that name.
+def convertMarkdown(t, latex):
+    if latex:
+        target = "--to=latex"
+    else:
+        target = "--to=html"
+    res = subprocess.check_output(['pandoc', '--from=markdown', target], input=t, universal_newlines=True)
+    if latex:
+        res = re.sub(r'\\href{(.*).abc}', r'\\hyperlink{\1}', res)
+    else:
+        res = re.sub(r'href="(.*).abc"', r'href="\1.html"', res)
+    return res
+
 # 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):
@@ -187,11 +203,13 @@
 def getFieldDisplayText(inf, field, n = 1, starts = None, latex = False):
     res = getFieldText(inf, field, n, starts)
     if res:
+        res = convertAccents(res, latex)
         if field.upper() == "T":
             res = convertTitleToDisplay(res)
         elif field.upper() == "K":
             res = convertKeyToDisplay(res)
-        res = convertAccents(res, latex)
+        elif field.upper() in ["H", "N"]:
+            res = convertMarkdown(res, latex)
     return res
 
 if __name__ == "__main__":