iwmlib/site/markup.py

108 lines
3.1 KiB
Python
Raw Permalink Normal View History

from typing import Dict, List
import ssg.document
import ssg.utils
from ssg.tags import *
from dominate.tags import *
def iwmlibdoc_document(context):
doc = ssg.document.document(title=context.title)
with doc.head:
meta(charset="utf-8")
attr(lang=context.language())
if isinstance(context, static_page):
for tag in context.soup.find_all('link'):
if href := tag.attrs.get('href'):
link(rel="stylesheet", href=href)
for tag in context.soup.find_all('script'):
if src := tag.attrs.get('src'):
script(src=src)
else:
link(rel="stylesheet", href= "css/doctest.css")
link(rel="stylesheet", href= "css/demo.css")
with doc.body:
site_navigation(context)
ssg.document.placeholder(key='main', alternatives=['body']) # here goes the special content part
site_footer(context)
return doc
def site_navigation(context):
mainmenu(context)
br()
br()
breadcrumb(context)
hr()
def level_menu(context, root):
for name, child in sorted(context.named_children.items()):
if child.folderish():
if index := child.named_children.get('index.html'):
url = '.' + index.url(relative=root)
yield dict(url=url, title=index.title)
if child.title == name:
continue
if name.endswith('.html'):
url = '.' + child.url(relative=root)
yield dict(url=url, title=child.title)
def main_menu(context):
root = context.menuroot()
if context == root:
for info in level_menu(root, root):
yield info
else:
yield dict(url= context.up() + 'sitemap.html', title="Home")
for info in level_menu(context.parent, root):
yield info
@nav(cls="breadcrumb")
def breadcrumb(context):
"""
{% macro breadcrumb(context) -%}
<nav class="breadcrumb">
{% for info in context.breadcrumb() %}
{% if info.url %}
<a href="{{info.url}}">{{info.title}}</a>
{% else %}
<a href="#"><u>{{info.title}}</u></a>
{% endif %}
{% endfor %}
</nav>
{% endmacro %}
"""
for info in context.breadcrumb():
if url := info.get('url'):
a(info['title'], href=url)
else:
a(info['title'], href='#')
@nav(cls="mainmenu")
def mainmenu(context:ssg.generator):
for info in main_menu(context):
if url := info.get('url'):
a(info['title'], href=url)
else:
a(info['title'], href='#')
@main
def sitemap_main(context:ssg.generator):
root = context.menuroot()
h3("Sitemap")
with ul(id="sitemap"):
for info in getsite().page_infos(relative=root):
url = info['url']
if url.endswith('.html'):
li(a(info['title'], href=url))
@footer
def site_footer(context):
hr()
date = ssg.utils.now('en')
p(f'Generated by IWMSite {date}')