summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBotond Hende <nettingman@gmail.com>2024-09-22 19:50:55 +0200
committerBotond Hende <nettingman@gmail.com>2024-09-22 19:50:55 +0200
commit0f2a22208906cb2f7aeaf01bccaeb9c58fb83ae3 (patch)
tree0a340f7b6eb3b3d614ccb3f37fa848eb243d009b
parent57ad1a02f119e81d44141c7f4960574132f392c4 (diff)
spoiler renderer
-rw-r--r--spoiler.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/spoiler.py b/spoiler.py
new file mode 100644
index 0000000..ee15dfd
--- /dev/null
+++ b/spoiler.py
@@ -0,0 +1,35 @@
+"""
+Encapsulates text in a <div> html tag with the class "quote" and between two paragraphs which contain a quote (").
+These all can be styled in css.
+
+Usage (without the backslashes):
+\"""my quote\"""
+or
+\"""my quote "" source \"""
+"""
+from marko import inline
+from marko.helpers import MarkoExtension
+
+
+class SpoilerElement(inline.InlineElement):
+ pattern = '\\|\\|(.*)\\|\\|(.*)\\|\\|'
+ parse_children = True
+ parse_group = 2
+ priority = 7
+
+ def __init__(self, match):
+ self.id = match.group(1)
+ self.children = match.group(2)
+
+
+
+class SpoilerRenderer:
+ def render_spoiler_element(self, element: SpoilerElement):
+ content = self.render_children(element)
+ return f'<span><input type="checkbox" class="spoiler" id="{element.id}"><label class="spoiler-on" for="{element.id}"><span>{"▉" * len(content)}</span></label><label class="spoiler-off" for="{element.id}"><span>{content}</span></label></span>'
+
+
+Spoiler = MarkoExtension(
+ elements=[SpoilerElement],
+ renderer_mixins=[SpoilerRenderer]
+)