You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

106 lines
2.3 KiB

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 "&nbsp;"
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