summaryrefslogtreecommitdiff
path: root/headinginjector.py
blob: cb1b5ca726db0a681384027cf5edca145f7c7c17 (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
"""
Prefixes headers with the specified string.

Usage:
MyExtension = get_heading_injector(prefix)
"""
import marko.block
from marko.helpers import MarkoExtension


def __create_heading_renderer(prefix: str):
    class HeadingRenderer:
        PREFIX = prefix

        def render_heading(self, element: marko.block.Heading):
            return f"<h{element.level}>{HeadingRenderer.PREFIX}{self.render_children(element)}</h{element.level}>\n"

    return HeadingRenderer


def get_heading_injector(prefix: str):
    return MarkoExtension(
        renderer_mixins=[__create_heading_renderer(prefix)]
    )