made the configuration file format TOML instead of YAML

This commit is contained in:
Amy G. Bowersox 2024-08-08 16:16:36 -06:00
parent 821ae2d8a5
commit 8e5c40a642
4 changed files with 27 additions and 13 deletions

11
doc/configuration.toml Normal file
View File

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

View File

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

View File

@ -4,7 +4,7 @@ from argparse import Namespace
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
import yaml import tomllib
from .tree import SourceIndex, SourceNode from .tree import SourceIndex, SourceNode
@ -27,16 +27,23 @@ class Context:
assert self.source_dir is not None assert self.source_dir is not None
config_path = self.source_dir / config_filename config_path = self.source_dir / config_filename
if config_path.exists() and config_path.is_file(): if config_path.exists() and config_path.is_file():
with open(config_path, "r") as f: with open(config_path, "rb") as f:
self.config = yaml.full_load(f) self.config = tomllib.load(f)
self.template_dir = self.source_dir / self.config.get("template_directory", DEFAULT_TEMPLATE_DIRECTORY) templates_section = self.config.get("templates", {})
self._default_template_name = self.config.get("default_template", DEFAULT_TEMPLATE_NAME) 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 @property
def url_prefix(self) -> str: 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 + '/' 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: def get_template_name_for_node(self, node: SourceNode) -> str:
if not self.template_dir.is_dir(): if not self.template_dir.is_dir():
return DEFAULT_TEMPLATE_NAME return DEFAULT_TEMPLATE_NAME

View File

@ -46,8 +46,10 @@ class SourceNode:
p = p.with_suffix('.html') p = p.with_suffix('.html')
return dest_dir / p 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 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()) return urlquote(prefix + xpath.as_posix())
def load_metadata(self, source_dir: Path) -> None: def load_metadata(self, source_dir: Path) -> None: