~sqwishy/froghat.ca

froghat.ca/pelext/rst.py -rw-r--r-- 3.8 KiB
af1e7ef2sqwishy fix video width being too big for tiny viewports 3 months ago
                                                                                
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from docutils import nodes
from docutils.parsers.rst import roles, directives, Directive
from docutils.parsers.rst.directives.body import CodeBlock
from docutils.parsers.rst.directives.images import Image
from docutils.writers.html5_polyglot import Writer, HTMLTranslator

from pelican.readers import (
    PelicanHTMLWriter,
    PelicanHTMLTranslator,
    RstReader,
    METADATA_PROCESSORS,
)


roles.DEFAULT_INTERPRETED_ROLE = "code"  # ¯\_(ツ)_/¯ intead of title/citation


def rst_role(fn):
    roles.register_local_role(fn.__name__.replace("_","-"), fn)


@rst_role
def newthought(name, rawtext, text, lineno, inliner, options={}, content=[]):
    return [nodes.inline(text=text, classes=["newthought"])], []

@rst_role
def hl_purple(name, rawtext, text, lineno, inliner, options={}, content=[]):
    return [nodes.inline(text=text, classes=["hl-purple"])], []

@rst_role
def hl_yellow(name, rawtext, text, lineno, inliner, options={}, content=[]):
    return [nodes.inline(text=text, classes=["hl-yellow"])], []


class EmptyCode(CodeBlock):
    has_content = False

    def assert_has_content(self):
        pass


class Picture(Image):
    has_content = True

    def run(self):
        class picture(nodes.General, nodes.Element):
            pass

        (image_node,) = Image.run(self)
        if isinstance(image_node, nodes.system_message):
            return [image_node]

        picture_node = picture("")
        if self.content:
            self.state.nested_parse(self.content, self.content_offset, picture_node)
        picture_node += image_node
        return [picture_node]


class Source(Directive):
    option_spec = {"media": directives.unchanged, "srcset": directives.uri}

    def run(self):
        class source(nodes.General, nodes.Element):
            pass

        return [source(**self.options)]


directives.register_directive("empty-code", EmptyCode)
directives.register_directive("picture", Picture)
directives.register_directive("source", Source)

PelicanHTMLTranslator.__bases__ = (HTMLTranslator,)


class MemeHTMLWriter(PelicanHTMLWriter):
    def __init__(self):
        super().__init__()
        self.translator_class = MemeHTMLTranslator


class MemeHTMLTranslator(PelicanHTMLTranslator):
    def visit_section(self, node):
        """Same as base but <section>."""
        self.section_level += 1
        self.body.append(self.starttag(node, "section"))

    def depart_section(self, node):
        self.section_level -= 1
        self.body.append("</section>")

    def visit_attribution(self, node):
        prefix, suffix = self.attribution_formats[self.settings.attribution]
        self.context.append(suffix)
        self.body.append(self.starttag(node, "footer", prefix, CLASS="attribution"))

    def depart_attribution(self, node):
        self.body.append(self.context.pop() + "</footer>\n")

    def visit_figure(self, node):
        """Same as base but <figure>."""
        atts = {"class": "figure"}
        if node.get("width"):
            atts["style"] = "width: %s" % node["width"]
        if node.get("align"):
            atts["class"] += " align-" + node["align"]
        self.body.append(self.starttag(node, "figure", **atts))

    def depart_figure(self, node):
        self.body.append("</figure>\n")

    def visit_picture(self, node):
        self.body.append(self.starttag(node, "picture"))

    def depart_picture(self, node):
        self.body.append("</picture>\n")

    def visit_source(self, node):
        attrs = {a: node.get(a) for a in Source.option_spec.keys()}
        self.body.append(self.starttag(node, "source", **attrs))

    def depart_source(self, node):
        pass

    def visit_caption(self, node):
        """Like base but <figcaption>."""
        self.body.append(self.starttag(node, "figcaption", ""))

    def depart_caption(self, node):
        self.body.append("</figcaption>\n")


RstReader.writer_class = MemeHTMLWriter