diff --git a/formatitko.py b/formatitko.py index 373791c..6d14a2d 100755 --- a/formatitko.py +++ b/formatitko.py @@ -9,16 +9,19 @@ from typing import List from transform import transform from util import * from context import Context +from html import html from mj_show import show doc = import_md(open(sys.argv[1], "r").read()) + print(show(doc)) context = Context(doc) doc = doc.walk(transform, context) print("---------------------") -print(show(doc)) -print(convert_text(doc, input_format="panflute", output_format="markdown")) +#print(show(doc)) +#print(convert_text(doc, input_format="panflute", output_format="markdown")) +print(html(doc)) diff --git a/html.py b/html.py new file mode 100644 index 0000000..0c1a6a1 --- /dev/null +++ b/html.py @@ -0,0 +1,106 @@ +from panflute import * +from whitespace import NBSP + + +def html(e: Element, indent_level: int=0, indent_str: str="\t") -> str: + + tag = e.tag.lower() + attributes = "" + content_foot = "" + content_head = "" + + if hasattr(e, "identifier") and e.identifier != "": + attributes += f' id="{e.identifier}"' + + if hasattr(e, "classes") and len(e.classes) != 0: + attributes += f' class="{" ".join(e.classes)}"' + + if isinstance(e, BulletList): + tag = "ul" + + if isinstance(e, Citation): + return "TODO: " + e.tag + + if isinstance(e, Cite): + return "TODO: " + e.tag + + if isinstance(e, CodeBlock): + # TODO: Syntax highlighting + tag = "pre" + + if isinstance(e, Definition): + return "TODO: " + e.tag + + if isinstance(e, DefinitionItem): + return "TODO: " + e.tag + + if isinstance(e, DefinitionList): + return "TODO: " + e.tag + + if isinstance(e, Emph): + tag = "em" + + if isinstance(e, Figure): + content_foot = html(e.caption, indent_level+1, indent_str) + + if isinstance(e, Caption): + tag = "figcaption" + + if isinstance(e, Image): + tag = "img" + # TODO: Finish this + # TODO: Image processing + + if isinstance(e, LineBreak): + return f"\n{indent_level*indent_str}
\n{indent_level*indent_str}" + + if isinstance(e, Para): + tag = "p" + + if isinstance(e, Header): + tag = "h"+str(e.level) + + if isinstance(e, Link): + tag = "a" + attributes += f' href="{e.url}"' + if len(e.title) != 0: + attributes += f' title="{e.title}"' + + if isinstance(e, Str): + return e.text + + if isinstance(e, NBSP): + return " " + + if isinstance(e, Space): + return " " + + if isinstance(e, Null): + return "" + + if isinstance(e, Math): + return "TODO: MATH" + + if isinstance(e, RawInline): + return e.text + + if isinstance(e, Inline): + return f"<{tag}{attributes}> {content_head} {''.join([html(child, 0, '') for child in e.content])} {content_foot} " + + out_str = "" + if not isinstance(e, Plain): + out_str += f"{indent_level*indent_str}<{tag}{attributes}>\n" + out_str += content_head + if hasattr(e, "_content"): + if len(e.content) > 0 and isinstance(e.content[0], Inline): + out_str += (indent_level+1)*indent_str + for child in e.content: + out_str += html(child, indent_level+1, indent_str) + if hasattr(e, "text"): + out_str += e.text + out_str += f"{content_foot}\n" + if not isinstance(e, Plain): + out_str += f"{indent_level*indent_str}\n" + + return out_str + diff --git a/test.md b/test.md index 4d14b83..651764a 100644 --- a/test.md +++ b/test.md @@ -27,6 +27,11 @@ ctx.set_metadata("a.b", {}) ctx.set_metadata("a.b.c", "Bruh **bruh** bruh") ``` +```python +def bruh(no): + wat +``` + ::::{if=cat} This should only be shown to cats the second time :::: @@ -39,6 +44,8 @@ A text with some $math$. This should be seen by all. +![This is a figure, go figure...](/this/image/does/not/exist.jpg)\ + [!opendatatask]{} [This too!]{if=cat}