Skip to content

Commit 935efa1

Browse files
committed
Add myst_line_comment
1 parent 4f8c8e0 commit 935efa1

File tree

5 files changed

+117
-42
lines changed

5 files changed

+117
-42
lines changed

markdown_it/common/utils.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -228,38 +228,38 @@ def isPunctChar(ch):
228228

229229

230230
MD_ASCII_PUNCT = {
231-
0x21, # /* ! */:
232-
0x22, # /* " */:
233-
0x23, # /* # */:
234-
0x24, # /* $ */:
235-
0x25, # /* % */:
236-
0x26, # /* & */:
237-
0x27, # /* ' */:
238-
0x28, # /* ( */:
239-
0x29, # /* ) */:
240-
0x2A, # /* * */:
241-
0x2B, # /* + */:
242-
0x2C, # /* , */:
243-
0x2D, # /* - */:
244-
0x2E, # /* . */:
245-
0x2F, # /* / */:
246-
0x3A, # /* : */:
247-
0x3B, # /* ; */:
248-
0x3C, # /* < */:
249-
0x3D, # /* = */:
250-
0x3E, # /* > */:
251-
0x3F, # /* ? */:
252-
0x40, # /* @ */:
253-
0x5B, # /* [ */:
254-
0x5C, # /* \ */:
255-
0x5D, # /* ] */:
256-
0x5E, # /* ^ */:
257-
0x5F, # /* _ */:
258-
0x60, # /* ` */:
259-
0x7B, # /* { */:
260-
0x7C, # /* | */:
261-
0x7D, # /* } */:
262-
0x7E, # /* ~ */:
231+
0x21, # /* ! */
232+
0x22, # /* " */
233+
0x23, # /* # */
234+
0x24, # /* $ */
235+
0x25, # /* % */
236+
0x26, # /* & */
237+
0x27, # /* ' */
238+
0x28, # /* ( */
239+
0x29, # /* ) */
240+
0x2A, # /* * */
241+
0x2B, # /* + */
242+
0x2C, # /* , */
243+
0x2D, # /* - */
244+
0x2E, # /* . */
245+
0x2F, # /* / */
246+
0x3A, # /* : */
247+
0x3B, # /* ; */
248+
0x3C, # /* < */
249+
0x3D, # /* = */
250+
0x3E, # /* > */
251+
0x3F, # /* ? */
252+
0x40, # /* @ */
253+
0x5B, # /* [ */
254+
0x5C, # /* \ */
255+
0x5D, # /* ] */
256+
0x5E, # /* ^ */
257+
0x5F, # /* _ */
258+
0x60, # /* ` */
259+
0x7B, # /* { */
260+
0x7C, # /* | */
261+
0x7D, # /* } */
262+
0x7E, # /* ~ */
263263
}
264264

265265

markdown_it/extensions/myst_blocks/index.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
from markdown_it import MarkdownIt
44
from markdown_it.rules_block import StateBlock
5-
from markdown_it.common.utils import charCodeAt, isSpace
5+
from markdown_it.common.utils import charCodeAt, isSpace, escapeHtml
66

77

88
TARGET_PATTERN = re.compile(r"^\(([a-zA-Z\_\-\+\:]{1,36})\)\=\s*$")
99

1010

1111
def myst_block_plugin(md: MarkdownIt):
12+
md.block.ruler.before(
13+
"blockquote",
14+
"myst_line_comment",
15+
line_comment,
16+
{"alt": ["paragraph", "reference", "blockquote", "list", "footnote_def"]},
17+
)
1218
md.block.ruler.before(
1319
"hr",
1420
"myst_block_break",
@@ -22,6 +28,37 @@ def myst_block_plugin(md: MarkdownIt):
2228
{"alt": ["paragraph", "reference", "blockquote", "list", "footnote_def"]},
2329
)
2430
md.add_render_rule("myst_target", render_myst_target)
31+
md.add_render_rule("myst_line_comment", render_myst_line_comment)
32+
33+
34+
def line_comment(state: StateBlock, startLine: int, endLine: int, silent: bool):
35+
36+
pos = state.bMarks[startLine] + state.tShift[startLine]
37+
maximum = state.eMarks[startLine]
38+
39+
# if it's indented more than 3 spaces, it should be a code block
40+
if state.sCount[startLine] - state.blkIndent >= 4:
41+
return False
42+
43+
marker = charCodeAt(state.src, pos)
44+
pos += 1
45+
46+
# Check block marker /* % */
47+
if marker != 0x25:
48+
return False
49+
50+
if silent:
51+
return True
52+
53+
state.line = startLine + 1
54+
55+
token = state.push("myst_line_comment", "", 0)
56+
token.attrSet("class", "myst-line-comment")
57+
token.content = state.src[pos:maximum].strip()
58+
token.map = [startLine, state.line]
59+
token.markup = chr(marker)
60+
61+
return True
2562

