Reformat using black
This commit is contained in:
parent
efee4ad081
commit
a87fb49918
221 changed files with 19127 additions and 7310 deletions
|
@ -12,22 +12,28 @@ from lxml.etree import ParserError, XMLSyntaxError
|
|||
from judge.highlight_code import highlight_code
|
||||
from judge.jinja2.markdown.lazy_load import lazy_load as lazy_load_processor
|
||||
from judge.jinja2.markdown.math import MathInlineGrammar, MathInlineLexer, MathRenderer
|
||||
from judge.jinja2.markdown.spoiler import SpoilerInlineGrammar, SpoilerInlineLexer, SpoilerRenderer
|
||||
from judge.jinja2.markdown.spoiler import (
|
||||
SpoilerInlineGrammar,
|
||||
SpoilerInlineLexer,
|
||||
SpoilerRenderer,
|
||||
)
|
||||
from judge.utils.camo import client as camo_client
|
||||
from judge.utils.texoid import TEXOID_ENABLED, TexoidRenderer
|
||||
from .. import registry
|
||||
|
||||
logger = logging.getLogger('judge.html')
|
||||
logger = logging.getLogger("judge.html")
|
||||
|
||||
NOFOLLOW_WHITELIST = settings.NOFOLLOW_EXCLUDED
|
||||
|
||||
|
||||
class CodeSafeInlineGrammar(mistune.InlineGrammar):
|
||||
double_emphasis = re.compile(r'^\*{2}([\s\S]+?)()\*{2}(?!\*)') # **word**
|
||||
emphasis = re.compile(r'^\*((?:\*\*|[^\*])+?)()\*(?!\*)') # *word*
|
||||
double_emphasis = re.compile(r"^\*{2}([\s\S]+?)()\*{2}(?!\*)") # **word**
|
||||
emphasis = re.compile(r"^\*((?:\*\*|[^\*])+?)()\*(?!\*)") # *word*
|
||||
|
||||
|
||||
class AwesomeInlineGrammar(MathInlineGrammar, SpoilerInlineGrammar, CodeSafeInlineGrammar):
|
||||
class AwesomeInlineGrammar(
|
||||
MathInlineGrammar, SpoilerInlineGrammar, CodeSafeInlineGrammar
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
|
@ -37,8 +43,8 @@ class AwesomeInlineLexer(MathInlineLexer, SpoilerInlineLexer, mistune.InlineLexe
|
|||
|
||||
class AwesomeRenderer(MathRenderer, SpoilerRenderer, mistune.Renderer):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.nofollow = kwargs.pop('nofollow', True)
|
||||
self.texoid = TexoidRenderer() if kwargs.pop('texoid', False) else None
|
||||
self.nofollow = kwargs.pop("nofollow", True)
|
||||
self.texoid = TexoidRenderer() if kwargs.pop("texoid", False) else None
|
||||
self.parser = HTMLParser()
|
||||
super(AwesomeRenderer, self).__init__(*args, **kwargs)
|
||||
|
||||
|
@ -51,18 +57,18 @@ class AwesomeRenderer(MathRenderer, SpoilerRenderer, mistune.Renderer):
|
|||
else:
|
||||
if url.netloc and url.netloc not in NOFOLLOW_WHITELIST:
|
||||
return ' rel="nofollow"'
|
||||
return ''
|
||||
return ""
|
||||
|
||||
def autolink(self, link, is_email=False):
|
||||
text = link = mistune.escape(link)
|
||||
if is_email:
|
||||
link = 'mailto:%s' % link
|
||||
link = "mailto:%s" % link
|
||||
return '<a href="%s"%s>%s</a>' % (link, self._link_rel(link), text)
|
||||
|
||||
def table(self, header, body):
|
||||
return (
|
||||
'<table class="table">\n<thead>%s</thead>\n'
|
||||
'<tbody>\n%s</tbody>\n</table>\n'
|
||||
"<tbody>\n%s</tbody>\n</table>\n"
|
||||
) % (header, body)
|
||||
|
||||
def link(self, link, title, text):
|
||||
|
@ -70,40 +76,53 @@ class AwesomeRenderer(MathRenderer, SpoilerRenderer, mistune.Renderer):
|
|||
if not title:
|
||||
return '<a href="%s"%s>%s</a>' % (link, self._link_rel(link), text)
|
||||
title = mistune.escape(title, quote=True)
|
||||
return '<a href="%s" title="%s"%s>%s</a>' % (link, title, self._link_rel(link), text)
|
||||
return '<a href="%s" title="%s"%s>%s</a>' % (
|
||||
link,
|
||||
title,
|
||||
self._link_rel(link),
|
||||
text,
|
||||
)
|
||||
|
||||
def block_code(self, code, lang=None):
|
||||
if not lang:
|
||||
return '\n<pre><code>%s</code></pre>\n' % mistune.escape(code).rstrip()
|
||||
return "\n<pre><code>%s</code></pre>\n" % mistune.escape(code).rstrip()
|
||||
return highlight_code(code, lang)
|
||||
|
||||
def block_html(self, html):
|
||||
if self.texoid and html.startswith('<latex'):
|
||||
attr = html[6:html.index('>')]
|
||||
latex = html[html.index('>') + 1:html.rindex('<')]
|
||||
if self.texoid and html.startswith("<latex"):
|
||||
attr = html[6 : html.index(">")]
|
||||
latex = html[html.index(">") + 1 : html.rindex("<")]
|
||||
latex = self.parser.unescape(latex)
|
||||
result = self.texoid.get_result(latex)
|
||||
if not result:
|
||||
return '<pre>%s</pre>' % mistune.escape(latex, smart_amp=False)
|
||||
elif 'error' not in result:
|
||||
img = ('''<img src="%(svg)s" onerror="this.src='%(png)s';this.onerror=null"'''
|
||||
'width="%(width)s" height="%(height)s"%(tail)s>') % {
|
||||
'svg': result['svg'], 'png': result['png'],
|
||||
'width': result['meta']['width'], 'height': result['meta']['height'],
|
||||
'tail': ' /' if self.options.get('use_xhtml') else '',
|
||||
return "<pre>%s</pre>" % mistune.escape(latex, smart_amp=False)
|
||||
elif "error" not in result:
|
||||
img = (
|
||||
'''<img src="%(svg)s" onerror="this.src='%(png)s';this.onerror=null"'''
|
||||
'width="%(width)s" height="%(height)s"%(tail)s>'
|
||||
) % {
|
||||
"svg": result["svg"],
|
||||
"png": result["png"],
|
||||
"width": result["meta"]["width"],
|
||||
"height": result["meta"]["height"],
|
||||
"tail": " /" if self.options.get("use_xhtml") else "",
|
||||
}
|
||||
style = ['max-width: 100%',
|
||||
'height: %s' % result['meta']['height'],
|
||||
'max-height: %s' % result['meta']['height'],
|
||||
'width: %s' % result['meta']['height']]
|
||||
if 'inline' in attr:
|
||||
tag = 'span'
|
||||
style = [
|
||||
"max-width: 100%",
|
||||
"height: %s" % result["meta"]["height"],
|
||||
"max-height: %s" % result["meta"]["height"],
|
||||
"width: %s" % result["meta"]["height"],
|
||||
]
|
||||
if "inline" in attr:
|
||||
tag = "span"
|
||||
else:
|
||||
tag = 'div'
|
||||
style += ['text-align: center']
|
||||
return '<%s style="%s">%s</%s>' % (tag, ';'.join(style), img, tag)
|
||||
tag = "div"
|
||||
style += ["text-align: center"]
|
||||
return '<%s style="%s">%s</%s>' % (tag, ";".join(style), img, tag)
|
||||
else:
|
||||
return '<pre>%s</pre>' % mistune.escape(result['error'], smart_amp=False)
|
||||
return "<pre>%s</pre>" % mistune.escape(
|
||||
result["error"], smart_amp=False
|
||||
)
|
||||
return super(AwesomeRenderer, self).block_html(html)
|
||||
|
||||
def header(self, text, level, *args, **kwargs):
|
||||
|
@ -113,30 +132,41 @@ class AwesomeRenderer(MathRenderer, SpoilerRenderer, mistune.Renderer):
|
|||
@registry.filter
|
||||
def markdown(value, style, math_engine=None, lazy_load=False):
|
||||
styles = settings.MARKDOWN_STYLES.get(style, settings.MARKDOWN_DEFAULT_STYLE)
|
||||
escape = styles.get('safe_mode', True)
|
||||
nofollow = styles.get('nofollow', True)
|
||||
texoid = TEXOID_ENABLED and styles.get('texoid', False)
|
||||
math = hasattr(settings, 'MATHOID_URL') and styles.get('math', False)
|
||||
escape = styles.get("safe_mode", True)
|
||||
nofollow = styles.get("nofollow", True)
|
||||
texoid = TEXOID_ENABLED and styles.get("texoid", False)
|
||||
math = hasattr(settings, "MATHOID_URL") and styles.get("math", False)
|
||||
|
||||
post_processors = []
|
||||
if styles.get('use_camo', False) and camo_client is not None:
|
||||
if styles.get("use_camo", False) and camo_client is not None:
|
||||
post_processors.append(camo_client.update_tree)
|
||||
if lazy_load:
|
||||
post_processors.append(lazy_load_processor)
|
||||
|
||||
renderer = AwesomeRenderer(escape=escape, nofollow=nofollow, texoid=texoid,
|
||||
math=math and math_engine is not None, math_engine=math_engine)
|
||||
markdown = mistune.Markdown(renderer=renderer, inline=AwesomeInlineLexer,
|
||||
parse_block_html=1, parse_inline_html=1)
|
||||
renderer = AwesomeRenderer(
|
||||
escape=escape,
|
||||
nofollow=nofollow,
|
||||
texoid=texoid,
|
||||
math=math and math_engine is not None,
|
||||
math_engine=math_engine,
|
||||
)
|
||||
markdown = mistune.Markdown(
|
||||
renderer=renderer,
|
||||
inline=AwesomeInlineLexer,
|
||||
parse_block_html=1,
|
||||
parse_inline_html=1,
|
||||
)
|
||||
result = markdown(value)
|
||||
if post_processors:
|
||||
try:
|
||||
tree = html.fromstring(result, parser=html.HTMLParser(recover=True))
|
||||
except (XMLSyntaxError, ParserError) as e:
|
||||
if result and (not isinstance(e, ParserError) or e.args[0] != 'Document is empty'):
|
||||
logger.exception('Failed to parse HTML string')
|
||||
tree = html.Element('div')
|
||||
if result and (
|
||||
not isinstance(e, ParserError) or e.args[0] != "Document is empty"
|
||||
):
|
||||
logger.exception("Failed to parse HTML string")
|
||||
tree = html.Element("div")
|
||||
for processor in post_processors:
|
||||
processor(tree)
|
||||
result = html.tostring(tree, encoding='unicode')
|
||||
result = html.tostring(tree, encoding="unicode")
|
||||
return Markup(result)
|
||||
|
|
|
@ -5,16 +5,16 @@ from lxml import html
|
|||
|
||||
|
||||
def lazy_load(tree):
|
||||
blank = static('blank.gif')
|
||||
for img in tree.xpath('.//img'):
|
||||
src = img.get('src', '')
|
||||
if src.startswith('data') or '-math' in img.get('class', ''):
|
||||
blank = static("blank.gif")
|
||||
for img in tree.xpath(".//img"):
|
||||
src = img.get("src", "")
|
||||
if src.startswith("data") or "-math" in img.get("class", ""):
|
||||
continue
|
||||
noscript = html.Element('noscript')
|
||||
noscript = html.Element("noscript")
|
||||
copy = deepcopy(img)
|
||||
copy.tail = ''
|
||||
copy.tail = ""
|
||||
noscript.append(copy)
|
||||
img.addprevious(noscript)
|
||||
img.set('data-src', src)
|
||||
img.set('src', blank)
|
||||
img.set('class', img.get('class') + ' unveil' if img.get('class') else 'unveil')
|
||||
img.set("data-src", src)
|
||||
img.set("src", blank)
|
||||
img.set("class", img.get("class") + " unveil" if img.get("class") else "unveil")
|
||||
|
|
|
@ -6,13 +6,13 @@ from django.conf import settings
|
|||
|
||||
from judge.utils.mathoid import MathoidMathParser
|
||||
|
||||
mistune._pre_tags.append('latex')
|
||||
mistune._pre_tags.append("latex")
|
||||
|
||||
|
||||
class MathInlineGrammar(mistune.InlineGrammar):
|
||||
block_math = re.compile(r'^\$\$(.*?)\$\$|^\\\[(.*?)\\\]', re.DOTALL)
|
||||
math = re.compile(r'^~(.*?)~|^\\\((.*?)\\\)', re.DOTALL)
|
||||
text = re.compile(r'^[\s\S]+?(?=[\\<!\[_*`~$]|\\[\[(]|https?://| {2,}\n|$)')
|
||||
block_math = re.compile(r"^\$\$(.*?)\$\$|^\\\[(.*?)\\\]", re.DOTALL)
|
||||
math = re.compile(r"^~(.*?)~|^\\\((.*?)\\\)", re.DOTALL)
|
||||
text = re.compile(r"^[\s\S]+?(?=[\\<!\[_*`~$]|\\[\[(]|https?://| {2,}\n|$)")
|
||||
|
||||
|
||||
class MathInlineLexer(mistune.InlineLexer):
|
||||
|
@ -21,8 +21,10 @@ class MathInlineLexer(mistune.InlineLexer):
|
|||
def __init__(self, *args, **kwargs):
|
||||
self.default_rules = self.default_rules[:]
|
||||
self.inline_html_rules = self.default_rules
|
||||
self.default_rules.insert(self.default_rules.index('strikethrough') + 1, 'math')
|
||||
self.default_rules.insert(self.default_rules.index('strikethrough') + 1, 'block_math')
|
||||
self.default_rules.insert(self.default_rules.index("strikethrough") + 1, "math")
|
||||
self.default_rules.insert(
|
||||
self.default_rules.index("strikethrough") + 1, "block_math"
|
||||
)
|
||||
super(MathInlineLexer, self).__init__(*args, **kwargs)
|
||||
|
||||
def output_block_math(self, m):
|
||||
|
@ -35,14 +37,14 @@ class MathInlineLexer(mistune.InlineLexer):
|
|||
tag = m.group(1)
|
||||
text = m.group(3)
|
||||
if self._parse_inline_html and text:
|
||||
if tag == 'a':
|
||||
if tag == "a":
|
||||
self._in_link = True
|
||||
text = self.output(text)
|
||||
self._in_link = False
|
||||
else:
|
||||
text = self.output(text)
|
||||
extra = m.group(2) or ''
|
||||
html = '<%s%s>%s</%s>' % (tag, extra, text, tag)
|
||||
extra = m.group(2) or ""
|
||||
html = "<%s%s>%s</%s>" % (tag, extra, text, tag)
|
||||
else:
|
||||
html = m.group(0)
|
||||
return self.renderer.inline_html(html)
|
||||
|
@ -50,18 +52,18 @@ class MathInlineLexer(mistune.InlineLexer):
|
|||
|
||||
class MathRenderer(mistune.Renderer):
|
||||
def __init__(self, *args, **kwargs):
|
||||
if kwargs.pop('math', False) and settings.MATHOID_URL != False:
|
||||
self.mathoid = MathoidMathParser(kwargs.pop('math_engine', None) or 'svg')
|
||||
if kwargs.pop("math", False) and settings.MATHOID_URL != False:
|
||||
self.mathoid = MathoidMathParser(kwargs.pop("math_engine", None) or "svg")
|
||||
else:
|
||||
self.mathoid = None
|
||||
super(MathRenderer, self).__init__(*args, **kwargs)
|
||||
|
||||
def block_math(self, math):
|
||||
if self.mathoid is None or not math:
|
||||
return r'\[%s\]' % mistune.escape(str(math))
|
||||
return r"\[%s\]" % mistune.escape(str(math))
|
||||
return self.mathoid.display_math(math)
|
||||
|
||||
def math(self, math):
|
||||
if self.mathoid is None or not math:
|
||||
return r'\(%s\)' % mistune.escape(str(math))
|
||||
return self.mathoid.inline_math(math)
|
||||
return r"\(%s\)" % mistune.escape(str(math))
|
||||
return self.mathoid.inline_math(math)
|
||||
|
|
|
@ -3,25 +3,28 @@ import mistune
|
|||
|
||||
|
||||
class SpoilerInlineGrammar(mistune.InlineGrammar):
|
||||
spoiler = re.compile(r'^\|\|(.+?)\s+([\s\S]+?)\s*\|\|')
|
||||
spoiler = re.compile(r"^\|\|(.+?)\s+([\s\S]+?)\s*\|\|")
|
||||
|
||||
|
||||
class SpoilerInlineLexer(mistune.InlineLexer):
|
||||
grammar_class = SpoilerInlineGrammar
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.default_rules.insert(0, 'spoiler')
|
||||
self.default_rules.insert(0, "spoiler")
|
||||
super(SpoilerInlineLexer, self).__init__(*args, **kwargs)
|
||||
|
||||
|
||||
def output_spoiler(self, m):
|
||||
return self.renderer.spoiler(m.group(1), m.group(2))
|
||||
|
||||
|
||||
class SpoilerRenderer(mistune.Renderer):
|
||||
def spoiler(self, summary, text):
|
||||
return '''<details>
|
||||
return """<details>
|
||||
<summary style="color: brown">
|
||||
<span class="spoiler-summary">%s</span>
|
||||
</summary>
|
||||
<div class="spoiler-text">%s</div>
|
||||
</details>''' % (summary, text)
|
||||
</details>""" % (
|
||||
summary,
|
||||
text,
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue