Skip to content

Commit

Permalink
Update math tests and only allow matches starting from beginning posi…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
chrisjsewell committed Mar 27, 2020
1 parent a76e350 commit 49b3aaa
Show file tree
Hide file tree
Showing 44 changed files with 878 additions and 2,122 deletions.
2 changes: 1 addition & 1 deletion markdown_it/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .main import MarkdownIt # noqa: F401


__version__ = "0.3.1"
__version__ = "0.3.2"
98 changes: 51 additions & 47 deletions markdown_it/extensions/texmath/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,28 @@ def render_math_block(self, tokens, idx, options, env):
md.add_render_rule(rule_block["name"], render_math_block)


def applyRule(rule, string: str, beg, inBlockquote):

pre = string.startswith(rule["tag"], beg) and (
rule["pre"](string, beg) if "pre" in rule else True
)

match = rule["rex"].search(string, beg) if pre else False
post = True
if match:
lastIndex = match.end() - 1
if "post" in rule:
post = (
rule["post"](string, lastIndex) # valid post-condition
# remove evil blockquote bug (https:#github.com/goessner/mdmath/issues/50)
and (not inBlockquote or "\n" not in match.group(1))
)
def applyRule(rule, string: str, begin, inBlockquote):

if not (
string.startswith(rule["tag"], begin)
and (rule["pre"](string, begin) if "pre" in rule else True)
):
return False

match = rule["rex"].match(string[begin:]) # type: re.Match

if not match or match.start() != 0:
return False

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


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

state.pos = res.end()
state.pos += res.end()

return bool(res)

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

def make_block_func(rule):
def _func(state, begLine, endLine, silent):
res = applyRule(
rule,
state.src,
state.bMarks[begLine] + state.tShift[begLine],
state.parentType == "blockquote",
)
begin = state.bMarks[begLine] + state.tShift[begLine]
res = applyRule(rule, state.src, begin, state.parentType == "blockquote")
if res:
if not silent:
token = state.push(rule["name"], "math", 0)
Expand All @@ -88,7 +88,7 @@ def _func(state, begLine, endLine, silent):
token.markup = rule["tag"]

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

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

state.pos = res.end()
state.pos = begin + res.end()

return bool(res)

Expand Down Expand Up @@ -146,7 +146,7 @@ def render(tex, displayMode, macros):
"inline": [
{
"name": "math_inline",
"rex": re.compile(r"\\\((.+?)\\\)"),
"rex": re.compile(r"^\\\((.+?)\\\)"),
"tmpl": "<eq>{0}</eq>",
"tag": "\\(",
}
Expand All @@ -155,15 +155,15 @@ def render(tex, displayMode, macros):
{
"name": "math_block_eqno",
"rex": re.compile(
r"\\\[(((?!\\\]|\\\[)[\s\S])+?)\\\]\s*?\(([^)$\r\n]+?)\)", re.M
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><eqn>{0}</eqn></section>",
"rex": re.compile(r"^\\\[([\s\S]+?)\\\]", re.M),
"tmpl": "<section>\n<eqn>{0}</eqn>\n</section>\n",
"tag": "\\[",
},
],
Expand All @@ -172,7 +172,7 @@ def render(tex, displayMode, macros):
"inline": [
{
"name": "math_inline",
"rex": re.compile(r"\$`(.+?)`\$"),
"rex": re.compile(r"^\$`(.+?)`\$"),
"tmpl": "<eq>{0}</eq>",
"tag": "$`",
}
Expand All @@ -181,15 +181,15 @@ def render(tex, displayMode, macros):
{
"name": "math_block_eqno",
"rex": re.compile(
r"`{3}math\s+?([^`]+?)\s+?`{3}\s*?\(([^)$\r\n]+?)\)", re.M
r"^`{3}math\s+?([^`]+?)\s+?`{3}\s*?\(([^)$\r\n]+?)\)", re.M
),
"tmpl": '<section class="eqno"><eqn>{0}</eqn><span>({1})</span></section>',
"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><eqn>{0}</eqn></section>",
"rex": re.compile(r"^`{3}math\s+?([^`]+?)\s+?`{3}", re.M),
"tmpl": "<section>\n<eqn>{0}</eqn>\n</section>\n",
"tag": "```math",
},
],
Expand All @@ -198,21 +198,21 @@ def render(tex, displayMode, macros):
"inline": [
{
"name": "math_inline",
"rex": re.compile(r"`{2}([^`]+?)`{2}"),
"rex": re.compile(r"^`{2}([^`]+?)`{2}"),
"tmpl": "<eq>{0}</eq>",
"tag": "``",
},
{
"name": "math_inline",
"rex": re.compile(r"\$(\S[^$\r\n]*?[^\s\\]{1}?)\$"),
"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}?)\$"),
"rex": re.compile(r"^\$([^$\s\\]{1}?)\$"),
"tmpl": "<eq>{0}</eq>",
"tag": "$",
"pre": dollar_pre,
Expand All @@ -223,14 +223,14 @@ def render(tex, displayMode, macros):
{
"name": "math_block_eqno",
"rex": re.compile(
r"`{3}math\s+?([^`]+?)\s+?`{3}\s*?\(([^)$\r\n]+?)\)", re.M
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),
"rex": re.compile(r"^`{3}math\s+?([^`]+?)\s+?`{3}", re.M),
"tmpl": "<section><eqn>{0}</eqn></section>",
"tag": "```math",
},
Expand All @@ -240,21 +240,23 @@ def render(tex, displayMode, macros):
"inline": [
{
"name": "math_inline",
"rex": re.compile(r"\${2}([^$\r\n]*?)\${2}"),
"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),
"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),
"rex": re.compile(r"^\${2}([^$]*?)\${2}", re.M),
"tmpl": "<section><eqn>{0}</eqn></section>",
"tag": "$$",
},
Expand All @@ -264,15 +266,15 @@ def render(tex, displayMode, macros):
"inline": [
{
"name": "math_inline",
"rex": re.compile(r"\$(\S[^$\r\n]*?[^\s\\]{1}?)\$"),
"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}?)\$"),
"rex": re.compile(r"^\$([^$\s\\]{1}?)\$"),
"tmpl": "<eq>{0}</eq>",
"tag": "$",
"pre": dollar_pre,
Expand All @@ -282,13 +284,15 @@ def render(tex, displayMode, macros):
"block": [
{
"name": "math_block_eqno",
"rex": re.compile(r"\${2}([^$]*?)\${2}\s*?\(([^)$\r\n]+?)\)", re.M),
"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),
"rex": re.compile(r"^\${2}([^$]*?)\${2}", re.M),
"tmpl": "<section>\n<eqn>{0}</eqn>\n</section>\n",
"tag": "$$",
},
Expand Down
Loading

0 comments on commit 49b3aaa

Please sign in to comment.