From 9e7fe6d8dad56c237adb1905e130c1d723131812 Mon Sep 17 00:00:00 2001 From: altaf-creator Date: Sun, 17 May 2026 13:01:57 +0800 Subject: big structural change, altafcreator.com no longer served statically, projects now use md, blog is being reworked, videos removed temp --- markdown_renderer.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 markdown_renderer.py (limited to 'markdown_renderer.py') 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("

+-+-+-

", '
') + html_text = html_text.replace("

+-+-+-", '

') + html_text = html_text.replace("+-+-+-

", '
') + html_text = html_text.replace("-+-+-+

", "
") + html_text = html_text.replace("

-+-+-+", "

") + html_text = html_text.replace("

+=+=+=

", '
') + html_text = html_text.replace("

+=+=+=", '

') + html_text = html_text.replace("+=+=+=

", '
') + html_text = html_text.replace("

{{{

", '
') + html_text = html_text.replace("

{{{", '

') + html_text = html_text.replace("{{{

", '
') + html_text = html_text.replace("

}}}

", '
') + html_text = html_text.replace("}}}

", '
') + html_text = html_text.replace("

}}}", '

') + + return (metadata, html_text) -- cgit v1.2.3