Mercurial > dottes
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 590:a6fc091a8c5e | 591:2face6618bf3 |
|---|---|
| 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 re | |
| 22 import subprocess | |
| 21 import sys | 23 import sys |
| 22 | 24 |
| 23 accentedletters = { | 25 accentedletters = { |
| 24 # Acute accents | 26 # Acute accents |
| 25 "'A" : ("Á", "\\'{A}"), | 27 "'A" : ("Á", "\\'{A}"), |
| 150 mode = t[1:] | 152 mode = t[1:] |
| 151 except IndexError: | 153 except IndexError: |
| 152 pass | 154 pass |
| 153 mode = mode.strip().lower() | 155 mode = mode.strip().lower() |
| 154 return letter + accidental + ' ' + abckeys.get(mode, "Major") | 156 return letter + accidental + ' ' + abckeys.get(mode, "Major") |
| 157 | |
| 158 # Convert input string from Markdown to HTML or LaTeX. Fix up link | |
| 159 # targets so any 'foo.abc' target links to the tune with that name. | |
| 160 def convertMarkdown(t, latex): | |
| 161 if latex: | |
| 162 target = "--to=latex" | |
| 163 else: | |
| 164 target = "--to=html" | |
| 165 res = subprocess.check_output(['pandoc', '--from=markdown', target], input=t, universal_newlines=True) | |
| 166 if latex: | |
| 167 res = re.sub(r'\\href{(.*).abc}', r'\\hyperlink{\1}', res) | |
| 168 else: | |
| 169 res = re.sub(r'href="(.*).abc"', r'href="\1.html"', res) | |
| 170 return res | |
| 155 | 171 |
| 156 # Return the raw text for a given field. Optionally the nth field is taken, | 172 # Return the raw text for a given field. Optionally the nth field is taken, |
| 157 # or the field data must start with a designated string to be recognised. | 173 # or the field data must start with a designated string to be recognised. |
| 158 def getFieldText(inf, field, n = 1, starts = None): | 174 def getFieldText(inf, field, n = 1, starts = None): |
| 159 res = "" | 175 res = "" |
| 185 | 201 |
| 186 # Return display text for a given field. | 202 # Return display text for a given field. |
| 187 def getFieldDisplayText(inf, field, n = 1, starts = None, latex = False): | 203 def getFieldDisplayText(inf, field, n = 1, starts = None, latex = False): |
| 188 res = getFieldText(inf, field, n, starts) | 204 res = getFieldText(inf, field, n, starts) |
| 189 if res: | 205 if res: |
| 206 res = convertAccents(res, latex) | |
| 190 if field.upper() == "T": | 207 if field.upper() == "T": |
| 191 res = convertTitleToDisplay(res) | 208 res = convertTitleToDisplay(res) |
| 192 elif field.upper() == "K": | 209 elif field.upper() == "K": |
| 193 res = convertKeyToDisplay(res) | 210 res = convertKeyToDisplay(res) |
| 194 res = convertAccents(res, latex) | 211 elif field.upper() in ["H", "N"]: |
| 212 res = convertMarkdown(res, latex) | |
| 195 return res | 213 return res |
| 196 | 214 |
| 197 if __name__ == "__main__": | 215 if __name__ == "__main__": |
| 198 def process(inf, options): | 216 def process(inf, options): |
| 199 if options.display: | 217 if options.display: |
