From c391f1bc0cad244d371dbdd1bc74f92492799c57 Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen Date: Wed, 31 Mar 2021 13:43:02 +0300 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20REFACTOR:=20Remove=20AttrD?= =?UTF-8?q?ict=20usage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mdit_py_plugins/texmath/index.py | 315 +++++++++++++++---------------- tests/test_texmath.py | 4 +- 2 files changed, 156 insertions(+), 163 deletions(-) diff --git a/mdit_py_plugins/texmath/index.py b/mdit_py_plugins/texmath/index.py index f4fb509..ecf178c 100644 --- a/mdit_py_plugins/texmath/index.py +++ b/mdit_py_plugins/texmath/index.py @@ -3,7 +3,6 @@ from markdown_it import MarkdownIt from markdown_it.common.utils import charCodeAt -from markdown_it.utils import AttrDict def texmath_plugin(md: MarkdownIt, delimiters="dollars", macros: Optional[dict] = None): @@ -152,163 +151,157 @@ def render(tex, displayMode, macros): # All regexes areg global (g) and sticky (y), see: # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky -rules = AttrDict( - { - "brackets": { - "inline": [ - { - "name": "math_inline", - "rex": re.compile(r"^\\\((.+?)\\\)", re.DOTALL), - "tmpl": "{0}", - "tag": "\\(", - } - ], - "block": [ - { - "name": "math_block_eqno", - "rex": re.compile( - r"^\\\[(((?!\\\]|\\\[)[\s\S])+?)\\\]\s*?\(([^)$\r\n]+?)\)", re.M - ), - "tmpl": '
{0}({1})
', - "tag": "\\[", - }, - { - "name": "math_block", - "rex": re.compile(r"^\\\[([\s\S]+?)\\\]", re.M), - "tmpl": "
\n{0}\n
\n", - "tag": "\\[", - }, - ], - }, - "gitlab": { - "inline": [ - { - "name": "math_inline", - "rex": re.compile(r"^\$`(.+?)`\$"), - "tmpl": "{0}", - "tag": "$`", - } - ], - "block": [ - { - "name": "math_block_eqno", - "rex": re.compile( - r"^`{3}math\s+?([^`]+?)\s+?`{3}\s*?\(([^)$\r\n]+?)\)", re.M - ), - "tmpl": '
\n{0}({1})\n
\n', # noqa: E501 - "tag": "```math", - }, - { - "name": "math_block", - "rex": re.compile(r"^`{3}math\s+?([^`]+?)\s+?`{3}", re.M), - "tmpl": "
\n{0}\n
\n", - "tag": "```math", - }, - ], - }, - "julia": { - "inline": [ - { - "name": "math_inline", - "rex": re.compile(r"^`{2}([^`]+?)`{2}"), - "tmpl": "{0}", - "tag": "``", - }, - { - "name": "math_inline", - "rex": re.compile(r"^\$(\S[^$\r\n]*?[^\s\\]{1}?)\$"), - "tmpl": "{0}", - "tag": "$", - "pre": dollar_pre, - "post": dollar_post, - }, - { - "name": "math_single", - "rex": re.compile(r"^\$([^$\s\\]{1}?)\$"), - "tmpl": "{0}", - "tag": "$", - "pre": dollar_pre, - "post": dollar_post, - }, - ], - "block": [ - { - "name": "math_block_eqno", - "rex": re.compile( - r"^`{3}math\s+?([^`]+?)\s+?`{3}\s*?\(([^)$\r\n]+?)\)", re.M - ), - "tmpl": '
{0}({1})
', - "tag": "```math", - }, - { - "name": "math_block", - "rex": re.compile(r"^`{3}math\s+?([^`]+?)\s+?`{3}", re.M), - "tmpl": "
{0}
", - "tag": "```math", - }, - ], - }, - "kramdown": { - "inline": [ - { - "name": "math_inline", - "rex": re.compile(r"^\${2}([^$\r\n]*?)\${2}"), - "tmpl": "{0}", - "tag": "$$", - } - ], - "block": [ - { - "name": "math_block_eqno", - "rex": re.compile( - r"^\${2}([^$]*?)\${2}\s*?\(([^)$\r\n]+?)\)", re.M - ), - "tmpl": '
{0}({1})
', - "tag": "$$", - }, - { - "name": "math_block", - "rex": re.compile(r"^\${2}([^$]*?)\${2}", re.M), - "tmpl": "
{0}
", - "tag": "$$", - }, - ], - }, - "dollars": { - "inline": [ - { - "name": "math_inline", - "rex": re.compile(r"^\$(\S[^$]*?[^\s\\]{1}?)\$"), - "tmpl": "{0}", - "tag": "$", - "pre": dollar_pre, - "post": dollar_post, - }, - { - "name": "math_single", - "rex": re.compile(r"^\$([^$\s\\]{1}?)\$"), - "tmpl": "{0}", - "tag": "$", - "pre": dollar_pre, - "post": dollar_post, - }, - ], - "block": [ - { - "name": "math_block_eqno", - "rex": re.compile( - r"^\${2}([^$]*?)\${2}\s*?\(([^)$\r\n]+?)\)", re.M - ), - "tmpl": '
\n{0}({1})\n
\n', # noqa: E501 - "tag": "$$", - }, - { - "name": "math_block", - "rex": re.compile(r"^\${2}([^$]*?)\${2}", re.M), - "tmpl": "
\n{0}\n
\n", - "tag": "$$", - }, - ], - }, - } -) +rules: dict = { + "brackets": { + "inline": [ + { + "name": "math_inline", + "rex": re.compile(r"^\\\((.+?)\\\)", re.DOTALL), + "tmpl": "{0}", + "tag": "\\(", + } + ], + "block": [ + { + "name": "math_block_eqno", + "rex": re.compile( + r"^\\\[(((?!\\\]|\\\[)[\s\S])+?)\\\]\s*?\(([^)$\r\n]+?)\)", re.M + ), + "tmpl": '
{0}({1})
', + "tag": "\\[", + }, + { + "name": "math_block", + "rex": re.compile(r"^\\\[([\s\S]+?)\\\]", re.M), + "tmpl": "
\n{0}\n
\n", + "tag": "\\[", + }, + ], + }, + "gitlab": { + "inline": [ + { + "name": "math_inline", + "rex": re.compile(r"^\$`(.+?)`\$"), + "tmpl": "{0}", + "tag": "$`", + } + ], + "block": [ + { + "name": "math_block_eqno", + "rex": re.compile( + r"^`{3}math\s+?([^`]+?)\s+?`{3}\s*?\(([^)$\r\n]+?)\)", re.M + ), + "tmpl": '
\n{0}({1})\n
\n', # noqa: E501 + "tag": "```math", + }, + { + "name": "math_block", + "rex": re.compile(r"^`{3}math\s+?([^`]+?)\s+?`{3}", re.M), + "tmpl": "
\n{0}\n
\n", + "tag": "```math", + }, + ], + }, + "julia": { + "inline": [ + { + "name": "math_inline", + "rex": re.compile(r"^`{2}([^`]+?)`{2}"), + "tmpl": "{0}", + "tag": "``", + }, + { + "name": "math_inline", + "rex": re.compile(r"^\$(\S[^$\r\n]*?[^\s\\]{1}?)\$"), + "tmpl": "{0}", + "tag": "$", + "pre": dollar_pre, + "post": dollar_post, + }, + { + "name": "math_single", + "rex": re.compile(r"^\$([^$\s\\]{1}?)\$"), + "tmpl": "{0}", + "tag": "$", + "pre": dollar_pre, + "post": dollar_post, + }, + ], + "block": [ + { + "name": "math_block_eqno", + "rex": re.compile( + r"^`{3}math\s+?([^`]+?)\s+?`{3}\s*?\(([^)$\r\n]+?)\)", re.M + ), + "tmpl": '
{0}({1})
', + "tag": "```math", + }, + { + "name": "math_block", + "rex": re.compile(r"^`{3}math\s+?([^`]+?)\s+?`{3}", re.M), + "tmpl": "
{0}
", + "tag": "```math", + }, + ], + }, + "kramdown": { + "inline": [ + { + "name": "math_inline", + "rex": re.compile(r"^\${2}([^$\r\n]*?)\${2}"), + "tmpl": "{0}", + "tag": "$$", + } + ], + "block": [ + { + "name": "math_block_eqno", + "rex": re.compile(r"^\${2}([^$]*?)\${2}\s*?\(([^)$\r\n]+?)\)", re.M), + "tmpl": '
{0}({1})
', + "tag": "$$", + }, + { + "name": "math_block", + "rex": re.compile(r"^\${2}([^$]*?)\${2}", re.M), + "tmpl": "
{0}
", + "tag": "$$", + }, + ], + }, + "dollars": { + "inline": [ + { + "name": "math_inline", + "rex": re.compile(r"^\$(\S[^$]*?[^\s\\]{1}?)\$"), + "tmpl": "{0}", + "tag": "$", + "pre": dollar_pre, + "post": dollar_post, + }, + { + "name": "math_single", + "rex": re.compile(r"^\$([^$\s\\]{1}?)\$"), + "tmpl": "{0}", + "tag": "$", + "pre": dollar_pre, + "post": dollar_post, + }, + ], + "block": [ + { + "name": "math_block_eqno", + "rex": re.compile(r"^\${2}([^$]*?)\${2}\s*?\(([^)$\r\n]+?)\)", re.M), + "tmpl": '
\n{0}({1})\n
\n', # noqa: E501 + "tag": "$$", + }, + { + "name": "math_block", + "rex": re.compile(r"^\${2}([^$]*?)\${2}", re.M), + "tmpl": "
\n{0}\n
\n", + "tag": "$$", + }, + ], + }, +} diff --git a/tests/test_texmath.py b/tests/test_texmath.py index ddccb45..fd8c103 100644 --- a/tests/test_texmath.py +++ b/tests/test_texmath.py @@ -15,7 +15,7 @@ def test_inline_func(): - inline_func = main.make_inline_func(main.rules.dollars.inline[0]) + inline_func = main.make_inline_func(main.rules["dollars"]["inline"][0]) md = MarkdownIt() src = r"$a=1$ $b=2$" @@ -41,7 +41,7 @@ def test_inline_func(): def test_block_func(): - block_func = main.make_block_func(main.rules.dollars.block[0]) + block_func = main.make_block_func(main.rules["dollars"]["block"][0]) md = MarkdownIt() src = r"$$\na=1\n\nc\nb=2$$ (abc)" tokens = []