From fdcdb6a766070ef3d1bd0cc0c6ffd48f79ff2f8c Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Sun, 11 Aug 2024 22:41:04 -0600 Subject: [PATCH] added additional configuration and metadata information --- doc/configuration.toml | 2 ++ doc/metadata-values.txt | 4 ++++ src/dragonglass/config.py | 8 ++++++++ src/dragonglass/dragonglass.py | 25 +++++++++++++------------ src/dragonglass/style.py | 8 ++++---- src/dragonglass/template.py | 5 ++--- src/dragonglass/tree.py | 5 +++++ 7 files changed, 38 insertions(+), 19 deletions(-) diff --git a/doc/configuration.toml b/doc/configuration.toml index 2b797ea..c746bbe 100644 --- a/doc/configuration.toml +++ b/doc/configuration.toml @@ -9,3 +9,5 @@ relative = false directory = ".dragonglass.tmpl" # The default template name. Default is "default.html". default = "default.html" +# The name of the default stylesheet. +stylesheet = "dragonglass.css" diff --git a/doc/metadata-values.txt b/doc/metadata-values.txt index ae73024..cc64c48 100644 --- a/doc/metadata-values.txt +++ b/doc/metadata-values.txt @@ -5,6 +5,10 @@ aliases List of alternative names that can be used to link to a particular page. +publish + (Obsidian Publish standard metadata) + If this boolean value is False, the page will not be published. + template The file name of the template to be used to render this page, overriding the default. diff --git a/src/dragonglass/config.py b/src/dragonglass/config.py index 062bbe4..e6b6b73 100644 --- a/src/dragonglass/config.py +++ b/src/dragonglass/config.py @@ -17,6 +17,8 @@ DEFAULT_CONFIG_FILE = ".dragonglass" DEFAULT_TEMPLATE_DIRECTORY = ".dragonglass.tmpl" """The default template name.""" DEFAULT_TEMPLATE_NAME = "default.html" +""" The default stylesheet name.""" +DEFAULT_STYLESHEET_NAME = "dragonglass.css" class Context: @@ -71,6 +73,12 @@ class Context: links_section = self.config.get("links", {}) return links_section.get("relative", False) + @property + def default_stylesheet(self) -> str: + """Returns the default stylesheet name.""" + templates_section = self.config.get("templates", {}) + return templates_section.get("stylesheet", DEFAULT_STYLESHEET_NAME) + def get_template_name_for_node(self, node: SourceNode) -> str: """ Returns the name of a template to be used to render the current node. diff --git a/src/dragonglass/dragonglass.py b/src/dragonglass/dragonglass.py index d311d61..de938a6 100644 --- a/src/dragonglass/dragonglass.py +++ b/src/dragonglass/dragonglass.py @@ -87,19 +87,20 @@ def main() -> int: tenv = create_template_environment(context) for node in nodes: - p = node.target_file(dest_dir) - 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(template_vars(node, context)) - with p.open("wt") as f: - f.write(data) - else: - shutil.copyfile(context.source_dir / node.path, p) + if node.publish: + p = node.target_file(dest_dir) + 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(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) + write_default_stylesheet(context, tenv, dest_dir) except: logger.exception("Unexpected error in processing") diff --git a/src/dragonglass/style.py b/src/dragonglass/style.py index d7dac41..e4f52d2 100644 --- a/src/dragonglass/style.py +++ b/src/dragonglass/style.py @@ -5,8 +5,7 @@ from pathlib import Path from jinja2 import Environment -"""Name of the default stylesheet.""" -STYLESHEET_NAME = "dragonglass.css" +from .config import Context """Template data for the default stylesheet.""" STYLESHEET_DATA = """/* Dragonglass default CSS file - ensure all generated HTML pages reference this */ @@ -214,15 +213,16 @@ span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: """ -def write_default_stylesheet(tenv: Environment, dest_dir: Path) -> None: +def write_default_stylesheet(ctxt: Context, tenv: Environment, dest_dir: Path) -> None: """ Writes the default stylesheet data to the destination directory. Args: + ctxt (Context): The context for the operation, which includes the configuration. tenv (Environment): Template engine used to render the default stylesheet data. dest_dir (Path): The destination directory to write the stylesheet to. """ - to_file = dest_dir / STYLESHEET_NAME + to_file = dest_dir / ctxt.default_stylesheet tmpl = tenv.from_string(STYLESHEET_DATA) data = tmpl.render({}) with to_file.open("wt") as f: diff --git a/src/dragonglass/template.py b/src/dragonglass/template.py index 7bd15b0..c84ae97 100644 --- a/src/dragonglass/template.py +++ b/src/dragonglass/template.py @@ -8,7 +8,6 @@ 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 from ._version import __version__ @@ -98,12 +97,12 @@ def template_vars(node: SourceNode, ctxt: Context) -> dict[str, Any]: # Add reference to the default stylesheet. if ctxt.relative_links: - stylesheet_path = ctxt.source_dir / STYLESHEET_NAME + stylesheet_path = ctxt.source_dir / ctxt.default_stylesheet 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) + tvars['default_stylesheet'] = urlquote(ctxt.url_prefix + ctxt.default_stylesheet) pass return tvars diff --git a/src/dragonglass/tree.py b/src/dragonglass/tree.py index db37d75..50e2b7e 100644 --- a/src/dragonglass/tree.py +++ b/src/dragonglass/tree.py @@ -77,6 +77,11 @@ class SourceNode: """Returns the standard page title for this node.""" return self.metadata.get("title", self._path.stem) + @property + def publish(self) -> bool: + """Returns the "publish" flag for a page.""" + return self.metadata.get("publish", True) if self._is_md else True + def target_file(self, dest_dir: Path) -> Path: """ Computes the path of the target file as it will be written to the destination directory.