Mercurial > dottes
comparison abcfield.py @ 735:68f926e61d16
Revise Markdown handling of character entities.
Ideally I'd like the ABC character entities to survive Markdown and then
get converted. But because they start with '\', they don't. So I have
no alternative but to convert them to HTML entities, which Markdown then
converts to UTF-8.
| author | Jim Hague <jim.hague@acm.org> |
|---|---|
| date | Thu, 12 Oct 2017 13:32:24 +0100 |
| parents | e896cf93fe98 |
| children | 6bd946700312 |
comparison
equal
deleted
inserted
replaced
| 734:e896cf93fe98 | 735:68f926e61d16 |
|---|---|
| 176 res = re.sub(r'href="(.*?).abc"', r'href="\1.html"', res) | 176 res = re.sub(r'href="(.*?).abc"', r'href="\1.html"', res) |
| 177 return res.strip() | 177 return res.strip() |
| 178 | 178 |
| 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): |
| 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 # Because we're going through Markdown, character entities must be | |
| 185 # HTML. Pandoc will convert them to UTF-8. | |
| 184 def getTitleLink(m): | 186 def getTitleLink(m): |
| 185 fname = m.group(1) + ".abc" | 187 fname = m.group(1) + ".abc" |
| 186 path = pathlib.Path(dir, fname) | 188 path = pathlib.Path(dir, fname) |
| 187 with path.open() as f: | 189 with path.open() as f: |
| 188 lines = f.readlines() | 190 lines = f.readlines() |
| 189 return "[" + getFullTitle(lines, dir, latex=latex) + "](" + fname + ")" | 191 return "[" + getFullTitle(lines, dir) + "](" + fname + ")" |
| 190 return re.sub(r'<(.*?).abc>', getTitleLink, t) | 192 return re.sub(r'<(.*?).abc>', getTitleLink, t) |
| 191 | 193 |
| 192 # Return the raw text for a given field. Optionally the nth field is taken, | 194 # Return the raw text for a given field. Optionally the nth field is taken, |
| 193 # or the field data must start with a designated string to be recognised. | 195 # or the field data must start with a designated string to be recognised. |
| 194 def getFieldText(lines, field, n = 1, starts = None): | 196 def getFieldText(lines, field, n = 1, starts = None): |
| 221 | 223 |
| 222 # Return display text for a given field. | 224 # Return display text for a given field. |
| 223 def getFieldDisplayText(lines, dir, field, n = 1, starts = None, latex = False): | 225 def getFieldDisplayText(lines, dir, field, n = 1, starts = None, latex = False): |
| 224 res = getFieldText(lines, field, n, starts) | 226 res = getFieldText(lines, field, n, starts) |
| 225 if res: | 227 if res: |
| 226 res = convertAccents(res, latex) | 228 # Fields that go through Markdown must have HTML entities. |
| 229 mdfield = field.upper() in ['H', 'N']; | |
| 230 res = convertAccents(res, False if mdfield else latex) | |
| 227 if field.upper() == "T": | 231 if field.upper() == "T": |
| 228 res = convertTitleToDisplay(res) | 232 res = convertTitleToDisplay(res) |
| 229 elif field.upper() == "K": | 233 elif field.upper() == "K": |
| 230 res = convertKeyToDisplay(res) | 234 res = convertKeyToDisplay(res) |
| 231 elif field.upper() in ["H", "N"]: | 235 elif mdfield: |
| 232 res = convertMarkdown(expandCustomMarkdown(res, dir, latex), latex) | 236 res = convertMarkdown(expandCustomMarkdown(res, dir), latex) |
| 233 return res | 237 return res |
| 234 | 238 |
| 235 # Return full title (title + [" (" + subtitle + ")"] if subtitle exists). | 239 # Return full title (title + [" (" + subtitle + ")"] if subtitle exists). |
| 236 def getFullTitle(lines, dir, starts = None, latex = False): | 240 def getFullTitle(lines, dir, starts = None, latex = False): |
| 237 title = getFieldDisplayText(lines, dir, "T", starts=starts, latex=latex) | 241 title = getFieldDisplayText(lines, dir, "T", starts=starts, latex=latex) |
