Skip to content

Commit 49b3aaa

Browse files
committed
Update math tests and only allow matches starting from beginning position
1 parent a76e350 commit 49b3aaa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+878
-2122
lines changed

markdown_it/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .main import MarkdownIt # noqa: F401
22

33

4-
__version__ = "0.3.1"
4+
__version__ = "0.3.2"

markdown_it/extensions/texmath/index.py

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,28 @@ def render_math_block(self, tokens, idx, options, env):
3535
md.add_render_rule(rule_block["name"], render_math_block)
3636

3737

38-
def applyRule(rule, string: str, beg, inBlockquote):
39-
40-
pre = string.startswith(rule["tag"], beg) and (
41-
rule["pre"](string, beg) if "pre" in rule else True
42-
)
43-
44-
match = rule["rex"].search(string, beg) if pre else False
45-
post = True
46-
if match:
47-
lastIndex = match.end() - 1
48-
if "post" in rule:
49-
post = (
50-
rule["post"](string, lastIndex) # valid post-condition
51-
# remove evil blockquote bug (https:#github.com/goessner/mdmath/issues/50)
52-
and (not inBlockquote or "\n" not in match.group(1))
53-
)
38+
def applyRule(rule, string: str, begin, inBlockquote):
39+
40+
if not (
41+
string.startswith(rule["tag"], begin)
42+
and (rule["pre"](string, begin) if "pre" in rule else True)
43+
):
44+
return False
45+
46+
match = rule["rex"].match(string[begin:]) # type: re.Match
47+
48+
if not match or match.start() != 0:
49+
return False
5450

55-
return post and match
51+
lastIndex = match.end() + begin - 1
52+
if "post" in rule:
53+
if not (
54+
rule["post"](string, lastIndex) # valid post-condition
55+
# remove evil blockquote bug (https:#github.com/goessner/mdmath/issues/50)
56+
and (not inBlockquote or "\n" not in match.group(1))
57+
):
58+
return False
59+
return match
5660

5761

5862
def make_inline_func(rule):
@@ -64,7 +68,7 @@ def _func(state, silent):
6468
token.content = res[1] # group 1 from regex ..
6569
token.markup = rule["tag"]
6670

67-
state.pos = res.end()
71+
state.pos += res.end()
6872

6973
return bool(res)
7074

@@ -73,12 +77,8 @@ def _func(state, silent):
7377

7478
def make_block_func(rule):
7579
def _func(state, begLine, endLine, silent):
76-
res = applyRule(
77-
rule,
78-
state.src,
79-
state.bMarks[begLine] + state.tShift[begLine],
80-
state.parentType == "blockquote",
81-
)
80+
begin = state.bMarks[begLine] + state.tShift[begLine]
81+
res = applyRule(rule, state.src, begin, state.parentType == "blockquote")
8282
if res:
8383
if not silent:
8484
token = state.push(rule["name"], "math", 0)
@@ -88,7 +88,7 @@ def _func(state, begLine, endLine, silent):
8888
token.markup = rule["tag"]
8989

9090
line = begLine
91-
endpos = res.end() - 1
91+
endpos = begin + res.end() - 1
9292

9393
while line < endLine:
9494
if endpos >= state.bMarks[line] and endpos <= state.eMarks[line]:
@@ -97,7 +97,7 @@ def _func(state, begLine, endLine, silent):
9797
break
9898
line += 1
9999

100-
state.pos = res.end()
100+
state.pos = begin + res.end()
101101

102102
return bool(res)
103103

