Draft simple rendering to HTML.

This commit is contained in:
Sergio Martínez Portela 2021-08-27 00:22:48 +02:00
parent d6ae48ed25
commit 22035852fd

View File

@ -1,15 +1,17 @@
#!/usr/bin/env python3
import html
import logging
import os
import sys
import uuid
from datetime import datetime
from org_rw import OrgTime
from org_rw import OrgTime, dom
from org_rw import dump as dump_org
from org_rw import dumps as dumps_org
from org_rw import load as load_org
from org_rw import token_list_to_raw
EXTENSIONS = [
".org",
@ -45,6 +47,7 @@ def load_all(top_dir_relative):
def main(src_top, dest_top):
docs = load_all(src_top)
files_generated = 0
os.makedirs(dest_top, exist_ok=True)
for doc in docs:
@ -74,24 +77,100 @@ def main(src_top, dest_top):
if not relpath.startswith("public/"):
# print("Skip:", relpath)
continue
pubpath = relpath.split("/", 1)[1]
endpath = pubpath + ".html"
for extension in EXTENSIONS:
if pubpath.endswith(extension):
endpath = pubpath[: -len(extension)] + ".html"
break
print("PUBLIC", pubpath, "", endpath)
endpath = os.path.join(dest_top, endpath)
dirname = os.path.dirname(endpath)
if len(dirname) > 0:
os.makedirs(dirname, exist_ok=True)
with open(endpath, "wt") as f:
f.write(render(doc))
for headline in headlines:
endpath = os.path.join(dest_top, headline.id + ".node.html")
with open(endpath, "wt") as f:
f.write(render(headline, doc))
files_generated += 1
logging.info("Generated {} files".format(files_generated))
def render(doc):
return dumps_org(doc)
def print_tree(tree, indentation=0):
for element in tree:
print(" " * indentation + "- " + str(type(element)))
if "children" in dir(element):
if len(element.children) > 0:
print_tree(element.children, indentation + 1)
print()
def render_property_drawer(element, acc):
pass
def render_logbook_drawer(element, acc):
pass
def render_property_node(element, acc):
pass
def render_list_group(element, acc):
acc.append("<ul>")
render_tree(element.children, acc)
acc.append("</ul>")
def render_list_item(element, acc):
acc.append("<li>")
if element.content.tag is not None:
acc.append("<span class='tag'>")
acc.append(element.content.tag)
acc.append("</span>")
acc.append("<span class='item'>")
acc.append(token_list_to_raw(element.content.content))
acc.append("</span></li>")
def render_code_block(element, acc):
pass
def render_text(element, acc):
pass
def render_tag(element, acc):
return {
dom.PropertyDrawerNode: render_property_drawer,
dom.LogbookDrawerNode: render_logbook_drawer,
dom.PropertyNode: render_property_node,
dom.ListGroupNode: render_list_group,
dom.ListItem: render_list_item,
dom.CodeBlock: render_code_block,
dom.Text: render_text,
}[type(element)](element, acc)
def render_tree(tree, acc):
for element in tree:
render_tag(element, acc)
def render(headline, doc):
print("\n===========")
dom = headline.as_dom()
print_tree(dom)
content = []
render_tree(dom, content)
return f"""
<article id="{html.escape(headline.id)}">
<h1 class="title">
<a href=\"org-protocol://org-id?id={html.escape(headline.id)}\">
{html.escape(headline.title)}
</a>
</h1>
{''.join(content)}
</article>
"""
def save_changes(doc):