NDOJ/judge/jinja2/markdown/spoiler.py

31 lines
862 B
Python
Raw Normal View History

2021-12-16 20:16:49 +00:00
import re
import mistune
class SpoilerInlineGrammar(mistune.InlineGrammar):
2022-05-14 17:57:27 +00:00
spoiler = re.compile(r"^\|\|(.+?)\s+([\s\S]+?)\s*\|\|")
2021-12-16 20:16:49 +00:00
class SpoilerInlineLexer(mistune.InlineLexer):
grammar_class = SpoilerInlineGrammar
def __init__(self, *args, **kwargs):
2022-05-14 17:57:27 +00:00
self.default_rules.insert(0, "spoiler")
2021-12-16 20:16:49 +00:00
super(SpoilerInlineLexer, self).__init__(*args, **kwargs)
2022-05-14 17:57:27 +00:00
2021-12-16 20:16:49 +00:00
def output_spoiler(self, m):
return self.renderer.spoiler(m.group(1), m.group(2))
class SpoilerRenderer(mistune.Renderer):
def spoiler(self, summary, text):
2022-05-14 17:57:27 +00:00
return """<details>
2021-12-16 20:16:49 +00:00
<summary style="color: brown">
<span class="spoiler-summary">%s</span>
</summary>
<div class="spoiler-text">%s</div>
2022-05-14 17:57:27 +00:00
</details>""" % (
summary,
text,
)