feat: let a site choose which suffix the plugin treats as Mews

This commit is contained in:
randogoth 2026-10-11 21:07:07 +03:00
parent 0d9cf1ed26
commit 5e2e709496
3 changed files with 53 additions and 2 deletions

View file

@ -47,7 +47,13 @@ LOGGER = logging.getLogger(__name__)
CSS_NAME = "mews-0.1.css"
DEFAULT_CSS_DIR = "css"
XHTML_SUFFIX = ".xhtml"
# Which files Pelican writes are Mews pages. The default keeps the plugin off
# everything else, so it can run on a site that also produces ordinary HTML.
# A site that is Mews throughout wants ".html" instead: servers send .xhtml as
# application/xhtml+xml, which makes a browser parse as XML and fail hard on a
# small markup error, and section 7.1 asks for text/html precisely to avoid
# that. Set MEWS_SUFFIX to choose.
DEFAULT_SUFFIX = ".xhtml"
ALLOWLIST = parse_allowlist(
files(__package__).joinpath("dtd", "mews.dtd").read_text(encoding="utf-8")
@ -67,6 +73,12 @@ def _on_initialized(pelican) -> None:
overrides.append(templates)
def _suffix() -> str:
"""Return the file extension a Mews page is written under, with its dot."""
suffix = str(_settings.get("MEWS_SUFFIX", DEFAULT_SUFFIX)).strip()
return suffix if suffix.startswith(".") else f".{suffix}"
def _on_generator_init(generator) -> None:
css_dir = generator.settings.get("MEWS_CSS_DIR", DEFAULT_CSS_DIR)
generator.env.globals["MEWS_CSS"] = f"{css_dir}/{CSS_NAME}"
@ -74,7 +86,7 @@ def _on_generator_init(generator) -> None:
def _on_content_written(path, **_) -> None:
file = Path(path)
if file.suffix != XHTML_SUFFIX:
if file.suffix != _suffix():
return
try:
tree, doctype = parse_page(file.read_bytes())