added additional configuration and metadata information

This commit is contained in:
Amy G. Bowersox 2024-08-11 22:41:04 -06:00
parent ad7f109328
commit fdcdb6a766
7 changed files with 38 additions and 19 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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