2663

2764
def block_break(state: StateBlock, startLine: int, endLine: int, silent: bool):
@@ -36,7 +73,7 @@ def block_break(state: StateBlock, startLine: int, endLine: int, silent: bool):
3673
marker = charCodeAt(state.src, pos)
3774
pos += 1
3875

39-
# Check block marker /* + */:
76+
# Check block marker /* + */
4077
if marker != 0x2B:
4178
return False
4279

@@ -96,4 +133,12 @@ def target(state: StateBlock, startLine: int, endLine: int, silent: bool):
96133

97134
def render_myst_target(self, tokens, idx, options, env):
98135
content = tokens[idx].content
99-
return f'<div class=" admonition myst-target">target = <code>{content}</code></div>'
136+
return (
137+
'<div class=" admonition myst-target">'
138+
f"target = <code>{escapeHtml(content)}</code></div>"
139+
)
140+
141+
142+
def render_myst_line_comment(self, tokens, idx, options, env):
143+
content = tokens[idx].content
144+
return f"<!--- {escapeHtml(content)} --->"

markdown_it/extensions/myst_role/index.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from markdown_it import MarkdownIt
44
from markdown_it.rules_inline import StateInline
5-
from markdown_it.common.utils import charCodeAt
5+
from markdown_it.common.utils import charCodeAt, escapeHtml
66

77

88
PATTERN = re.compile(r"^\{([a-zA-Z\_\-\+\:]{1,36})\}(`+)(?!`)(.+?)(?<!`)\2(?!`)")
@@ -37,4 +37,8 @@ def myst_role(state: StateInline, silent: bool):
3737
def render_myst_role(self, tokens, idx, options, env):
3838
token = tokens[idx]
3939
name = token.meta.get("name", "unknown")
40-
return '<code class="sphinx-role">' f"{{{name}}}[{token.content}]" "</code>"
40+
return (
41+
'<code class="sphinx-role">'
42+
f"{{{name}}}[{escapeHtml(token.content)}]"
43+
"</code>"
44+
)

markdown_it/rules_core/state_core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66

77
class StateCore(StateBase):
8-
def __init__(self, src: str, md, env):
8+
def __init__(self, src: str, md, env, tokens=None):
99
self.src = src
1010
self.md = md # link to parser instance
1111
self.env = env
12-
self.tokens: List[Token] = []
12+
self.tokens: List[Token] = tokens or []
1313
self.inlineMode = False

tests/test_myst_block/fixtures.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Block Break Too Few Markers:
2020
<p>++</p>
2121
.
2222

23-
Terminates other blocks:
23+
Block Break terminates other blocks:
2424
.
2525
a
2626
+++
@@ -50,7 +50,7 @@ Target:
5050
.
5151

5252

53-
Terminates other blocks:
53+
Target terminates other blocks:
5454
.
5555
a
5656
(a)=
@@ -67,4 +67,30 @@ a
6767
<p>c</p>
6868
</blockquote>
6969
<div class=" admonition myst-target">target = <code>a</code></div>
70-
.
70+
.
71+
72+
Comment:
73+
.
74+
% abc
75+
.
76+
<!--- abc --->
77+
.
78+
79+
Comment terminates other blocks:
80+
.
81+
a
82+
% abc
83+
- b
84+
% abc
85+
> c
86+
% abc
87+
.
88+
<p>a</p>
89+
<!--- abc ---><ul>
90+
<li>b</li>
91+
</ul>
92+
<!--- abc ---><blockquote>
93+
<p>c</p>
94+
</blockquote>
95+
<!--- abc --->
96+
.

0 commit comments

Comments
 (0)