codified and implemented some of the metadata values and template variables

This commit is contained in:
Amy G. Bowersox 2024-08-11 22:29:18 -06:00
parent 375767a93c
commit ad7f109328
6 changed files with 42 additions and 2 deletions

12
doc/metadata-values.txt Normal file
View File

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

21
doc/template-vars.txt Normal file
View File

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

View File

@ -0,0 +1 @@
__version__ = "0.1.0"

View File

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

View File

@ -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 = """<html>
<head>
<title>{{ title }}</title>
<link rel="stylesheet" href="{{ default_stylesheet }}"/>
<meta name="generator" content="dragonglass/{{ dragonglass_version }} python/{{ python_version }}"/>
</head>
<body>
<h1>{{ title }}</h1>
@ -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,

View File

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