-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathindex.py
More file actions
154 lines (114 loc) · 4.28 KB
/
index.py
File metadata and controls
154 lines (114 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import itertools
from markdown_it import MarkdownIt
from markdown_it.common.utils import escapeHtml, isSpace
from markdown_it.rules_block import StateBlock
def myst_block_plugin(md: MarkdownIt):
"""Parse MyST targets (``(name)=``), blockquotes (``% comment``) and block breaks (``+++``)."""
md.block.ruler.before(
"blockquote",
"myst_line_comment",
line_comment,
{"alt": ["paragraph", "reference", "blockquote", "list", "footnote_def"]},
)
md.block.ruler.before(
"hr",
"myst_block_break",
block_break,
{"alt": ["paragraph", "reference", "blockquote", "list", "footnote_def"]},
)
md.block.ruler.before(
"hr",
"myst_target",
target,
{"alt": ["paragraph", "reference", "blockquote", "list", "footnote_def"]},
)
md.add_render_rule("myst_target", render_myst_target)
md.add_render_rule("myst_line_comment", render_myst_line_comment)
def line_comment(state: StateBlock, startLine: int, endLine: int, silent: bool):
pos = state.bMarks[startLine] + state.tShift[startLine]
maximum = state.eMarks[startLine]
# if it's indented more than 3 spaces, it should be a code block
if state.sCount[startLine] - state.blkIndent >= 4:
return False
if state.src[pos] != "%":
return False
if silent:
return True
token = state.push("myst_line_comment", "", 0)
token.attrSet("class", "myst-line-comment")
token.content = state.src[pos + 1 : maximum].rstrip()
token.markup = "%"
# search end of block while appending lines to `token.content`
for nextLine in itertools.count(startLine + 1):
if nextLine >= endLine:
break
pos = state.bMarks[nextLine] + state.tShift[nextLine]
maximum = state.eMarks[nextLine]
if state.src[pos] != "%":
break
token.content += "\n" + state.src[pos + 1 : maximum].rstrip()
state.line = nextLine
token.map = [startLine, nextLine]
return True
def block_break(state: StateBlock, startLine: int, endLine: int, silent: bool):
pos = state.bMarks[startLine] + state.tShift[startLine]
maximum = state.eMarks[startLine]
# if it's indented more than 3 spaces, it should be a code block
if state.sCount[startLine] - state.blkIndent >= 4:
return False
marker = state.srcCharCode[pos]
pos += 1
# Check block marker /* + */
if marker != 0x2B:
return False
# markers can be mixed with spaces, but there should be at least 3 of them
cnt = 1
while pos < maximum:
ch = state.srcCharCode[pos]
if ch != marker and not isSpace(ch):
break
if ch == marker:
cnt += 1
pos += 1
if cnt < 3:
return False
if silent:
return True
state.line = startLine + 1
token = state.push("myst_block_break", "hr", 0)
token.attrSet("class", "myst-block")
token.content = state.src[pos:maximum].strip()
token.map = [startLine, state.line]
token.markup = chr(marker) * cnt
return True
def target(state: StateBlock, startLine: int, endLine: int, silent: bool):
pos = state.bMarks[startLine] + state.tShift[startLine]
maximum = state.eMarks[startLine]
# if it's indented more than 3 spaces, it should be a code block
if state.sCount[startLine] - state.blkIndent >= 4:
return False
text = state.src[pos:maximum].strip()
if not text.startswith("("):
return False
if not text.endswith(")="):
return False
content = text[1:-2]
if not content or "\\" in content:
return False
if silent:
return True
state.line = startLine + 1
token = state.push("myst_target", "", 0)
token.attrSet("class", "myst-target")
token.content = content
token.map = [startLine, state.line]
return True
def render_myst_target(self, tokens, idx, options, env):
label = tokens[idx].content
class_name = "myst-target"
target = f'<a href="#{label}">({label})=</a>'
return f'<div class="{class_name}">{target}</div>'
def render_myst_line_comment(self, tokens, idx, options, env):
# Strip leading whitespace from all lines
content = "\n".join(line.lstrip() for line in tokens[idx].content.split("\n"))
return f"<!-- {escapeHtml(content)} -->"