Skip to content

Commit a85ada1

Browse files
authored
♻️ REFACTOR: Remove AttrDict usage from texmath (#15)
1 parent c14b8fa commit a85ada1

File tree

2 files changed

+156
-163
lines changed

2 files changed

+156
-163
lines changed

mdit_py_plugins/texmath/index.py

Lines changed: 154 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from markdown_it import MarkdownIt
55
from markdown_it.common.utils import charCodeAt
6-
from markdown_it.utils import AttrDict
76

87

98
def texmath_plugin(md: MarkdownIt, delimiters="dollars", macros: Optional[dict] = None):
@@ -152,163 +151,157 @@ def render(tex, displayMode, macros):
152151
# All regexes areg global (g) and sticky (y), see:
153152
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky
154153

155-
rules = AttrDict(
156-
{
157-
"brackets": {
158-
"inline": [
159-
{
160-
"name": "math_inline",
161-
"rex": re.compile(r"^\\\((.+?)\\\)", re.DOTALL),
162-
"tmpl": "<eq>{0}</eq>",
163-
"tag": "\\(",
164-
}
165-
],
166-
"block": [
167-
{
168-
"name": "math_block_eqno",
169-
"rex": re.compile(
170-
r"^\\\[(((?!\\\]|\\\[)[\s\S])+?)\\\]\s*?\(([^)$\r\n]+?)\)", re.M
171-
),
172-
"tmpl": '<section class="eqno"><eqn>{0}</eqn><span>({1})</span></section>',
173-
"tag": "\\[",
174-
},
175-
{
176-
"name": "math_block",
177-
"rex": re.compile(r"^\\\[([\s\S]+?)\\\]", re.M),
178-
"tmpl": "<section>\n<eqn>{0}</eqn>\n</section>\n",
179-
"tag": "\\[",
180-
},
181-
],
182-
},
183-
"gitlab": {
184-
"inline": [
185-
{
186-
"name": "math_inline",
187-
"rex": re.compile(r"^\$`(.+?)`\$"),
188-
"tmpl": "<eq>{0}</eq>",
189-
"tag": "$`",
190-
}
191-
],
192-
"block": [
193-
{
194-
"name": "math_block_eqno",
195-
"rex": re.compile(
196-
r"^`{3}math\s+?([^`]+?)\s+?`{3}\s*?\(([^)$\r\n]+?)\)", re.M
197-
),
198-
"tmpl": '<section class="eqno">\n<eqn>{0}</eqn><span>({1})</span>\n</section>\n', # noqa: E501
199-
"tag": "```math",
200-
},
201-
{
202-
"name": "math_block",
203-
"rex": re.compile(r"^`{3}math\s+?([^`]+?)\s+?`{3}", re.M),
204-
"tmpl": "<section>\n<eqn>{0}</eqn>\n</section>\n",
205-
"tag": "```math",
206-
},
207-
],
208-
},
209-
"julia": {
210-
"inline": [
211-
{
212-
"name": "math_inline",
213-
"rex": re.compile(r"^`{2}([^`]+?)`{2}"),
214-
"tmpl": "<eq>{0}</eq>",
215-
"tag": "``",
216-
},
217-
{
218-
"name": "math_inline",
219-
"rex": re.compile(r"^\$(\S[^$\r\n]*?[^\s\\]{1}?)\$"),
220-
"tmpl": "<eq>{0}</eq>",
221-
"tag": "$",
222-
"pre": dollar_pre,
223-
"post": dollar_post,
224-
},
225-
{
226-
"name": "math_single",
227-
"rex": re.compile(r"^\$([^$\s\\]{1}?)\$"),
228-
"tmpl": "<eq>{0}</eq>",
229-
"tag": "$",
230-
"pre": dollar_pre,
231-
"post": dollar_post,
232-
},
233-
],
234-
"block": [
235-
{
236-
"name": "math_block_eqno",
237-
"rex": re.compile(
238-
r"^`{3}math\s+?([^`]+?)\s+?`{3}\s*?\(([^)$\r\n]+?)\)", re.M
239-
),
240-
"tmpl": '<section class="eqno"><eqn>{0}</eqn><span>({1})</span></section>',
241-
"tag": "```math",
242-
},
243-
{
244-
"name": "math_block",
245-
"rex": re.compile(r"^`{3}math\s+?([^`]+?)\s+?`{3}", re.M),
246-
"tmpl": "<section><eqn>{0}</eqn></section>",
247-
"tag": "```math",
248-
},
249-
],
250-
},
251-
"kramdown": {
252-
"inline": [
253-
{
254-
"name": "math_inline",
255-
"rex": re.compile(r"^\${2}([^$\r\n]*?)\${2}"),
256-
"tmpl": "<eq>{0}</eq>",
257-
"tag": "$$",
258-
}
259-
],
260-
"block": [
261-
{
262-
"name": "math_block_eqno",
263-
"rex": re.compile(
264-
r"^\${2}([^$]*?)\${2}\s*?\(([^)$\r\n]+?)\)", re.M
265-
),
266-
"tmpl": '<section class="eqno"><eqn>{0}</eqn><span>({1})</span></section>',
267-
"tag": "$$",
268-
},
269-
{
270-
"name": "math_block",
271-
"rex": re.compile(r"^\${2}([^$]*?)\${2}", re.M),
272-
"tmpl": "<section><eqn>{0}</eqn></section>",
273-
"tag": "$$",
274-
},
275-
],
276-
},
277-
"dollars": {
278-
"inline": [
279-
{
280-
"name": "math_inline",
281-
"rex": re.compile(r"^\$(\S[^$]*?[^\s\\]{1}?)\$"),
282-
"tmpl": "<eq>{0}</eq>",
283-
"tag": "$",
284-
"pre": dollar_pre,
285-
"post": dollar_post,
286-
},
287-
{
288-
"name": "math_single",
289-
"rex": re.compile(r"^\$([^$\s\\]{1}?)\$"),
290-
"tmpl": "<eq>{0}</eq>",
291-
"tag": "$",
292-
"pre": dollar_pre,
293-
"post": dollar_post,
294-
},
295-
],
296-
"block": [
297-
{
298-
"name": "math_block_eqno",
299-
"rex": re.compile(
300-
r"^\${2}([^$]*?)\${2}\s*?\(([^)$\r\n]+?)\)", re.M
301-
),
302-
"tmpl": '<section class="eqno">\n<eqn>{0}</eqn><span>({1})</span>\n</section>\n', # noqa: E501
303-
"tag": "$$",
304-
},
305-
{
306-
"name": "math_block",
307-
"rex": re.compile(r"^\${2}([^$]*?)\${2}", re.M),
308-
"tmpl": "<section>\n<eqn>{0}</eqn>\n</section>\n",
309-
"tag": "$$",
310-
},
311-
],
312-
},
313-
}
314-
)
154+
rules: dict = {
155+
"brackets": {
156+
"inline": [
157+
{
158+
"name": "math_inline",
159+
"rex": re.compile(r"^\\\((.+?)\\\)", re.DOTALL),
160+
"tmpl": "<eq>{0}</eq>",
161+
"tag": "\\(",
162+
}
163+
],
164+
"block": [
165+
{
166+
"name": "math_block_eqno",
167+
"rex": re.compile(
168+
r"^\\\[(((?!\\\]|\\\[)[\s\S])+?)\\\]\s*?\(([^)$\r\n]+?)\)", re.M
169+
),
170+
"tmpl": '<section class="eqno"><eqn>{0}</eqn><span>({1})</span></section>',
171+
"tag": "\\[",
172+
},
173+
{
174+
"name": "math_block",
175+
"rex": re.compile(r"^\\\[([\s\S]+?)\\\]", re.M),
176+
"tmpl": "<section>\n<eqn>{0}</eqn>\n</section>\n",
177+
"tag": "\\[",
178+
},
179+
],
180+
},
181+
"gitlab": {
182+
"inline": [
183+
{
184+
"name": "math_inline",
185+
"rex": re.compile(r"^\$`(.+?)`\$"),
186+
"tmpl": "<eq>{0}</eq>",
187+
"tag": "$`",
188+
}
189+
],
190+
"block": [
191+
{
192+
"name": "math_block_eqno",
193+
"rex": re.compile(
194+
r"^`{3}math\s+?([^`]+?)\s+?`{3}\s*?\(([^)$\r\n]+?)\)", re.M
195+
),
196+
"tmpl": '<section class="eqno">\n<eqn>{0}</eqn><span>({1})</span>\n</section>\n', # noqa: E501
197+
"tag": "```math",
198+
},
199+
{
200+
"name": "math_block",
201+
"rex": re.compile(r"^`{3}math\s+?([^`]+?)\s+?`{3}", re.M),
202+
"tmpl": "<section>\n<eqn>{0}</eqn>\n</section>\n",
203+
"tag": "```math",
204+
},
205+
],
206+
},
207+
"julia": {
208+
"inline": [
209+
{
210+
"name": "math_inline",
211+
"rex": re.compile(r"^`{2}([^`]+?)`{2}"),
212+
"tmpl": "<eq>{0}</eq>",
213+
"tag": "``",
214+
},
215+
{
216+
"name": "math_inline",
217+
"rex": re.compile(r"^\$(\S[^$\r\n]*?[^\s\\]{1}?)\$"),
218+
"tmpl": "<eq>{0}</eq>",
219+
"tag": "$",
220+
"pre": dollar_pre,
221+
"post": dollar_post,
222+
},
223+
{
224+
"name": "math_single",
225+
"rex": re.compile(r"^\$([^$\s\\]{1}?)\$"),
226+
"tmpl": "<eq>{0}</eq>",
227+
"tag": "$",
228+
"pre": dollar_pre,
229+
"post": dollar_post,
230+
},
231+
],
232+
"block": [
233+
{
234+
"name": "math_block_eqno",
235+
"rex": re.compile(
236+
r"^`{3}math\s+?([^`]+?)\s+?`{3}\s*?\(([^)$\r\n]+?)\)", re.M
237+
),
238+
"tmpl": '<section class="eqno"><eqn>{0}</eqn><span>({1})</span></section>',
239+
"tag": "```math",
240+
},
241+
{
242+
"name": "math_block",
243+
"rex": re.compile(r"^`{3}math\s+?([^`]+?)\s+?`{3}", re.M),
244+
"tmpl": "<section><eqn>{0}</eqn></section>",
245+
"tag": "```math",
246+
},
247+
],
248+
},
249+
"kramdown": {
250+
"inline": [
251+
{
252+
"name": "math_inline",
253+
"rex": re.compile(r"^\${2}([^$\r\n]*?)\${2}"),
254+
"tmpl": "<eq>{0}</eq>",
255+
"tag": "$$",
256+
}
257+
],
258+
"block": [
259+
{
260+
"name": "math_block_eqno",
261+
"rex": re.compile(r"^\${2}([^$]*?)\${2}\s*?\(([^)$\r\n]+?)\)", re.M),
262+
"tmpl": '<section class="eqno"><eqn>{0}</eqn><span>({1})</span></section>',
263+
"tag": "$$",
264+
},
265+
{
266+
"name": "math_block",
267+
"rex": re.compile(r"^\${2}([^$]*?)\${2}", re.M),
268+
"tmpl": "<section><eqn>{0}</eqn></section>",
269+
"tag": "$$",
270+
},
271+
],
272+
},
273+
"dollars": {
274+
"inline": [
275+
{
276+
"name": "math_inline",
277+
"rex": re.compile(r"^\$(\S[^$]*?[^\s\\]{1}?)\$"),
278+
"tmpl": "<eq>{0}</eq>",
279+
"tag": "$",
280+
"pre": dollar_pre,
281+
"post": dollar_post,
282+
},
283+
{
284+
"name": "math_single",
285+
"rex": re.compile(r"^\$([^$\s\\]{1}?)\$"),
286+
"tmpl": "<eq>{0}</eq>",
287+
"tag": "$",
288+
"pre": dollar_pre,
289+
"post": dollar_post,
290+
},
291+
],
292+
"block": [
293+
{
294+
"name": "math_block_eqno",
295+
"rex": re.compile(r"^\${2}([^$]*?)\${2}\s*?\(([^)$\r\n]+?)\)", re.M),
296+
"tmpl": '<section class="eqno">\n<eqn>{0}</eqn><span>({1})</span>\n</section>\n', # noqa: E501
297+
"tag": "$$",
298+
},
299+
{
300+
"name": "math_block",
301+
"rex": re.compile(r"^\${2}([^$]*?)\${2}", re.M),
302+
"tmpl": "<section>\n<eqn>{0}</eqn>\n</section>\n",
303+
"tag": "$$",
304+
},
305+
],
306+
},
307+
}

tests/test_texmath.py

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

1616
def test_inline_func():
1717

18-
inline_func = main.make_inline_func(main.rules.dollars.inline[0])
18+
inline_func = main.make_inline_func(main.rules["dollars"]["inline"][0])
1919

2020
md = MarkdownIt()
2121
src = r"$a=1$ $b=2$"
@@ -41,7 +41,7 @@ def test_inline_func():
4141

4242

4343
def test_block_func():
44-
block_func = main.make_block_func(main.rules.dollars.block[0])
44+
block_func = main.make_block_func(main.rules["dollars"]["block"][0])
4545
md = MarkdownIt()
4646
src = r"$$\na=1\n\nc\nb=2$$ (abc)"
4747
tokens = []

0 commit comments

Comments
 (0)