From 72a2ea1ab57a6c8ec45cb091005db1a884c1d2bf Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Sun, 11 Aug 2024 23:07:15 -0600 Subject: [PATCH] include support for description metadata --- doc/configuration.toml | 6 ++++++ doc/metadata-values.txt | 4 ++++ doc/template-vars.txt | 3 +++ src/dragonglass/config.py | 24 +++++++++++++++--------- src/dragonglass/template.py | 9 +++++++++ src/dragonglass/tree.py | 3 ++- 6 files changed, 39 insertions(+), 10 deletions(-) diff --git a/doc/configuration.toml b/doc/configuration.toml index c746bbe..46ba3f0 100644 --- a/doc/configuration.toml +++ b/doc/configuration.toml @@ -1,3 +1,5 @@ +# In this file, the value of each configuration option is its default. + [links] # The prefix to apply to all internally-generated URLs. Default is "/". prefix = "/" @@ -11,3 +13,7 @@ directory = ".dragonglass.tmpl" default = "default.html" # The name of the default stylesheet. stylesheet = "dragonglass.css" + +[metadata] +# If true, use page title as default description. +description-title = false diff --git a/doc/metadata-values.txt b/doc/metadata-values.txt index cc64c48..25ed5f7 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. +description + (Obsidian Publish standard metadata) + The description for this page. + publish (Obsidian Publish standard metadata) If this boolean value is False, the page will not be published. diff --git a/doc/template-vars.txt b/doc/template-vars.txt index 137b731..f39355b 100644 --- a/doc/template-vars.txt +++ b/doc/template-vars.txt @@ -8,6 +8,9 @@ default_stylesheet The filename of the default stylesheet which is generated by dragonglass and added to the generated pages. +description + The description of this page. May be empty. + dragonglass_version The version number of dragonglass. diff --git a/src/dragonglass/config.py b/src/dragonglass/config.py index e6b6b73..c3c7d65 100644 --- a/src/dragonglass/config.py +++ b/src/dragonglass/config.py @@ -59,11 +59,16 @@ class Context: self._default_template_name = templates_section.get("default", DEFAULT_TEMPLATE_NAME) @property - def url_prefix(self) -> str: - """Returns the configured URL prefix for all URLs generated for internal links.""" - links_section = self.config.get("links", {}) - rc = links_section.get("prefix", "/") - return rc if rc.endswith("/") else rc + '/' + def default_stylesheet(self) -> str: + """Returns the default stylesheet name.""" + templates_section = self.config.get("templates", {}) + return templates_section.get("stylesheet", DEFAULT_STYLESHEET_NAME) + + @property + def description_title(self) -> bool: + """Returns ``True`` if we use the title as the default description, ``False`` if not.""" + metadata_section = self.config.get("metadata", {}) + return metadata_section.get("description-title", False) @property def relative_links(self) -> bool: @@ -74,10 +79,11 @@ class Context: 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 url_prefix(self) -> str: + """Returns the configured URL prefix for all URLs generated for internal links.""" + links_section = self.config.get("links", {}) + rc = links_section.get("prefix", "/") + return rc if rc.endswith("/") else rc + '/' def get_template_name_for_node(self, node: SourceNode) -> str: """ diff --git a/src/dragonglass/template.py b/src/dragonglass/template.py index e822908..1549e58 100644 --- a/src/dragonglass/template.py +++ b/src/dragonglass/template.py @@ -17,6 +17,10 @@ DEFAULT_TEMPLATE = """ {{ title }} + {% if description|length > 0 %} + + + {% endif %} @@ -86,6 +90,11 @@ def template_vars(node: SourceNode, ctxt: Context) -> dict[str, Any]: """ tvars = node.make_vars() + # Patch description. + if not tvars["description"] and ctxt.description_title: + tvars["description"] = node.page_title + + # Add easily-calculated "global" variables. tvars["dragonglass_version"] = __version__ tvars["python_version"] = f"{sys.version_info[0]}.{sys.version_info[1]}.{sys.version_info[2]}" diff --git a/src/dragonglass/tree.py b/src/dragonglass/tree.py index 50e2b7e..4ae39e4 100644 --- a/src/dragonglass/tree.py +++ b/src/dragonglass/tree.py @@ -155,7 +155,8 @@ class SourceNode: """ return { "text": self.text, - "title": self.page_title + "title": self.page_title, + "description": self.metadata.get("description", "") }