From 0f075b9602875e6babaffab9ba8535f07f1df40e Mon Sep 17 00:00:00 2001 From: Botond Hende Date: Wed, 24 Jul 2024 20:15:04 +0200 Subject: added classinjector module --- classinjector.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 classinjector.py (limited to 'classinjector.py') diff --git a/classinjector.py b/classinjector.py new file mode 100644 index 0000000..7489d82 --- /dev/null +++ b/classinjector.py @@ -0,0 +1,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"

{self.render_children(element)}

" + + +ClassInjector = MarkoExtension( + renderer_mixins=[ClassInjectorRenderer] +) -- cgit v1.2.3-70-g09d2