summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBotond Hende <nettingman@gmail.com>2024-07-24 20:15:04 +0200
committerBotond Hende <nettingman@gmail.com>2024-07-24 20:15:04 +0200
commit0f075b9602875e6babaffab9ba8535f07f1df40e (patch)
tree5c1163cc73bb099ce6eb5a06bf28673688a985fd
parentab7e60b397e5c213e00e85cb942c559deb6cd76c (diff)
added classinjector module
-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]
+)