Skip to content

Commit d37ff04

Browse files
committed
Simplify the heading attribute pattern
Matches a heading's trailing attribute blocks with a pattern that reads each block in one pass; the previous form could take a very long time on a heading carrying many blocks followed by other text. Parsed results are unchanged. No-Verification-Needed: one regular expression rewritten with identical results, covered by a new unit test
1 parent 4c0a8bb commit d37ff04

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

scripts/docs/translations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class PageError(Exception):
7373
# A heading's trailing attr_list block(s), matched with or without the whitespace
7474
# attr_list itself needs, so blocks the model glued to CJK text or doubled are
7575
# still seen; `body` is the last block's, the one attr_list reads.
76-
HEADING_ATTRS = re.compile(r"(?:[ \t]*\{:?[ \t]*(?P<body>[^}\n]*?)[ \t]*\})+[ \t]*$")
76+
HEADING_ATTRS = re.compile(r"(?:[ \t]*\{:?(?P<body>[^}\n]*)\})+[ \t]*$")
7777
ESCAPE = re.compile(r"\\(?P<char>[!\"#$%&'()*+,\-./:;<=>?@\[\\\]^_`{|}~])")
7878
# An underscore that is not word-internal turns into emphasis before attr_list
7979
# reads the block, so it must be written escaped inside `{#...}`.

tests/docs/test_translations.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,18 @@ def test_heading_the_source_scan_cannot_pin_makes_the_page_an_error(tmp_path: Pa
292292
assert str(excinfo.value) == snapshot("the page renders 2 headings but 1 are ATX headings at column 0")
293293

294294

295+
def test_many_attribute_blocks_pin_the_last_id_only_when_they_end_the_heading() -> None:
296+
"""Tool-defined: a run of `{...}` blocks ending the line pins the last block's id; with text after it the
297+
run stays heading text and pins nothing, and the scan still returns promptly however many blocks it has."""
298+
blocks = "{ a } " * 20
299+
page = f"## Pinned {blocks}{{#last}}\n## Prose {blocks}end\n"
300+
301+
assert t.parse_headings(page) == [
302+
t.Heading(0, 2, "Pinned", "last"),
303+
t.Heading(1, 2, f"Prose {blocks}end", None),
304+
]
305+
306+
295307
ENGLISH = """\
296308
# Guide
297309

0 commit comments

Comments
 (0)