@@ -35,24 +35,28 @@ def render_math_block(self, tokens, idx, options, env):
35
35
md .add_render_rule (rule_block ["name" ], render_math_block )
36
36
37
37
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
54
50
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
56
60
57
61
58
62
def make_inline_func (rule ):
@@ -64,7 +68,7 @@ def _func(state, silent):
64
68
token .content = res [1 ] # group 1 from regex ..
65
69
token .markup = rule ["tag" ]
66
70
67
- state .pos = res .end ()
71
+ state .pos + = res .end ()
68
72
69
73
return bool (res )
70
74
@@ -73,12 +77,8 @@ def _func(state, silent):
73
77
74
78
def make_block_func (rule ):
75
79
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" )
82
82
if res :
83
83
if not silent :
84
84
token = state .push (rule ["name" ], "math" , 0 )
@@ -88,7 +88,7 @@ def _func(state, begLine, endLine, silent):
88
88
token .markup = rule ["tag" ]
89
89
90
90
line = begLine
91
- endpos = res .end () - 1
91
+ endpos = begin + res .end () - 1
92
92
93
93
while line < endLine :
94
94
if endpos >= state .bMarks [line ] and endpos <= state .eMarks [line ]:
@@ -97,7 +97,7 @@ def _func(state, begLine, endLine, silent):
97
97
break
98
98
line += 1
99
99
100
- state .pos = res .end ()
100
+ state .pos = begin + res .end ()
101
101
102
102
return bool (res )
103
103
@@ -146,7 +146,7 @@ def render(tex, displayMode, macros):
146
146
"inline" : [
147
147
{
148
148
"name" : "math_inline" ,
149
- "rex" : re .compile (r"\\\((.+?)\\\)" ),
149
+ "rex" : re .compile (r"^ \\\((.+?)\\\)" ),
150
150
"tmpl" : "<eq>{0}</eq>" ,
151
151
"tag" : "\\ (" ,
152
152
}
@@ -155,15 +155,15 @@ def render(tex, displayMode, macros):
155
155
{
156
156
"name" : "math_block_eqno" ,
157
157
"rex" : re .compile (
158
- r"\\\[(((?!\\\]|\\\[)[\s\S])+?)\\\]\s*?\(([^)$\r\n]+?)\)" , re .M
158
+ r"^ \\\[(((?!\\\]|\\\[)[\s\S])+?)\\\]\s*?\(([^)$\r\n]+?)\)" , re .M
159
159
),
160
160
"tmpl" : '<section class="eqno"><eqn>{0}</eqn><span>({1})</span></section>' ,
161
161
"tag" : "\\ [" ,
162
162
},
163
163
{
164
164
"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 " ,
167
167
"tag" : "\\ [" ,
168
168
},
169
169
],
@@ -172,7 +172,7 @@ def render(tex, displayMode, macros):
172
172
"inline" : [
173
173
{
174
174
"name" : "math_inline" ,
175
- "rex" : re .compile (r"\$`(.+?)`\$" ),
175
+ "rex" : re .compile (r"^ \$`(.+?)`\$" ),
176
176
"tmpl" : "<eq>{0}</eq>" ,
177
177
"tag" : "$`" ,
178
178
}
@@ -181,15 +181,15 @@ def render(tex, displayMode, macros):
181
181
{
182
182
"name" : "math_block_eqno" ,
183
183
"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
185
185
),
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
187
187
"tag" : "```math" ,
188
188
},
189
189
{
190
190
"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 " ,
193
193
"tag" : "```math" ,
194
194
},
195
195
],
@@ -198,21 +198,21 @@ def render(tex, displayMode, macros):
198
198
"inline" : [
199
199
{
200
200
"name" : "math_inline" ,
201
- "rex" : re .compile (r"`{2}([^`]+?)`{2}" ),
201
+ "rex" : re .compile (r"^ `{2}([^`]+?)`{2}" ),
202
202
"tmpl" : "<eq>{0}</eq>" ,
203
203
"tag" : "``" ,
204
204
},
205
205
{
206
206
"name" : "math_inline" ,
207
- "rex" : re .compile (r"\$(\S[^$\r\n]*?[^\s\\]{1}?)\$" ),
207
+ "rex" : re .compile (r"^ \$(\S[^$\r\n]*?[^\s\\]{1}?)\$" ),
208
208
"tmpl" : "<eq>{0}</eq>" ,
209
209
"tag" : "$" ,
210
210
"pre" : dollar_pre ,
211
211
"post" : dollar_post ,
212
212
},
213
213
{
214
214
"name" : "math_single" ,
215
- "rex" : re .compile (r"\$([^$\s\\]{1}?)\$" ),
215
+ "rex" : re .compile (r"^ \$([^$\s\\]{1}?)\$" ),
216
216
"tmpl" : "<eq>{0}</eq>" ,
217
217
"tag" : "$" ,
218
218
"pre" : dollar_pre ,
@@ -223,14 +223,14 @@ def render(tex, displayMode, macros):
223
223
{
224
224
"name" : "math_block_eqno" ,
225
225
"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
227
227
),
228
228
"tmpl" : '<section class="eqno"><eqn>{0}</eqn><span>({1})</span></section>' ,
229
229
"tag" : "```math" ,
230
230
},
231
231
{
232
232
"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 ),
234
234
"tmpl" : "<section><eqn>{0}</eqn></section>" ,
235
235
"tag" : "```math" ,
236
236
},
@@ -240,21 +240,23 @@ def render(tex, displayMode, macros):
240
240
"inline" : [
241
241
{
242
242
"name" : "math_inline" ,
243
- "rex" : re .compile (r"\${2}([^$\r\n]*?)\${2}" ),
243
+ "rex" : re .compile (r"^ \${2}([^$\r\n]*?)\${2}" ),
244
244
"tmpl" : "<eq>{0}</eq>" ,
245
245
"tag" : "$$" ,
246
246
}
247
247
],
248
248
"block" : [
249
249
{
250
250
"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
+ ),
252
254
"tmpl" : '<section class="eqno"><eqn>{0}</eqn><span>({1})</span></section>' ,
253
255
"tag" : "$$" ,
254
256
},
255
257
{
256
258
"name" : "math_block" ,
257
- "rex" : re .compile (r"\${2}([^$]*?)\${2}" , re .M ),
259
+ "rex" : re .compile (r"^ \${2}([^$]*?)\${2}" , re .M ),
258
260
"tmpl" : "<section><eqn>{0}</eqn></section>" ,
259
261
"tag" : "$$" ,
260
262
},
@@ -264,15 +266,15 @@ def render(tex, displayMode, macros):
264
266
"inline" : [
265
267
{
266
268
"name" : "math_inline" ,
267
- "rex" : re .compile (r"\$(\S[^$\r\n]*?[^\s\\]{1}?)\$" ),
269
+ "rex" : re .compile (r"^ \$(\S[^$\r\n]*?[^\s\\]{1}?)\$" ),
268
270
"tmpl" : "<eq>{0}</eq>" ,
269
271
"tag" : "$" ,
270
272
"pre" : dollar_pre ,
271
273
"post" : dollar_post ,
272
274
},
273
275
{
274
276
"name" : "math_single" ,
275
- "rex" : re .compile (r"\$([^$\s\\]{1}?)\$" ),
277
+ "rex" : re .compile (r"^ \$([^$\s\\]{1}?)\$" ),
276
278
"tmpl" : "<eq>{0}</eq>" ,
277
279
"tag" : "$" ,
278
280
"pre" : dollar_pre ,
@@ -282,13 +284,15 @@ def render(tex, displayMode, macros):
282
284
"block" : [
283
285
{
284
286
"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
+ ),
286
290
"tmpl" : '<section class="eqno">\n <eqn>{0}</eqn><span>({1})</span>\n </section>\n ' , # noqa: E501
287
291
"tag" : "$$" ,
288
292
},
289
293
{
290
294
"name" : "math_block" ,
291
- "rex" : re .compile (r"\${2}([^$]*?)\${2}" , re .M ),
295
+ "rex" : re .compile (r"^ \${2}([^$]*?)\${2}" , re .M ),
292
296
"tmpl" : "<section>\n <eqn>{0}</eqn>\n </section>\n " ,
293
297
"tag" : "$$" ,
294
298
},
0 commit comments