diff options
| author | altaf-creator <dev@altafcreator.com> | 2026-05-17 13:01:57 +0800 |
|---|---|---|
| committer | altaf-creator <dev@altafcreator.com> | 2026-05-17 13:01:57 +0800 |
| commit | 9e7fe6d8dad56c237adb1905e130c1d723131812 (patch) | |
| tree | dd067e14b3ec5af719d748427bb322320c248879 /markdown_renderer.py | |
| parent | d04598ba59d7267ee4db3745ff90eaa1a990072a (diff) | |
big structural change, altafcreator.com no longer served statically, projects now use md, blog is being reworked, videos removed temp
Diffstat (limited to 'markdown_renderer.py')
| -rw-r--r-- | markdown_renderer.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/markdown_renderer.py b/markdown_renderer.py new file mode 100644 index 0000000..d64fcf2 --- /dev/null +++ b/markdown_renderer.py @@ -0,0 +1,46 @@ +import markdown +import frontmatter +import markdown.treeprocessors +import markdown.extensions + + +class DefaultClassTreeprocessor(markdown.treeprocessors.Treeprocessor): + def run(self, root): + for element in root.iter("a"): + print(element.attrib) + if "class" not in element.attrib or "button" not in element.attrib["class"]: + element.set("class", "link") + if "href" in element.attrib and element.attrib["href"] == "": + del element.attrib["href"] + for element in root.iter("img"): + element.set("class", "img") + + +class DefaultClassExtension(markdown.extensions.Extension): + def extendMarkdown(self, md): + md.treeprocessors.register(DefaultClassTreeprocessor(md), "link_class", 2) + + +def md_to_html(raw_content: str) -> str: + metadata, content = frontmatter.parse(raw_content) + html_text = markdown.markdown( + content, + output_format="html", + extensions=["attr_list", "fenced_code", "tables", DefaultClassExtension(), "md_in_html"] + ) + html_text = html_text.replace("<p>+-+-+-</p>", '<section><div class="center-grid"><div class="div-sizing">') + html_text = html_text.replace("<p>+-+-+-", '<section><div class="center-grid"><div class="div-sizing">') + html_text = html_text.replace("+-+-+-</p>", '<section><div class="center-grid"><div class="div-sizing">') + html_text = html_text.replace("-+-+-+</p>", "</div></div></section>") + html_text = html_text.replace("<p>-+-+-+", "</div></div></section>") + html_text = html_text.replace("<p>+=+=+=</p>", '<section class="normal-section"><div class="center-grid"><div class="div-sizing">') + html_text = html_text.replace("<p>+=+=+=", '<section class="normal-section"><div class="center-grid"><div class="div-sizing">') + html_text = html_text.replace("+=+=+=</p>", '<section class="normal-section"><div class="center-grid"><div class="div-sizing">') + html_text = html_text.replace("<p>{{{</p>", '<div class="flex-container gap">') + html_text = html_text.replace("<p>{{{", '<div class="flex-container gap">') + html_text = html_text.replace("{{{</p>", '<div class="flex-container gap">') + html_text = html_text.replace("<p>}}}</p>", '</div>') + html_text = html_text.replace("}}}</p>", '</div>') + html_text = html_text.replace("<p>}}}", '</div>') + + return (metadata, html_text) |
