summaryrefslogtreecommitdiff
path: root/spoiler.py
blob: ee15dfd564d531edd1c24bdee604a3405ca62653 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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]
)