added default stylesheet generation

This commit is contained in:
Amy G. Bowersox 2024-08-09 16:22:55 -06:00
parent d278260b90
commit b8b2d5ebee
3 changed files with 53 additions and 2 deletions

View File

@ -7,7 +7,8 @@ from pathlib import Path
from .config import Context
from .mparse import create_markdown_parser
from .template import create_template_environment
from .style import write_default_stylesheet
from .template import create_template_environment, template_vars
from .tree import SourceIndex, generate_list
logger = logging.getLogger(__name__)
@ -76,11 +77,13 @@ def main() -> int:
if node.is_dir:
p.mkdir(exist_ok=True)
elif node.is_md:
context.current_node = node
tmpl = tenv.get_template(context.get_template_name_for_node(node))
data = tmpl.render(node.make_vars())
data = tmpl.render(template_vars(node, context))
with p.open("wt") as f:
f.write(data)
else:
shutil.copyfile(context.source_dir / node.path, p)
write_default_stylesheet(tenv, dest_dir)
return 0

28
src/dragonglass/style.py Normal file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env python3
from pathlib import Path
from jinja2 import Environment
STYLESHEET_NAME = "dragonglass.css"
STYLESHEET_DATA = """/* Dragonglass default CSS file - ensure all generated HTML pages reference this */
a {
color: #8a5cf5;
}
.invalid-reference {
color: #ad8df8;
text-decoration: underline;
text-decoration-color: #e6ddfd;
}
ins {
background-color: #ffec99;
text-decoration: none;
}
"""
def write_default_stylesheet(tenv: Environment, dest_dir: Path) -> None:
to_file = dest_dir / STYLESHEET_NAME
tmpl = tenv.from_string(STYLESHEET_DATA)
data = tmpl.render({})
with to_file.open("wt") as f:
f.write(data)

View File

@ -1,13 +1,18 @@
#!/usr/bin/env python3
from jinja2 import Environment, BaseLoader, ChoiceLoader, FunctionLoader, FileSystemLoader
from typing import Any
from urllib.parse import quote as urlquote
from .config import Context, DEFAULT_TEMPLATE_NAME
from .style import STYLESHEET_NAME
from .tree import SourceNode
DEFAULT_TEMPLATE = """
<html>
<head>
<title>{{ title }}</title>
<link rel="stylesheet" href="{{ default_stylesheet }}"/>
</head>
<body>
<h1>{{ title }}</h1>
@ -26,3 +31,18 @@ def _create_loader(ctxt: Context) -> BaseLoader:
def create_template_environment(ctxt: Context) -> Environment:
return Environment(loader=_create_loader(ctxt))
def template_vars(node: SourceNode, ctxt: Context) -> dict[str, Any]:
tvars = node.make_vars()
if ctxt.relative_links:
stylesheet_path = ctxt.source_dir / STYLESHEET_NAME
rel_path = ctxt.source_dir / node.path
tvars['default_stylesheet'] = urlquote(stylesheet_path.relative_to(rel_path.parent, walk_up=True).as_posix())
pass
else:
tvars['default_stylesheet'] = urlquote(ctxt.url_prefix + STYLESHEET_NAME)
pass
return tvars