Skip to content

Commit aa3e31d

Browse files
committed
implement: Freeze a docx without the bracketed spans the PDF generator cannot compile (t58)
1 parent baead62 commit aa3e31d

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

‎in2lambda/source/__init__.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,16 @@ def blocked(position: int) -> bool:
428428
return "".join(written)
429429

430430

431-
_SPAN = re.compile(r"\[([^\[\]\n]*)\]\{[^{}\n]*\}")
431+
_ATTRIBUTE = r"""[.#][^\s{}]+|[\w-]+=(?:"[^"\n]*"|[^\s{}]+)"""
432+
"""One attribute of a pandoc attribute list: a class, an id, or a key and its value."""
433+
434+
_SPAN = re.compile(rf"\[([^\[\]\n]*)\]\{{(?:{_ATTRIBUTE})(?: +(?:{_ATTRIBUTE}))*\}}")
432435
"""A bracketed span as ``commonmark_x`` writes one, opened and closed on the one line.
433436
434437
The ``]{`` is what tells one from a link's ``](`` and from an image's ``){width=...}``.
438+
The braces must hold an attribute list, because LaTeX writes brackets before braces as
439+
well: ``$\\sqrt[3]{x + 1}$`` is a cube root, and dropping its braces would leave
440+
``$\\sqrt3$``, which KaTeX renders and no check reports.
435441
"""
436442

437443

@@ -471,6 +477,8 @@ def _spans_unwrapped(markdown: str) -> str:
471477
'::: {.solution}\nThe load is $F = pA$.\n:::\n'
472478
>>> _spans_unwrapped('![](figure.png){width="1in"}\n')
473479
'![](figure.png){width="1in"}\n'
480+
>>> _spans_unwrapped("The root is $\\sqrt[3]{x + 1}$.\n")
481+
'The root is $\\sqrt[3]{x + 1}$.\n'
474482
"""
475483
if "\r\n" in markdown:
476484
# Pandoc writes the line endings of whoever is running it, and the file on disk

‎tests/fixtures/sources/bracketed_spans/make_source.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"""
1111

1212
if __name__ == "__main__":
13+
from pathlib import Path
14+
1315
from docx import Document
1416
from docx.enum.text import WD_COLOR_INDEX
1517

@@ -30,4 +32,5 @@
3032
note.add_run("Note: ").font.small_caps = True
3133
note.add_run("the oil is incompressible.")
3234

33-
document.save("source.docx")
35+
# Beside this file, so that the fixture is written wherever the script is run from.
36+
document.save(Path(__file__).with_name("source.docx"))

0 commit comments

Comments
 (0)