Started working on HTML generation.
This commit is contained in:
parent
46dff9c42c
commit
f49085b309
3 changed files with 118 additions and 2 deletions
|
@ -9,16 +9,19 @@ from typing import List
|
||||||
from transform import transform
|
from transform import transform
|
||||||
from util import *
|
from util import *
|
||||||
from context import Context
|
from context import Context
|
||||||
|
from html import html
|
||||||
|
|
||||||
from mj_show import show
|
from mj_show import show
|
||||||
|
|
||||||
doc = import_md(open(sys.argv[1], "r").read())
|
doc = import_md(open(sys.argv[1], "r").read())
|
||||||
|
|
||||||
|
|
||||||
print(show(doc))
|
print(show(doc))
|
||||||
context = Context(doc)
|
context = Context(doc)
|
||||||
doc = doc.walk(transform, context)
|
doc = doc.walk(transform, context)
|
||||||
print("---------------------")
|
print("---------------------")
|
||||||
print(show(doc))
|
#print(show(doc))
|
||||||
print(convert_text(doc, input_format="panflute", output_format="markdown"))
|
#print(convert_text(doc, input_format="panflute", output_format="markdown"))
|
||||||
|
print(html(doc))
|
||||||
|
|
||||||
|
|
||||||
|
|
106
html.py
Normal file
106
html.py
Normal file
|
@ -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}<br>\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} </{tag}>"
|
||||||
|
|
||||||
|
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}</{tag}>\n"
|
||||||
|
|
||||||
|
return out_str
|
||||||
|
|
7
test.md
7
test.md
|
@ -27,6 +27,11 @@ ctx.set_metadata("a.b", {})
|
||||||
ctx.set_metadata("a.b.c", "Bruh **bruh** bruh")
|
ctx.set_metadata("a.b.c", "Bruh **bruh** bruh")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
def bruh(no):
|
||||||
|
wat
|
||||||
|
```
|
||||||
|
|
||||||
::::{if=cat}
|
::::{if=cat}
|
||||||
This should only be shown to cats the second time
|
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 should be seen by all.
|
||||||
|
|
||||||
|
![This is a figure, go figure...](/this/image/does/not/exist.jpg)\
|
||||||
|
|
||||||
[!opendatatask]{}
|
[!opendatatask]{}
|
||||||
|
|
||||||
[This too!]{if=cat}
|
[This too!]{if=cat}
|
||||||
|
|
Loading…
Reference in a new issue