diff options
author | My Dear Diary <mydeardiary@tilde.guru> | 2024-01-24 02:40:36 +0000 |
---|---|---|
committer | My Dear Diary <mydeardiary@tilde.guru> | 2024-01-24 02:40:36 +0000 |
commit | 85a242f098af335c66365029639b371a4837e190 (patch) | |
tree | 82076399f920b0f66475cd59df827f562fd90143 | |
download | python-webspace-85a242f098af335c66365029639b371a4837e190.tar.gz |
initial commit
Initial working prototype of simple markdown to html generator with support for meta headings and title headings.
-rw-r--r-- | main.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/main.py b/main.py new file mode 100644 index 0000000..056c9c5 --- /dev/null +++ b/main.py @@ -0,0 +1,43 @@ +import sys +import markdown +from string import Template + +txt = str(sys.stdin.read()) +md = markdown.Markdown(extensions=["meta", "fenced_code"]) + +html_string = md.convert(txt) +document_template = Template("""<!DOCTYPE html> +<html lang="en"> +<head> +<meta charset="UTF-8"/> +$title_heading +$meta_heading +<meta name="viewport" content="width=device-width,initial-scale=1"/> +</head> +<body> +$body_text +</body> +</html>""") + +title_heading = """<title>{}</title>""" +meta_template = """<meta name="{}" content="{}"/>""" + +def getTitle(): + for key, value in md.Meta.items(): + if key == "title": + return str(*value) + +my_title = getTitle() +my_title_headings = title_heading.format(my_title) +my_meta_heading_content = [] +def getMetaHeadings(): + for key, value in md.Meta.items(): + if key != "title": + my_meta_heading_content.append( + meta_template.format(key, str(*value)) + ) + +getMetaHeadings() + +print(document_template.substitute(title_heading=my_title_headings, meta_heading="\n".join(my_meta_heading_content), body_text=html_string)) + |