GitHub
· Extracted in 1758 ms
🐛 FIX: Apply a generator of rule names to every chain in enable/disab... · executablebooks/markdown-it-py@a852307
Source: https://github.com/executablebooks/markdown-it-py/commit/a8523074950ecddefe2b4542b3fcf318d96c987d
Repository navigation
Commit a852307
Browse files
muneikh
authored
🐛 FIX: Apply a generator of rule names to every chain in enable/disable ( #447 )
`MarkdownIt.enable()` and `disable()` accept `str | Iterable[str]`, but iterated
`names` once per rule chain and again for unknown-name validation. A one-shot
iterable (a generator, `iter(...)`, `map(...)`) was exhausted by the core chain,
so block and inline rules were silently not toggled and unknown names were never
reported. The names are now materialised once, so a generator behaves like a list,
including raising `ValueError` for an unknown name.
1 parent 2028cc2 commit a852307
2 files changed
Lines changed: 37 additions & 7 deletions
File tree
-
markdown_it
-
tests/test_api
Lines changed: 5 additions & 7 deletions
| Original file line number | Diff line number | Diff line change |
@@ -160,9 +160,9 @@ def get_active_rules(self) -> dict[str, list[str]]:
|
160 | 160 | def enable(
|
161 | 161 | self, names: str | Iterable[str], ignoreInvalid: bool = False
|
162 | 162 | ) -> MarkdownIt:
|
163 | | - """Enable list or rules. (chainable)
|
| 163 | + """Enable list of rules. (chainable)
|
164 | 164 |
|
165 | | - :param names: rule name or list of rule names to enable.
|
| 165 | + :param names: rule name or iterable of rule names to enable.
|
166 | 166 | :param ignoreInvalid: set `true` to ignore errors when rule not found.
|
167 | 167 |
|
168 | 168 | It will automatically find appropriate components,
|
@@ -176,8 +176,7 @@ def enable(
|
176 | 176 | """
|
177 | 177 | result = []
|
178 | 178 |
|
179 | | - if isinstance(names, str):
|
180 | | - names = [names]
|
| 179 | + names = [names] if isinstance(names, str) else list(names)
|
181 | 180 |
|
182 | 181 | for chain in ["core", "block", "inline"]:
|
183 | 182 | result.extend(self[chain].ruler.enable(names, True))
|
@@ -194,14 +193,13 @@ def disable(
|
194 | 193 | ) -> MarkdownIt:
|
195 | 194 | """The same as [[MarkdownIt.enable]], but turn specified rules off. (chainable)
|
196 | 195 |
|
197 | | - :param names: rule name or list of rule names to disable.
|
| 196 | + :param names: rule name or iterable of rule names to disable.
|
198 | 197 | :param ignoreInvalid: set `true` to ignore errors when rule not found.
|
199 | 198 |
|
200 | 199 | """
|
201 | 200 | result = []
|
202 | 201 |
|
203 | | - if isinstance(names, str):
|
204 | | - names = [names]
|
| 202 | + names = [names] if isinstance(names, str) else list(names)
|
205 | 203 |
|
206 | 204 | for chain in ["core", "block", "inline"]:
|
207 | 205 | result.extend(self[chain].ruler.disable(names, True))
|
|
Lines changed: 32 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
@@ -589,3 +589,35 @@ def peek_rule(state, silent):
|
589 | 589 | # the rule saw the text accumulate one character at a time
|
590 | 590 | assert seen[:4] == [0, 1, 2, 3]
|
591 | 591 | assert max(seen) == 9_999
|
| 592 | +
|
| 593 | +
|
| 594 | +@pytest.mark.parametrize("method", ["enable", "disable"])
|
| 595 | +def test_rule_names_generator_applies_to_all_chains(method):
|
| 596 | + """An iterator must be reused across each rule chain."""
|
| 597 | + md = MarkdownIt("zero" if method == "enable" else "commonmark")
|
| 598 | + core_rule = "smartquotes" if method == "enable" else "normalize"
|
| 599 | + names = (name for name in [core_rule, "heading", "emphasis"])
|
| 600 | + assert getattr(md, method)(names) is md
|
| 601 | + active = md.get_active_rules()
|
| 602 | + for chain, name in [
|
| 603 | + ("core", core_rule),
|
| 604 | + ("block", "heading"),
|
| 605 | + ("inline", "emphasis"),
|
| 606 | + ("inline2", "emphasis"),
|
| 607 | + ]:
|
| 608 | + assert (name in active[chain]) is (method == "enable")
|
| 609 | +
|
| 610 | +
|
| 611 | +@pytest.mark.parametrize("method", ["enable", "disable"])
|
| 612 | +def test_rule_names_generator_reports_unknown_rules(method):
|
| 613 | + """Unknown names are checked after all chains have inspected an iterator."""
|
| 614 | + with pytest.raises(ValueError, match="unknown rule"):
|
| 615 | + getattr(MarkdownIt(), method)(name for name in ["missing-rule"])
|
| 616 | +
|
| 617 | +
|
| 618 | +@pytest.mark.parametrize("method", ["enable", "disable"])
|
| 619 | +def test_rule_names_generator_ignores_unknown_rules(method):
|
| 620 | + """Ignoring an unknown name must still apply the known iterator names."""
|
| 621 | + md = MarkdownIt("zero" if method == "enable" else "commonmark")
|
| 622 | + getattr(md, method)((name for name in ["missing-rule", "heading"]), True)
|
| 623 | + assert ("heading" in md.get_active_rules()["block"]) is (method == "enable")
|
|
|