Skip to content

Commit 85f7ed3

Browse files
authored
👌 Centralise code block test (#83)
All block level rules first test whether the first character is indented enough to be a code block. This logic is centralised, and allows for the `StateBlock.is_code_block` method, coming in markdown-it-py v3.0.0
1 parent 875bbe5 commit 85f7ed3

File tree

13 files changed

+73
-38
lines changed

13 files changed

+73
-38
lines changed

‎mdit_py_plugins/admon/index.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from markdown_it import MarkdownIt
66
from markdown_it.rules_block import StateBlock
77

8+
from mdit_py_plugins.utils import is_code_block
9+
810

911
def _get_tag(params: str) -> Tuple[str, str]:
1012
"""Separate the tag name from the admonition title."""
@@ -44,6 +46,9 @@ def _extra_classes(markup: str) -> List[str]:
4446

4547

4648
def admonition(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool:
49+
if is_code_block(state, startLine):
50+
return False
51+
4752
start = state.bMarks[startLine] + state.tShift[startLine]
4853
maximum = state.eMarks[startLine]
4954

‎mdit_py_plugins/amsmath/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from markdown_it.common.utils import escapeHtml
77
from markdown_it.rules_block import StateBlock
88

9+
from mdit_py_plugins.utils import is_code_block
10+
911
# Taken from amsmath version 2.1
1012
# http://anorien.csc.warwick.ac.uk/mirrors/CTAN/macros/latex/required/amsmath/amsldoc.pdf
1113
ENVIRONMENTS = [
@@ -92,8 +94,7 @@ def match_environment(string):
9294

9395

9496
def amsmath_block(state: StateBlock, startLine: int, endLine: int, silent: bool):
95-
# if it's indented more than 3 spaces, it should be a code block
96-
if state.sCount[startLine] - state.blkIndent >= 4:
97+
if is_code_block(state, startLine):
9798
return False
9899

99100
begin = state.bMarks[startLine] + state.tShift[startLine]

‎mdit_py_plugins/attrs/index.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from markdown_it.rules_inline import StateInline
88
from markdown_it.token import Token
99

10+
from mdit_py_plugins.utils import is_code_block
11+
1012
from .parse import ParseError, parse
1113

1214

@@ -156,8 +158,7 @@ def _attr_block_rule(
156158
The block must be a single line that begins with a `{`, after three or less spaces,
157159
and end with a `}` followed by any number if spaces.
158160
"""
159-
# if it's indented more than 3 spaces, it should be a code block
160-
if state.sCount[startLine] - state.blkIndent >= 4:
161+
if is_code_block(state, startLine):
161162
return False
162163

163164
pos = state.bMarks[startLine] + state.tShift[startLine]

‎mdit_py_plugins/colon_fence.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from markdown_it.common.utils import escapeHtml, unescapeAll
33
from markdown_it.rules_block import StateBlock
44

5+
from mdit_py_plugins.utils import is_code_block
6+
57

68
def colon_fence_plugin(md: MarkdownIt):
79
"""This plugin directly mimics regular fences, but with `:` colons.
@@ -24,14 +26,13 @@ def colon_fence_plugin(md: MarkdownIt):
2426

2527

2628
def _rule(state: StateBlock, startLine: int, endLine: int, silent: bool):
29+
if is_code_block(state, startLine):
30+
return False
31+
2732
haveEndMarker = False
2833
pos = state.bMarks[startLine] + state.tShift[startLine]
2934
maximum = state.eMarks[startLine]
3035

31-
# if it's indented more than 3 spaces, it should be a code block
32-
if state.sCount[startLine] - state.blkIndent >= 4:
33-
return False
34-
3536
if pos + 3 > maximum:
3637
return False
3738

@@ -79,8 +80,7 @@ def _rule(state: StateBlock, startLine: int, endLine: int, silent: bool):
7980
if state.srcCharCode[pos] != marker:
8081
continue
8182

82-
if state.sCount[nextLine] - state.blkIndent >= 4:
83-
# closing fence should be indented less than 4 spaces
83+
if is_code_block(state, nextLine):
8484
continue
8585

8686
pos = state.skipChars(pos, marker)

‎mdit_py_plugins/container/index.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from markdown_it.common.utils import charCodeAt
77
from markdown_it.rules_block import StateBlock
88

9+
from mdit_py_plugins.utils import is_code_block
10+
911

1012
def container_plugin(
1113
md: MarkdownIt,
@@ -52,6 +54,9 @@ def renderDefault(self, tokens, idx, _options, env):
5254
render = render or renderDefault
5355

5456
def container_func(state: StateBlock, startLine: int, endLine: int, silent: bool):
57+
if is_code_block(state, startLine):
58+
return False
59+
5560
auto_closed = False
5661
start = state.bMarks[startLine] + state.tShift[startLine]
5762
maximum = state.eMarks[startLine]
@@ -109,8 +114,7 @@ def container_func(state: StateBlock, startLine: int, endLine: int, silent: bool
109114
if marker_char != state.srcCharCode[start]:
110115
continue
111116

112-
if state.sCount[nextLine] - state.blkIndent >= 4:
113-
# closing fence should be indented less than 4 spaces
117+
if is_code_block(state, nextLine):
114118
continue
115119

116120
pos = start + 1

‎mdit_py_plugins/deflist/index.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from markdown_it import MarkdownIt
33
from markdown_it.rules_block import StateBlock
44

5+
from mdit_py_plugins.utils import is_code_block
6+
57

68
def deflist_plugin(md: MarkdownIt):
79
"""Plugin ported from
@@ -66,6 +68,9 @@ def markTightParagraphs(state: StateBlock, idx: int):
6668
i += 1
6769

6870
def deflist(state: StateBlock, startLine: int, endLine: int, silent: bool):
71+
if is_code_block(state, startLine):
72+
return False
73+
6974
if silent:
7075
# quirk: validation mode validates a dd block only, not a whole deflist
7176
if state.ddIndent < 0:

‎mdit_py_plugins/dollarmath/index.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from markdown_it.rules_block import StateBlock
77
from markdown_it.rules_inline import StateInline
88

9+
from mdit_py_plugins.utils import is_code_block
10+
911

1012
def dollarmath_plugin(
1113
md: MarkdownIt,
@@ -262,14 +264,13 @@ def _math_block_dollar(
262264
) -> bool:
263265
# TODO internal backslash escaping
264266

267+
if is_code_block(state, startLine):
268+
return False
269+
265270
haveEndMarker = False
266271
startPos = state.bMarks[startLine] + state.tShift[startLine]
267272
end = state.eMarks[startLine]
268273

269-
# if it's indented more than 3 spaces, it should be a code block
270-
if state.sCount[startLine] - state.blkIndent >= 4:
271-
return False
272-
273274
if startPos + 2 > end:
274275
return False
275276

‎mdit_py_plugins/field_list/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from markdown_it import MarkdownIt
66
from markdown_it.rules_block import StateBlock
77

8+
from mdit_py_plugins.utils import is_code_block
9+
810

911
def fieldlist_plugin(md: MarkdownIt):
1012
"""Field lists are mappings from field names to field bodies, based on the
@@ -96,8 +98,7 @@ def set_parent_type(state: StateBlock, name: str):
9698
def _fieldlist_rule(state: StateBlock, startLine: int, endLine: int, silent: bool):
9799
# adapted from markdown_it/rules_block/list.py::list_block
98100

99-
# if it's indented more than 3 spaces, it should be a code block
100-
if state.sCount[startLine] - state.blkIndent >= 4:
101+
if is_code_block(state, startLine):
101102
return False
102103

103104
posAfterName, name_text = parseNameMarker(state, startLine)
@@ -221,8 +222,7 @@ def _fieldlist_rule(state: StateBlock, startLine: int, endLine: int, silent: boo
221222
if state.sCount[nextLine] < state.blkIndent:
222223
break
223224

224-
# if it's indented more than 3 spaces, it should be a code block
225-
if state.sCount[startLine] - state.blkIndent >= 4:
225+
if is_code_block(state, startLine):
226226
break
227227

228228
# get next field item

‎mdit_py_plugins/footnote/index.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from markdown_it.rules_inline import StateInline
1111
from markdown_it.token import Token
1212

13+
from mdit_py_plugins.utils import is_code_block
14+
1315

1416
def footnote_plugin(md: MarkdownIt):
1517
"""Plugin ported from
@@ -57,6 +59,9 @@ def footnote_plugin(md: MarkdownIt):
5759
def footnote_def(state: StateBlock, startLine: int, endLine: int, silent: bool):
5860
"""Process footnote block definition"""
5961

62+
if is_code_block(state, startLine):
63+
return False
64+
6065
start = state.bMarks[startLine] + state.tShift[startLine]
6166
maximum = state.eMarks[startLine]
6267

‎mdit_py_plugins/front_matter/index.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from markdown_it.common.utils import charCodeAt
66
from markdown_it.rules_block import StateBlock
77

8+
from mdit_py_plugins.utils import is_code_block
9+
810

911
def front_matter_plugin(md: MarkdownIt):
1012
"""Plugin ported from
@@ -88,8 +90,7 @@ def frontMatter(state: StateBlock, startLine: int, endLine: int, silent: bool):
8890
if marker_char != state.srcCharCode[start]:
8991
continue
9092

91-
if state.sCount[nextLine] - state.blkIndent >= 4:
92-
# closing fence should be indented less than 4 spaces
93+
if is_code_block(state, nextLine):
9394
continue
9495

9596
pos = start + 1

0 commit comments

Comments
 (0)