summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--classinjector.py24
1 files changed, 24 insertions, 0 deletions
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"<p class=\"{" ".join(element.classes)}\">{self.render_children(element)}</p>"
+
+
+ClassInjector = MarkoExtension(
+ renderer_mixins=[ClassInjectorRenderer]
+)