Home Download Direct linkSettings
Aa
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
Copy
import copy

from flaskext.markdown import Markdown

from markdown.extensions import Extension
from markdown.treeprocessors import Treeprocessor

from src import code_highlight as code_hlt


class TableWrapTreeprocessor(Treeprocessor):
    """
    Wraps all `table` tags in a `div` tag with the given css classes.
    """
    classes: list[str]

    def run(self, root):
        """
        Turns all `table` tags into `div` tags and appends copy of the original table to it.
        """
        # root.iter is converted to a tuple to prevent an infinite loop
        # every iteration append new table tag
        for block in tuple(root.iter("table")):
            table = copy.copy(block)
            block.clear()
            block.tag = "div"
            block.set("class", ''.join(self.classes))
            block.append(table)


class TableWrapExtension(Extension):

    def __init__(self, classes: list[str]):
        self.classes = classes

    def extendMarkdown(self, md):
        tp = TableWrapTreeprocessor(md)
        tp.classes = self.classes
        md.treeprocessors.register(tp, 'tablewrap', 30)
        md.registerExtension(self)


def init(app):
    Markdown(
        app,
        extensions=[
            'pymdownx.superfences',
            code_hlt.TemplateHighlightExtension(),
            'tables',
            TableWrapExtension(["table-box"]),
        ],
        extension_configs={},
    )

    app.jinja_env.globals.update(highlighter=code_hlt.TemplateHighlight)