Reformat using black

This commit is contained in:
cuom1999 2022-05-14 12:57:27 -05:00
parent efee4ad081
commit a87fb49918
221 changed files with 19127 additions and 7310 deletions

View file

@ -12,25 +12,25 @@ from mistune import escape
from judge.utils.file_cache import HashFileCache
from judge.utils.unicode import utf8bytes, utf8text
logger = logging.getLogger('judge.mathoid')
reescape = re.compile(r'(?<!\\)(?:\\{2})*[$]')
logger = logging.getLogger("judge.mathoid")
reescape = re.compile(r"(?<!\\)(?:\\{2})*[$]")
REPLACES = [
('\u2264', r'\le'),
('\u2265', r'\ge'),
('\u2026', '...'),
('\u2212', '-'),
('&le;', r'\le'),
('&ge;', r'\ge'),
('&lt;', '<'),
('&gt;', '>'),
('&amp;', '&'),
('&#8722;', '-'),
('&#8804;', r'\le'),
('&#8805;', r'\ge'),
('&#8230;', '...'),
(r'\lt', '<'),
(r'\gt', '>'),
("\u2264", r"\le"),
("\u2265", r"\ge"),
("\u2026", "..."),
("\u2212", "-"),
("&le;", r"\le"),
("&ge;", r"\ge"),
("&lt;", "<"),
("&gt;", ">"),
("&amp;", "&"),
("&#8722;", "-"),
("&#8804;", r"\le"),
("&#8805;", r"\ge"),
("&#8230;", "..."),
(r"\lt", "<"),
(r"\gt", ">"),
]
@ -41,15 +41,17 @@ def format_math(math):
class MathoidMathParser(object):
types = ('svg', 'mml', 'tex', 'jax')
types = ("svg", "mml", "tex", "jax")
def __init__(self, type):
self.type = type
self.mathoid_url = settings.MATHOID_URL
self.cache = HashFileCache(settings.MATHOID_CACHE_ROOT,
settings.MATHOID_CACHE_URL,
settings.MATHOID_GZIP)
self.cache = HashFileCache(
settings.MATHOID_CACHE_ROOT,
settings.MATHOID_CACHE_URL,
settings.MATHOID_GZIP,
)
mml_cache = settings.MATHOID_MML_CACHE
self.mml_cache = mml_cache and caches[mml_cache]
@ -61,69 +63,80 @@ class MathoidMathParser(object):
self.cache.create(hash)
try:
response = requests.post(self.mathoid_url, data={
'q': reescape.sub(lambda m: '\\' + m.group(0), formula).encode('utf-8'),
'type': 'tex' if formula.startswith(r'\displaystyle') else 'inline-tex',
})
response = requests.post(
self.mathoid_url,
data={
"q": reescape.sub(lambda m: "\\" + m.group(0), formula).encode(
"utf-8"
),
"type": "tex"
if formula.startswith(r"\displaystyle")
else "inline-tex",
},
)
response.raise_for_status()
data = response.json()
except requests.ConnectionError:
logger.exception('Failed to connect to mathoid for: %s', formula)
logger.exception("Failed to connect to mathoid for: %s", formula)
return
except requests.HTTPError as e:
logger.error('Mathoid failed to render: %s\n%s', formula, e.response.text)
logger.error("Mathoid failed to render: %s\n%s", formula, e.response.text)
return
except Exception:
logger.exception('Failed to connect to mathoid for: %s', formula)
logger.exception("Failed to connect to mathoid for: %s", formula)
return
if not data['success']:
logger.error('Mathoid failure for: %s\n%s', formula, data)
if not data["success"]:
logger.error("Mathoid failure for: %s\n%s", formula, data)
return
if any(i not in data for i in ('mml', 'png', 'svg', 'mathoidStyle')):
logger.error('Mathoid did not return required information (mml, png, svg, mathoidStyle needed):\n%s', data)
if any(i not in data for i in ("mml", "png", "svg", "mathoidStyle")):
logger.error(
"Mathoid did not return required information (mml, png, svg, mathoidStyle needed):\n%s",
data,
)
return
css = data['mathoidStyle']
mml = data['mml']
css = data["mathoidStyle"]
mml = data["mml"]
result = {
'css': css, 'mml': mml,
'png': self.cache.cache_data(hash, 'png', bytearray(data['png']['data'])),
'svg': self.cache.cache_data(hash, 'svg', data['svg'].encode('utf-8')),
"css": css,
"mml": mml,
"png": self.cache.cache_data(hash, "png", bytearray(data["png"]["data"])),
"svg": self.cache.cache_data(hash, "svg", data["svg"].encode("utf-8")),
}
self.cache.cache_data(hash, 'mml', mml.encode('utf-8'), url=False, gzip=False)
self.cache.cache_data(hash, 'css', css.encode('utf-8'), url=False, gzip=False)
self.cache.cache_data(hash, "mml", mml.encode("utf-8"), url=False, gzip=False)
self.cache.cache_data(hash, "css", css.encode("utf-8"), url=False, gzip=False)
return result
def query_cache(self, hash):
result = {
'svg': self.cache.get_url(hash, 'svg'),
'png': self.cache.get_url(hash, 'png'),
"svg": self.cache.get_url(hash, "svg"),
"png": self.cache.get_url(hash, "png"),
}
key = 'mathoid:css:' + hash
css = result['css'] = self.css_cache.get(key)
key = "mathoid:css:" + hash
css = result["css"] = self.css_cache.get(key)
if css is None:
css = result['css'] = self.cache.read_data(hash, 'css').decode('utf-8')
css = result["css"] = self.cache.read_data(hash, "css").decode("utf-8")
self.css_cache.set(key, css, self.mml_cache_ttl)
mml = None
if self.mml_cache:
mml = result['mml'] = self.mml_cache.get('mathoid:mml:' + hash)
mml = result["mml"] = self.mml_cache.get("mathoid:mml:" + hash)
if mml is None:
mml = result['mml'] = self.cache.read_data(hash, 'mml').decode('utf-8')
mml = result["mml"] = self.cache.read_data(hash, "mml").decode("utf-8")
if self.mml_cache:
self.mml_cache.set('mathoid:mml:' + hash, mml, self.mml_cache_ttl)
self.mml_cache.set("mathoid:mml:" + hash, mml, self.mml_cache_ttl)
return result
def get_result(self, formula):
if self.type == 'tex':
if self.type == "tex":
return
hash = hashlib.sha1(utf8bytes(formula)).hexdigest()
formula = utf8text(formula)
if self.cache.has_file(hash, 'css'):
if self.cache.has_file(hash, "css"):
result = self.query_cache(hash)
else:
result = self.query_mathoid(formula, hash)
@ -131,55 +144,76 @@ class MathoidMathParser(object):
if not result:
return None
result['tex'] = formula
result['display'] = formula.startswith(r'\displaystyle')
result["tex"] = formula
result["display"] = formula.startswith(r"\displaystyle")
return {
'mml': self.output_mml,
'msp': self.output_msp,
'svg': self.output_svg,
'jax': self.output_jax,
'png': self.output_png,
'raw': lambda x: x,
"mml": self.output_mml,
"msp": self.output_msp,
"svg": self.output_svg,
"jax": self.output_jax,
"png": self.output_png,
"raw": lambda x: x,
}[self.type](result)
def output_mml(self, result):
return result['mml']
return result["mml"]
def output_msp(self, result):
# 100% MediaWiki compatibility.
return format_html('<span class="{5}-math">'
'<span class="mwe-math-mathml-{5} mwe-math-mathml-a11y"'
' style="display: none;">{0}</span>'
'<img src="{1}" class="mwe-math-fallback-image-{5}"'
' onerror="this.src=\'{2}\';this.onerror=null"'
' aria-hidden="true" style="{3}" alt="{4}"></span>',
mark_safe(result['mml']), result['svg'], result['png'], result['css'], result['tex'],
['inline', 'display'][result['display']])
return format_html(
'<span class="{5}-math">'
'<span class="mwe-math-mathml-{5} mwe-math-mathml-a11y"'
' style="display: none;">{0}</span>'
'<img src="{1}" class="mwe-math-fallback-image-{5}"'
" onerror=\"this.src='{2}';this.onerror=null\""
' aria-hidden="true" style="{3}" alt="{4}"></span>',
mark_safe(result["mml"]),
result["svg"],
result["png"],
result["css"],
result["tex"],
["inline", "display"][result["display"]],
)
def output_jax(self, result):
return format_html('<span class="{4}">'
'''<img class="tex-image" src="{0}" style="{2}" alt="{3}"'''
''' onerror="this.src='{1}';this.onerror=null">'''
'''<span class="tex-text" style="display:none">{5}{3}{5}</span>'''
'</span>',
result['svg'], result['png'], result['css'], result['tex'],
['inline-math', 'display-math'][result['display']], ['~', '$$'][result['display']])
return format_html(
'<span class="{4}">'
'''<img class="tex-image" src="{0}" style="{2}" alt="{3}"'''
""" onerror="this.src='{1}';this.onerror=null">"""
"""<span class="tex-text" style="display:none">{5}{3}{5}</span>"""
"</span>",
result["svg"],
result["png"],
result["css"],
result["tex"],
["inline-math", "display-math"][result["display"]],
["~", "$$"][result["display"]],
)
def output_svg(self, result):
return format_html('<img class="{4}" src="{0}" style="{2}" alt="{3}" '
'''onerror="this.src='{1}';this.onerror=null">''',
result['svg'], result['png'], result['css'], result['tex'],
['inline-math', 'display-math'][result['display']])
return format_html(
'<img class="{4}" src="{0}" style="{2}" alt="{3}" '
"""onerror="this.src='{1}';this.onerror=null">""",
result["svg"],
result["png"],
result["css"],
result["tex"],
["inline-math", "display-math"][result["display"]],
)
def output_png(self, result):
return format_html('<img class="{3}" src="{0}" style="{1}" alt="{2}">',
result['png'], result['css'], result['tex'],
['inline-math', 'display-math'][result['display']])
return format_html(
'<img class="{3}" src="{0}" style="{1}" alt="{2}">',
result["png"],
result["css"],
result["tex"],
["inline-math", "display-math"][result["display"]],
)
def display_math(self, math):
math = format_math(math)
return self.get_result(r'\displaystyle ' + math) or r'\[%s\]' % escape(math)
return self.get_result(r"\displaystyle " + math) or r"\[%s\]" % escape(math)
def inline_math(self, math):
math = format_math(math)
return self.get_result(math) or r'\(%s\)' % escape(math)
return self.get_result(math) or r"\(%s\)" % escape(math)