GitHub
· Extracted in 1656 ms
👌 Build block line markers once per line, not per character (#449) · executablebooks/markdown-it-py@59256ad
Source: https://github.com/executablebooks/markdown-it-py/commit/59256adff15380d2515038528397e16cb64899c6
Repository navigation
Commit 59256ad
Browse files
John-Cusack
authored
👌 Build block line markers once per line, not per character ( #449 )
StateBlock.__init__ now finds each line break with str.find and scans only
the leading whitespace of each line, instead of dispatching every character.
The markers are identical to the old scan, including its upstream quirk: a
final line of only spaces/tabs with no "\n" gets no marker. A new test pins
the markers.
1 parent 4d7bf9e commit 59256ad
2 files changed
Lines changed: 81 additions & 29 deletions
File tree
-
markdown_it/rules_block
-
tests/test_api
Lines changed: 33 additions & 29 deletions
| Original file line number | Diff line number | Diff line change |
@@ -66,37 +66,41 @@ def __init__(
|
66 | 66 |
|
67 | 67 | # Create caches
|
68 | 68 | # Generate markers.
|
69 | | - indent_found = False
|
70 | | -
|
71 | | - start = pos = indent = offset = 0
|
| 69 | + # NOTE: this loop runs once per line, not once per character:
|
| 70 | + # `str.find` locates each line break at C speed and only leading
|
| 71 | + # whitespace is scanned (usually zero iterations), instead of
|
| 72 | + # dispatching every character through `isStrSpace`. The recorded
|
| 73 | + # markers are identical to the previous per-character scan.
|
| 74 | + start = 0
|
72 | 75 | length = len(self.src)
|
73 | | -
|
74 | | - for pos, character in enumerate(self.src):
|
75 | | - if not indent_found:
|
76 | | - if isStrSpace(character):
|
77 | | - indent += 1
|
78 | | -
|
79 | | - if character == "\t":
|
80 | | - offset += 4 - offset % 4
|
81 | | - else:
|
82 | | - offset += 1
|
83 | | - continue
|
| 76 | + find = self.src.find
|
| 77 | +
|
| 78 | + while start < length:
|
| 79 | + end = find("\n", start)
|
| 80 | + if end < 0:
|
| 81 | + end = length
|
| 82 | + indent = offset = 0
|
| 83 | + pos = start
|
| 84 | + while pos < end:
|
| 85 | + character = self.src[pos]
|
| 86 | + if not isStrSpace(character):
|
| 87 | + break
|
| 88 | + indent += 1
|
| 89 | + if character == "\t":
|
| 90 | + offset += 4 - offset % 4
|
84 | 91 | else:
|
85 | | - indent_found = True
|
86 | | -
|
87 | | - if character == "\n" or pos == length - 1:
|
88 | | - if character != "\n":
|
89 | | - pos += 1
|
90 | | - self.bMarks.append(start)
|
91 | | - self.eMarks.append(pos)
|
92 | | - self.tShift.append(indent)
|
93 | | - self.sCount.append(offset)
|
94 | | - self.bsCount.append(0)
|
95 | | -
|
96 | | - indent_found = False
|
97 | | - indent = 0
|
98 | | - offset = 0
|
99 | | - start = pos + 1
|
| 92 | + offset += 1
|
| 93 | + pos += 1
|
| 94 | + if pos == end == length:
|
| 95 | + # A final line of only spaces/tabs with no "\n" gets no marker,
|
| 96 | + # matching upstream markdown-it's per-character scan.
|
| 97 | + break
|
| 98 | + self.bMarks.append(start)
|
| 99 | + self.eMarks.append(end)
|
| 100 | + self.tShift.append(indent)
|
| 101 | + self.sCount.append(offset)
|
| 102 | + self.bsCount.append(0)
|
| 103 | + start = end + 1 if end < length else length
|
100 | 104 |
|
101 | 105 | # Push fake entry to simplify cache bounds checks
|
102 | 106 | self.bMarks.append(length)
|
|
Lines changed: 48 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
@@ -0,0 +1,48 @@
|
| 1 | +"""Line markers built by ``StateBlock.__init__``.
|
| 2 | +
|
| 3 | +The expected values are those of upstream markdown-it's per-character scan
|
| 4 | +(``lib/rules_block/state_block.mjs``), which the port must reproduce exactly:
|
| 5 | +token ``map`` values and html_block content are derived from them.
|
| 6 | +"""
|
| 7 | +
|
| 8 | +import pytest
|
| 9 | +
|
| 10 | +from markdown_it import MarkdownIt
|
| 11 | +from markdown_it.rules_block.state_block import StateBlock
|
| 12 | +
|
| 13 | +
|
| 14 | +@pytest.mark.parametrize(
|
| 15 | + "src,bmarks,emarks,tshift,scount",
|
| 16 | + [
|
| 17 | + ("", [0], [0], [0], [0]),
|
| 18 | + ("\n", [0, 1], [0, 1], [0, 0], [0, 0]),
|
| 19 | + ("a", [0, 1], [1, 1], [0, 0], [0, 0]),
|
| 20 | + ("a\n", [0, 2], [1, 2], [0, 0], [0, 0]),
|
| 21 | + ("a\nb", [0, 2, 3], [1, 3, 3], [0, 0, 0], [0, 0, 0]),
|
| 22 | + (" a\n\tb\n", [0, 4, 7], [3, 6, 7], [2, 1, 0], [2, 4, 0]),
|
| 23 | + (" \ta", [0, 3], [3, 3], [2, 0], [4, 0]),
|
| 24 | + ("a\n \nb", [0, 2, 5, 6], [1, 4, 6, 6], [0, 2, 0, 0], [0, 2, 0, 0]),
|
| 25 | + # a final line of only spaces/tabs with no "\n" gets no marker (upstream quirk)
|
| 26 | + (" ", [3], [3], [0], [0]),
|
| 27 | + ("\t", [1], [1], [0], [0]),
|
| 28 | + ("a\n ", [0, 4], [1, 4], [0, 0], [0, 0]),
|
| 29 | + ("a\n \t", [0, 4], [1, 4], [0, 0], [0, 0]),
|
| 30 | + ("a\n \n", [0, 2, 5], [1, 4, 5], [0, 2, 0], [0, 2, 0]),
|
| 31 | + ],
|
| 32 | +)
|
| 33 | +def test_line_markers(src, bmarks, emarks, tshift, scount):
|
| 34 | + state = StateBlock(src, MarkdownIt(), {}, [])
|
| 35 | + assert state.bMarks == bmarks
|
| 36 | + assert state.eMarks == emarks
|
| 37 | + assert state.tShift == tshift
|
| 38 | + assert state.sCount == scount
|
| 39 | + assert state.bsCount == [0] * len(bmarks)
|
| 40 | + assert state.lineMax == len(bmarks) - 1
|
| 41 | +
|
| 42 | +
|
| 43 | +def test_trailing_whitespace_line_not_in_html_block():
|
| 44 | + """Upstream markdown-it leaves a final whitespace-only line out of the block."""
|
| 45 | + md = MarkdownIt("commonmark")
|
| 46 | + assert md.render("<style>\n\nfoo\n ") == "<style>\n\nfoo\n"
|
| 47 | + tokens = md.parse("- a\n ")
|
| 48 | + assert tokens[0].map == [0, 1]
|
|
|