From ad7f109328bca252c436dce1a4e57600b1846b5d Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Sun, 11 Aug 2024 22:29:18 -0600 Subject: [PATCH] codified and implemented some of the metadata values and template variables --- doc/metadata-values.txt | 12 ++++++++++++ doc/template-vars.txt | 21 +++++++++++++++++++++ src/dragonglass/_version.py | 1 + src/dragonglass/config.py | 2 +- src/dragonglass/template.py | 6 ++++++ src/dragonglass/tree.py | 2 +- 6 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 doc/metadata-values.txt create mode 100644 doc/template-vars.txt create mode 100644 src/dragonglass/_version.py diff --git a/doc/metadata-values.txt b/doc/metadata-values.txt new file mode 100644 index 0000000..ae73024 --- /dev/null +++ b/doc/metadata-values.txt @@ -0,0 +1,12 @@ +METADATA VALUES USED BY DRAGONGLASS + +aliases + (Obsidian standard metadata) + List of alternative names that can be used to link to a + particular page. + +template + The file name of the template to be used to render this page, overriding the default. + +title + Title to use for the page, overriding the Markdown file name. diff --git a/doc/template-vars.txt b/doc/template-vars.txt new file mode 100644 index 0000000..137b731 --- /dev/null +++ b/doc/template-vars.txt @@ -0,0 +1,21 @@ +TEMPLATE VARIABLES PROVIDED BY DRAGONGLASS WHEN RENDERING A DOCUMENT + +backlinks + A list of pages that link to the page being rendered. Formatted as a list of dicts with + two elements, "name" and "link", containing the page title and the link to it, respectively. + +default_stylesheet + The filename of the default stylesheet which is generated by dragonglass and added to the + generated pages. + +dragonglass_version + The version number of dragonglass. + +python_version + The version number of Python that's running dragonglass. + +text + The text of the page being rendered. + +title + The title of the page being rendered. diff --git a/src/dragonglass/_version.py b/src/dragonglass/_version.py new file mode 100644 index 0000000..3dc1f76 --- /dev/null +++ b/src/dragonglass/_version.py @@ -0,0 +1 @@ +__version__ = "0.1.0" diff --git a/src/dragonglass/config.py b/src/dragonglass/config.py index df5c18d..062bbe4 100644 --- a/src/dragonglass/config.py +++ b/src/dragonglass/config.py @@ -83,4 +83,4 @@ class Context: """ if not self.template_dir.is_dir(): return DEFAULT_TEMPLATE_NAME - return self._default_template_name + return node.metadata.get("template", self._default_template_name) diff --git a/src/dragonglass/template.py b/src/dragonglass/template.py index f7fd978..7bd15b0 100644 --- a/src/dragonglass/template.py +++ b/src/dragonglass/template.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 """The template engine used to render the data to the destination.""" +import sys from jinja2 import Environment, BaseLoader, ChoiceLoader, FunctionLoader, FileSystemLoader from typing import Any @@ -9,6 +10,7 @@ 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__ """The default template used to render Markdown data.""" @@ -16,6 +18,7 @@ DEFAULT_TEMPLATE = """ {{ title }} +

{{ title }}

@@ -84,6 +87,9 @@ def template_vars(node: SourceNode, ctxt: Context) -> dict[str, Any]: """ tvars = node.make_vars() + tvars["dragonglass_version"] = __version__ + tvars["python_version"] = f"{sys.version_info[0]}.{sys.version_info[1]}.{sys.version_info[2]}" + # Compute backlinks for this node. backlinks = sorted(node.backlinks, key=lambda n: n.page_title) tvars['backlinks'] = [{'title': n.page_title, diff --git a/src/dragonglass/tree.py b/src/dragonglass/tree.py index 8d0d745..db37d75 100644 --- a/src/dragonglass/tree.py +++ b/src/dragonglass/tree.py @@ -75,7 +75,7 @@ class SourceNode: @property def page_title(self) -> str: """Returns the standard page title for this node.""" - return self._path.stem + return self.metadata.get("title", self._path.stem) def target_file(self, dest_dir: Path) -> Path: """