@@ -146,7 +146,7 @@ def render(tex, displayMode, macros):
146146
"inline": [
147147
{
148148
"name": "math_inline",
149-
"rex": re.compile(r"\\\((.+?)\\\)"),
149+
"rex": re.compile(r"^\\\((.+?)\\\)"),
150150
"tmpl": "<eq>{0}</eq>",
151151
"tag": "\\(",
152152
}
@@ -155,15 +155,15 @@ def render(tex, displayMode, macros):
155155
{
156156
"name": "math_block_eqno",
157157
"rex": re.compile(
158-
r"\\\[(((?!\\\]|\\\[)[\s\S])+?)\\\]\s*?\(([^)$\r\n]+?)\)", re.M
158+
r"^\\\[(((?!\\\]|\\\[)[\s\S])+?)\\\]\s*?\(([^)$\r\n]+?)\)", re.M
159159
),
160160
"tmpl": '<section class="eqno"><eqn>{0}</eqn><span>({1})</span></section>',
161161
"tag": "\\[",
162162
},
163163
{
164164
"name": "math_block",
165-
"rex": re.compile(r"\\\[([\s\S]+?)\\\]", re.M),
166-
"tmpl": "<section><eqn>{0}</eqn></section>",
165+
"rex": re.compile(r"^\\\[([\s\S]+?)\\\]", re.M),
166+
"tmpl": "<section>\n<eqn>{0}</eqn>\n</section>\n",
167167
"tag": "\\[",
168168
},
169169
],
@@ -172,7 +172,7 @@ def render(tex, displayMode, macros):
172172
"inline": [
173173
{
174174
"name": "math_inline",
175-
"rex": re.compile(r"\$`(.+?)`\$"),
175+
"rex": re.compile(r"^\$`(.+?)`\$"),
176176
"tmpl": "<eq>{0}</eq>",
177177
"tag": "$`",
178178
}
@@ -181,15 +181,15 @@ def render(tex, displayMode, macros):
181181
{
182182
"name": "math_block_eqno",
183183
"rex": re.compile(
184-
r"`{3}math\s+?([^`]+?)\s+?`{3}\s*?\(([^)$\r\n]+?)\)", re.M
184+
r"^`{3}math\s+?([^`]+?)\s+?`{3}\s*?\(([^)$\r\n]+?)\)", re.M
185185
),
186-
"tmpl": '<section class="eqno"><eqn>{0}</eqn><span>({1})</span></section>',
186+
"tmpl": '<section class="eqno">\n<eqn>{0}</eqn><span>({1})</span>\n</section>\n', # noqa: E501
187187
"tag": "```math",
188188
},
189189
{
190190
"name": "math_block",
191-
"rex": re.compile(r"`{3}math\s+?([^`]+?)\s+?`{3}", re.M),
192-
"tmpl": "<section><eqn>{0}</eqn></section>",
191+
"rex": re.compile(r"^`{3}math\s+?([^`]+?)\s+?`{3}", re.M),
192+
"tmpl": "<section>\n<eqn>{0}</eqn>\n</section>\n",
193193
"tag": "```math",
194194
},
195195
],
@@ -198,21 +198,21 @@ def render(tex, displayMode, macros):
198198
"inline": [
199199
{
200200
"name": "math_inline",
201-
"rex": re.compile(r"`{2}([^`]+?)`{2}"),
201+
"rex": re.compile(r"^`{2}([^`]+?)`{2}"),
202202
"tmpl": "<eq>{0}</eq>",
203203
"tag": "``",
204204
},
205205
{
206206
"name": "math_inline",
207-
"rex": re.compile(r"\$(\S[^$\r\n]*?[^\s\\]{1}?)\$"),
207+
"rex": re.compile(r"^\$(\S[^$\r\n]*?[^\s\\]{1}?)\$"),
208208
"tmpl": "<eq>{0}</eq>",
209209
"tag": "$",
210210
"pre": dollar_pre,
211211
"post": dollar_post,
212212
},
213213
{
214214
"name": "math_single",
215-
"rex": re.compile(r"\$([^$\s\\]{1}?)\$"),
215+
"rex": re.compile(r"^\$([^$\s\\]{1}?)\$"),
216216
"tmpl": "<eq>{0}</eq>",
217217
"tag": "$",
218218
"pre": dollar_pre,
@@ -223,14 +223,14 @@ def render(tex, displayMode, macros):
223223
{
224224
"name": "math_block_eqno",
225225
"rex": re.compile(
226-
r"`{3}math\s+?([^`]+?)\s+?`{3}\s*?\(([^)$\r\n]+?)\)", re.M
226+
r"^`{3}math\s+?([^`]+?)\s+?`{3}\s*?\(([^)$\r\n]+?)\)", re.M
227227
),
228228
"tmpl": '<section class="eqno"><eqn>{0}</eqn><span>({1})</span></section>',
229229
"tag": "```math",
230230
},
231231
{
232232
"name": "math_block",
233-
"rex": re.compile(r"`{3}math\s+?([^`]+?)\s+?`{3}", re.M),
233+
"rex": re.compile(r"^`{3}math\s+?([^`]+?)\s+?`{3}", re.M),
234234
"tmpl": "<section><eqn>{0}</eqn></section>",
235235
"tag": "```math",
236236
},
@@ -240,21 +240,23 @@ def render(tex, displayMode, macros):
240240
"inline": [
241241
{
242242
"name": "math_inline",
243-
"rex": re.compile(r"\${2}([^$\r\n]*?)\${2}"),
243+
"rex": re.compile(r"^\${2}([^$\r\n]*?)\${2}"),
244244
"tmpl": "<eq>{0}</eq>",
245245
"tag": "$$",
246246
}
247247
],
248248
"block": [
249249
{
250250
"name": "math_block_eqno",
251-
"rex": re.compile(r"\${2}([^$]*?)\${2}\s*?\(([^)$\r\n]+?)\)", re.M),
251+
"rex": re.compile(
252+
r"^\${2}([^$]*?)\${2}\s*?\(([^)$\r\n]+?)\)", re.M
253+
),
252254
"tmpl": '<section class="eqno"><eqn>{0}</eqn><span>({1})</span></section>',
253255
"tag": "$$",
254256
},
255257
{
256258
"name": "math_block",
257-
"rex": re.compile(r"\${2}([^$]*?)\${2}", re.M),
259+
"rex": re.compile(r"^\${2}([^$]*?)\${2}", re.M),
258260
"tmpl": "<section><eqn>{0}</eqn></section>",
259261
"tag": "$$",
260262
},
@@ -264,15 +266,15 @@ def render(tex, displayMode, macros):
264266
"inline": [
265267
{
266268
"name": "math_inline",
267-
"rex": re.compile(r"\$(\S[^$\r\n]*?[^\s\\]{1}?)\$"),
269+
"rex": re.compile(r"^\$(\S[^$\r\n]*?[^\s\\]{1}?)\$"),
268270
"tmpl": "<eq>{0}</eq>",
269271
"tag": "$",
270272
"pre": dollar_pre,
271273
"post": dollar_post,
272274
},
273275
{
274276
"name": "math_single",
275-
"rex": re.compile(r"\$([^$\s\\]{1}?)\$"),
277+
"rex": re.compile(r"^\$([^$\s\\]{1}?)\$"),
276278
"tmpl": "<eq>{0}</eq>",
277279
"tag": "$",
278280
"pre": dollar_pre,
@@ -282,13 +284,15 @@ def render(tex, displayMode, macros):
282284
"block": [
283285
{
284286
"name": "math_block_eqno",
285-
"rex": re.compile(r"\${2}([^$]*?)\${2}\s*?\(([^)$\r\n]+?)\)", re.M),
287+
"rex": re.compile(
288+
r"^\${2}([^$]*?)\${2}\s*?\(([^)$\r\n]+?)\)", re.M
289+
),
286290
"tmpl": '<section class="eqno">\n<eqn>{0}</eqn><span>({1})</span>\n</section>\n', # noqa: E501
287291
"tag": "$$",
288292
},
289293
{
290294
"name": "math_block",
291-
"rex": re.compile(r"\${2}([^$]*?)\${2}", re.M),
295+
"rex": re.compile(r"^\${2}([^$]*?)\${2}", re.M),
292296
"tmpl": "<section>\n<eqn>{0}</eqn>\n</section>\n",
293297
"tag": "$$",
294298
},

0 commit comments

Comments
 (0)