Compare commits

..

No commits in common. "17a9ed12730fb785764b3467e31e969448d3d57e" and "f26ea7c507e1ebcffb7d9a774f1852cd83061551" have entirely different histories.

4 changed files with 32 additions and 66 deletions

View File

@ -56,8 +56,6 @@ stylesheet = "dragonglass.css"
[metadata]
# If true, use page title as default description.
description-title = false
# The language of the site, expressed as a RFC5646 language identifier.
lang = "en"
# The site base URL. If supplied, this will be added to a <base> element in the page metadata.
sitebase = ""
# The site title. If supplied, this will be included in page metadata and used to formulate the default title.

View File

@ -1,23 +1,20 @@
TEMPLATE VARIABLES PROVIDED BY DRAGONGLASS WHEN RENDERING A DOCUMENT
backlinks:
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:
default_stylesheet
The filename of the default stylesheet which is generated by dragonglass and added to the
generated pages.
description:
description
The description of this page. May be empty.
dragonglass_version:
dragonglass_version
The version number of dragonglass.
lang:
The configured site language, as a RFC5646 language identifier.
python_version:
python_version
The version number of Python that's running dragonglass.
site_base:
@ -29,8 +26,8 @@ site_title:
tags:
A list of all tags the page being rendered has, in sorted order.
text:
text
The text of the page being rendered.
title:
title
The title of the page being rendered.

View File

@ -121,20 +121,14 @@ class Context:
@property
def site_base(self) -> str | None:
"""Returns the configured site base URL."""
metadata_section = self.config.get("metadata", {})
return metadata_section.get("sitebase", None)
@property
def site_language(self) -> str:
"""Returns the defayult language for the site."""
metadata_section = self.config.get("metadata", {})
return metadata_section.get("lang", "en")
generate_section = self.config.get("metadata", {})
return generate_section.get("sitebase", None)
@property
def site_title(self) -> str | None:
"""Returns the configured site title."""
metadata_section = self.config.get("metadata", {})
return metadata_section.get("sitetitle", None)
generate_section = self.config.get("metadata", {})
return generate_section.get("sitetitle", None)
@property
def url_prefix(self) -> str:

View File

@ -4,7 +4,7 @@
import sys
from jinja2 import Environment, BaseLoader, ChoiceLoader, FunctionLoader, FileSystemLoader
from typing import Any, Callable
from typing import Any
from urllib.parse import quote as urlquote
from .config import Context, DEFAULT_TEMPLATE_NAME
@ -13,7 +13,7 @@ from ._version import __version__
"""The default template used to render Markdown data."""
DEFAULT_TEMPLATE = """<html lang="{{ lang }}">
DEFAULT_TEMPLATE = """<html>
<head>
{% if site_title is defined %}
<title>{{ title }} - {{ site_title }}</title>
@ -21,7 +21,6 @@ DEFAULT_TEMPLATE = """<html lang="{{ lang }}">
{% else %}
<title>{{ title }}</title>
{% endif %}
<meta charset="utf-8"/>
{% if site_base is defined %}
<base href="{{ site_base }}"/>
{% endif %}
@ -74,28 +73,6 @@ def _create_loader(ctxt: Context) -> BaseLoader:
return ChoiceLoader(choices)
def makepath(ctxt: Context, node: SourceNode | None, s: str) -> str:
"""
Turn a string into a proper path reference, based on relative link settings and prefix.
Args:
ctxt (Context): The context to use for the operation.
node (SourceNode): The current node being processed; if this is ``None``, it's taken from the context.
s (str): The string to interpret.
Returns:
str: The resolved link.
"""
if not node:
node = ctxt.current_node
if ctxt.relative_links:
in_path = ctxt.source_dir / s
rel_path = ctxt.source_dir / node.path
return urlquote(in_path.relative_to(rel_path.parent, walk_up=True).as_posix())
else:
return urlquote(ctxt.url_prefix + s)
def create_template_environment(ctxt: Context) -> Environment:
"""
Create the template environment used to render the data to the destination.
@ -106,24 +83,7 @@ def create_template_environment(ctxt: Context) -> Environment:
Returns:
Environment: The new template environment.
"""
tenv = Environment(loader=_create_loader(ctxt))
# Add global variables easily calculated.
tenv.globals['dragonglass_version'] = __version__
tenv.globals["python_version"] = f"{sys.version_info[0]}.{sys.version_info[1]}.{sys.version_info[2]}"
# Add values derived from configuration.
tenv.globals["lang"] = ctxt.site_language
t = ctxt.site_base
if t:
tenv.globals["site_base"] = t
t = ctxt.site_title
if t:
tenv.globals["site_title"] = t
# Add global functions.
tenv.globals['makepath'] = lambda s: makepath(ctxt, None, s)
return tenv
return Environment(loader=_create_loader(ctxt))
def template_vars(node: SourceNode, ctxt: Context) -> dict[str, Any]:
@ -143,6 +103,18 @@ def template_vars(node: SourceNode, ctxt: Context) -> dict[str, Any]:
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]}"
# Add values derived from configuration.
t = ctxt.site_base
if t:
tvars["site_base"] = t
t = ctxt.site_title
if t:
tvars["site_title"] = t
# Compute backlinks for this node. Don't include nodes that aren't published.
backlinks = sorted([n for n in node.backlinks if n.publish], key=lambda n: n.page_title)
tvars['backlinks'] = [{'title': n.page_title,
@ -150,7 +122,12 @@ def template_vars(node: SourceNode, ctxt: Context) -> dict[str, Any]:
for n in backlinks]
# Add reference to the default stylesheet.
tvars['default_stylesheet'] = makepath(ctxt, node, ctxt.default_stylesheet)
if ctxt.relative_links:
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())
else:
tvars['default_stylesheet'] = urlquote(ctxt.url_prefix + ctxt.default_stylesheet)
return tvars