From b8b2d5ebeee837c22fdb39da736470978051052d Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Fri, 9 Aug 2024 16:22:55 -0600 Subject: [PATCH] added default stylesheet generation --- src/dragonglass/dragonglass.py | 7 +++++-- src/dragonglass/style.py | 28 ++++++++++++++++++++++++++++ src/dragonglass/template.py | 20 ++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/dragonglass/style.py diff --git a/src/dragonglass/dragonglass.py b/src/dragonglass/dragonglass.py index c1a6b3a..12717cd 100644 --- a/src/dragonglass/dragonglass.py +++ b/src/dragonglass/dragonglass.py @@ -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 diff --git a/src/dragonglass/style.py b/src/dragonglass/style.py new file mode 100644 index 0000000..8e11233 --- /dev/null +++ b/src/dragonglass/style.py @@ -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) diff --git a/src/dragonglass/template.py b/src/dragonglass/template.py index 83a65b3..a0c49d5 100644 --- a/src/dragonglass/template.py +++ b/src/dragonglass/template.py @@ -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 = """ {{ title }} +

{{ title }}

@@ -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