summaryrefslogtreecommitdiff
path: root/classinjector.py
blob: 7489d82b5fbdba83791999fe52752a026cf55e88 (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
"""
Injects class into paragraph element.

usage: add a list named "classes" to the desired Paragraph element

content = my_parser.parse(my_md_text)
for elem in content:
    if isinstance(elem, marko.block.Paragraph):
        elem.classes = ["my_custom_class", "my_other_class"]
"""
from marko.helpers import MarkoExtension


class ClassInjectorRenderer:
    def render_paragraph(self, element) -> str:
        if not hasattr(element, "classes"):
            return super().render_paragraph(element)

        return f"<p class=\"{" ".join(element.classes)}\">{self.render_children(element)}</p>"


ClassInjector = MarkoExtension(
    renderer_mixins=[ClassInjectorRenderer]
)