From 8e5c40a642c34b0882ada1d7a80b8e5e3f123e3e Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Thu, 8 Aug 2024 16:16:36 -0600 Subject: [PATCH] made the configuration file format TOML instead of YAML --- doc/configuration.toml | 11 +++++++++++ doc/configuration.yaml | 6 ------ src/dragonglass/config.py | 19 +++++++++++++------ src/dragonglass/tree.py | 4 +++- 4 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 doc/configuration.toml delete mode 100644 doc/configuration.yaml diff --git a/doc/configuration.toml b/doc/configuration.toml new file mode 100644 index 0000000..2b797ea --- /dev/null +++ b/doc/configuration.toml @@ -0,0 +1,11 @@ +[links] +# The prefix to apply to all internally-generated URLs. Default is "/". +prefix = "/" +# If true, generate relative URLs for all internal URLs. Default is false. +relative = false + +[templates] +# The template directory name under the Obsidian vault. Default is ".dragonglass.tmpl". +directory = ".dragonglass.tmpl" +# The default template name. Default is "default.html". +default = "default.html" diff --git a/doc/configuration.yaml b/doc/configuration.yaml deleted file mode 100644 index 6298e95..0000000 --- a/doc/configuration.yaml +++ /dev/null @@ -1,6 +0,0 @@ -# The prefix to apply to all internally-generated URLs. Default is "/". -url_prefix: / -# The template directory name under the Obsidian vault. Default is ".dragonglass.tmpl". -template_directory: .dragonglass.tmpl -# The default template name. Default is "default.html". -default_template: default.html diff --git a/src/dragonglass/config.py b/src/dragonglass/config.py index 49c46df..5055a0e 100644 --- a/src/dragonglass/config.py +++ b/src/dragonglass/config.py @@ -4,7 +4,7 @@ from argparse import Namespace from pathlib import Path from typing import Any -import yaml +import tomllib from .tree import SourceIndex, SourceNode @@ -27,16 +27,23 @@ class Context: assert self.source_dir is not None config_path = self.source_dir / config_filename if config_path.exists() and config_path.is_file(): - with open(config_path, "r") as f: - self.config = yaml.full_load(f) - self.template_dir = self.source_dir / self.config.get("template_directory", DEFAULT_TEMPLATE_DIRECTORY) - self._default_template_name = self.config.get("default_template", DEFAULT_TEMPLATE_NAME) + with open(config_path, "rb") as f: + self.config = tomllib.load(f) + templates_section = self.config.get("templates", {}) + self.template_dir = self.source_dir / templates_section.get("directory", DEFAULT_TEMPLATE_DIRECTORY) + self._default_template_name = templates_section.get("default", DEFAULT_TEMPLATE_NAME) @property def url_prefix(self) -> str: - rc = self.config.get("url_prefix", "/") + links_section = self.config.get("links", {}) + rc = links_section.get("prefix", "/") return rc if rc.endswith("/") else rc + '/' + @property + def relative_links(self) -> bool: + links_section = self.config.get("links", {}) + return links_section.get("relative", False) + def get_template_name_for_node(self, node: SourceNode) -> str: if not self.template_dir.is_dir(): return DEFAULT_TEMPLATE_NAME diff --git a/src/dragonglass/tree.py b/src/dragonglass/tree.py index 28b6087..b5b0508 100644 --- a/src/dragonglass/tree.py +++ b/src/dragonglass/tree.py @@ -46,8 +46,10 @@ class SourceNode: p = p.with_suffix('.html') return dest_dir / p - def link_target(self, prefix: str = "/") -> str: + def link_target(self, prefix: str = "/", rel_to: Any = None) -> str: xpath = self._path.with_suffix('.html') if self._is_md else self._path + if rel_to is not None: + return urlquote(xpath.relative_to(rel_to._path, walk_up=True).as_posix()) return urlquote(prefix + xpath.as_posix()) def load_metadata(self, source_dir: Path) -> None: