NDOJ/judge/jinja2/markdown/math.py

70 lines
2.3 KiB
Python
Raw Normal View History

2020-01-21 06:35:58 +00:00
import re
import mistune
2020-11-22 23:17:42 +00:00
from django.conf import settings
2020-01-21 06:35:58 +00:00
from judge.utils.mathoid import MathoidMathParser
2022-05-14 17:57:27 +00:00
mistune._pre_tags.append("latex")
2020-01-21 06:35:58 +00:00
class MathInlineGrammar(mistune.InlineGrammar):
2022-05-14 17:57:27 +00:00
block_math = re.compile(r"^\$\$(.*?)\$\$|^\\\[(.*?)\\\]", re.DOTALL)
math = re.compile(r"^~(.*?)~|^\\\((.*?)\\\)", re.DOTALL)
text = re.compile(r"^[\s\S]+?(?=[\\<!\[_*`~$]|\\[\[(]|https?://| {2,}\n|$)")
2020-01-21 06:35:58 +00:00
class MathInlineLexer(mistune.InlineLexer):
grammar_class = MathInlineGrammar
def __init__(self, *args, **kwargs):
self.default_rules = self.default_rules[:]
self.inline_html_rules = self.default_rules
2022-05-14 17:57:27 +00:00
self.default_rules.insert(self.default_rules.index("strikethrough") + 1, "math")
self.default_rules.insert(
self.default_rules.index("strikethrough") + 1, "block_math"
)
2020-01-21 06:35:58 +00:00
super(MathInlineLexer, self).__init__(*args, **kwargs)
def output_block_math(self, m):
return self.renderer.block_math(m.group(1) or m.group(2))
def output_math(self, m):
return self.renderer.math(m.group(1) or m.group(2))
def output_inline_html(self, m):
tag = m.group(1)
text = m.group(3)
if self._parse_inline_html and text:
2022-05-14 17:57:27 +00:00
if tag == "a":
2020-01-21 06:35:58 +00:00
self._in_link = True
text = self.output(text)
self._in_link = False
else:
text = self.output(text)
2022-05-14 17:57:27 +00:00
extra = m.group(2) or ""
html = "<%s%s>%s</%s>" % (tag, extra, text, tag)
2020-01-21 06:35:58 +00:00
else:
html = m.group(0)
return self.renderer.inline_html(html)
class MathRenderer(mistune.Renderer):
def __init__(self, *args, **kwargs):
2022-05-14 17:57:27 +00:00
if kwargs.pop("math", False) and settings.MATHOID_URL != False:
self.mathoid = MathoidMathParser(kwargs.pop("math_engine", None) or "svg")
2020-01-21 06:35:58 +00:00
else:
self.mathoid = None
super(MathRenderer, self).__init__(*args, **kwargs)
def block_math(self, math):
if self.mathoid is None or not math:
2022-05-14 17:57:27 +00:00
return r"\[%s\]" % mistune.escape(str(math))
2020-01-21 06:35:58 +00:00
return self.mathoid.display_math(math)
def math(self, math):
if self.mathoid is None or not math:
2022-05-14 17:57:27 +00:00
return r"\(%s\)" % mistune.escape(str(math))
return self.mathoid.inline_math(math)