Skip to content

♻️ REFACTOR: Remove AttrDict usage from texmath #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
315 changes: 154 additions & 161 deletions mdit_py_plugins/texmath/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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": "<eq>{0}</eq>",
"tag": "\\(",
}
],
"block": [
{
"name": "math_block_eqno",
"rex": re.compile(
r"^\\\[(((?!\\\]|\\\[)[\s\S])+?)\\\]\s*?\(([^)$\r\n]+?)\)", re.M
),
"tmpl": '<section class="eqno"><eqn>{0}</eqn><span>({1})</span></section>',
"tag": "\\[",
},
{
"name": "math_block",
"rex": re.compile(r"^\\\[([\s\S]+?)\\\]", re.M),
"tmpl": "<section>\n<eqn>{0}</eqn>\n</section>\n",
"tag": "\\[",
},
],
},
"gitlab": {
"inline": [
{
"name": "math_inline",
"rex": re.compile(r"^\$`(.+?)`\$"),
"tmpl": "<eq>{0}</eq>",
"tag": "$`",
}
],
"block": [
{
"name": "math_block_eqno",
"rex": re.compile(
r"^`{3}math\s+?([^`]+?)\s+?`{3}\s*?\(([^)$\r\n]+?)\)", re.M
),
"tmpl": '<section class="eqno">\n<eqn>{0}</eqn><span>({1})</span>\n</section>\n', # noqa: E501
"tag": "```math",
},
{
"name": "math_block",
"rex": re.compile(r"^`{3}math\s+?([^`]+?)\s+?`{3}", re.M),
"tmpl": "<section>\n<eqn>{0}</eqn>\n</section>\n",
"tag": "```math",
},
],
},
"julia": {
"inline": [
{
"name": "math_inline",
"rex": re.compile(r"^`{2}([^`]+?)`{2}"),
"tmpl": "<eq>{0}</eq>",
"tag": "``",
},
{
"name": "math_inline",
"rex": re.compile(r"^\$(\S[^$\r\n]*?[^\s\\]{1}?)\$"),
"tmpl": "<eq>{0}</eq>",
"tag": "$",
"pre": dollar_pre,
"post": dollar_post,
},
{
"name": "math_single",
"rex": re.compile(r"^\$([^$\s\\]{1}?)\$"),
"tmpl": "<eq>{0}</eq>",
"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": '<section class="eqno"><eqn>{0}</eqn><span>({1})</span></section>',
"tag": "```math",
},
{
"name": "math_block",
"rex": re.compile(r"^`{3}math\s+?([^`]+?)\s+?`{3}", re.M),
"tmpl": "<section><eqn>{0}</eqn></section>",
"tag": "```math",
},
],
},
"kramdown": {
"inline": [
{
"name": "math_inline",
"rex": re.compile(r"^\${2}([^$\r\n]*?)\${2}"),
"tmpl": "<eq>{0}</eq>",
"tag": "$$",
}
],
"block": [
{
"name": "math_block_eqno",
"rex": re.compile(
r"^\${2}([^$]*?)\${2}\s*?\(([^)$\r\n]+?)\)", re.M
),
"tmpl": '<section class="eqno"><eqn>{0}</eqn><span>({1})</span></section>',
"tag": "$$",
},
{
"name": "math_block",
"rex": re.compile(r"^\${2}([^$]*?)\${2}", re.M),
"tmpl": "<section><eqn>{0}</eqn></section>",
"tag": "$$",
},
],
},
"dollars": {
"inline": [
{
"name": "math_inline",
"rex": re.compile(r"^\$(\S[^$]*?[^\s\\]{1}?)\$"),
"tmpl": "<eq>{0}</eq>",
"tag": "$",
"pre": dollar_pre,
"post": dollar_post,
},
{
"name": "math_single",
"rex": re.compile(r"^\$([^$\s\\]{1}?)\$"),
"tmpl": "<eq>{0}</eq>",
"tag": "$",
"pre": dollar_pre,
"post": dollar_post,
},
],
"block": [
{
"name": "math_block_eqno",
"rex": re.compile(
r"^\${2}([^$]*?)\${2}\s*?\(([^)$\r\n]+?)\)", re.M
),
"tmpl": '<section class="eqno">\n<eqn>{0}</eqn><span>({1})</span>\n</section>\n', # noqa: E501
"tag": "$$",
},
{
"name": "math_block",
"rex": re.compile(r"^\${2}([^$]*?)\${2}", re.M),
"tmpl": "<section>\n<eqn>{0}</eqn>\n</section>\n",
"tag": "$$",
},
],
},
}
)
rules: dict = {
"brackets": {
"inline": [
{
"name": "math_inline",
"rex": re.compile(r"^\\\((.+?)\\\)", re.DOTALL),
"tmpl": "<eq>{0}</eq>",
"tag": "\\(",
}
],
"block": [
{
"name": "math_block_eqno",
"rex": re.compile(
r"^\\\[(((?!\\\]|\\\[)[\s\S])+?)\\\]\s*?\(([^)$\r\n]+?)\)", re.M
),
"tmpl": '<section class="eqno"><eqn>{0}</eqn><span>({1})</span></section>',
"tag": "\\[",
},
{
"name": "math_block",
"rex": re.compile(r"^\\\[([\s\S]+?)\\\]", re.M),
"tmpl": "<section>\n<eqn>{0}</eqn>\n</section>\n",
"tag": "\\[",
},
],
},
"gitlab": {
"inline": [
{
"name": "math_inline",
"rex": re.compile(r"^\$`(.+?)`\$"),
"tmpl": "<eq>{0}</eq>",
"tag": "$`",
}
],
"block": [
{
"name": "math_block_eqno",
"rex": re.compile(
r"^`{3}math\s+?([^`]+?)\s+?`{3}\s*?\(([^)$\r\n]+?)\)", re.M
),
"tmpl": '<section class="eqno">\n<eqn>{0}</eqn><span>({1})</span>\n</section>\n', # noqa: E501
"tag": "```math",
},
{
"name": "math_block",
"rex": re.compile(r"^`{3}math\s+?([^`]+?)\s+?`{3}", re.M),
"tmpl": "<section>\n<eqn>{0}</eqn>\n</section>\n",
"tag": "```math",
},
],
},
"julia": {
"inline": [
{
"name": "math_inline",
"rex": re.compile(r"^`{2}([^`]+?)`{2}"),
"tmpl": "<eq>{0}</eq>",
"tag": "``",
},
{
"name": "math_inline",
"rex": re.compile(r"^\$(\S[^$\r\n]*?[^\s\\]{1}?)\$"),
"tmpl": "<eq>{0}</eq>",
"tag": "$",
"pre": dollar_pre,
"post": dollar_post,
},
{
"name": "math_single",
"rex": re.compile(r"^\$([^$\s\\]{1}?)\$"),
"tmpl": "<eq>{0}</eq>",
"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": '<section class="eqno"><eqn>{0}</eqn><span>({1})</span></section>',
"tag": "```math",
},
{
"name": "math_block",
"rex": re.compile(r"^`{3}math\s+?([^`]+?)\s+?`{3}", re.M),
"tmpl": "<section><eqn>{0}</eqn></section>",
"tag": "```math",
},
],
},
"kramdown": {
"inline": [
{
"name": "math_inline",
"rex": re.compile(r"^\${2}([^$\r\n]*?)\${2}"),
"tmpl": "<eq>{0}</eq>",
"tag": "$$",
}
],
"block": [
{
"name": "math_block_eqno",
"rex": re.compile(r"^\${2}([^$]*?)\${2}\s*?\(([^)$\r\n]+?)\)", re.M),
"tmpl": '<section class="eqno"><eqn>{0}</eqn><span>({1})</span></section>',
"tag": "$$",
},
{
"name": "math_block",
"rex": re.compile(r"^\${2}([^$]*?)\${2}", re.M),
"tmpl": "<section><eqn>{0}</eqn></section>",
"tag": "$$",
},
],
},
"dollars": {
"inline": [
{
"name": "math_inline",
"rex": re.compile(r"^\$(\S[^$]*?[^\s\\]{1}?)\$"),
"tmpl": "<eq>{0}</eq>",
"tag": "$",
"pre": dollar_pre,
"post": dollar_post,
},
{
"name": "math_single",
"rex": re.compile(r"^\$([^$\s\\]{1}?)\$"),
"tmpl": "<eq>{0}</eq>",
"tag": "$",
"pre": dollar_pre,
"post": dollar_post,
},
],
"block": [
{
"name": "math_block_eqno",
"rex": re.compile(r"^\${2}([^$]*?)\${2}\s*?\(([^)$\r\n]+?)\)", re.M),
"tmpl": '<section class="eqno">\n<eqn>{0}</eqn><span>({1})</span>\n</section>\n', # noqa: E501
"tag": "$$",
},
{
"name": "math_block",
"rex": re.compile(r"^\${2}([^$]*?)\${2}", re.M),
"tmpl": "<section>\n<eqn>{0}</eqn>\n</section>\n",
"tag": "$$",
},
],
},
}
4 changes: 2 additions & 2 deletions tests/test_texmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -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$"
Expand All @@ -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 = []
Expand Down