"""Jinja2 environment and shared view context.""" from __future__ import annotations from pathlib import Path from typing import Any from jinja2 import Environment, FileSystemLoader, select_autoescape from . import content TEMPLATE_DIR = Path(__file__).resolve().parents[1] / "templates" environment = Environment( loader=FileSystemLoader(str(TEMPLATE_DIR)), autoescape=select_autoescape(("html", "xml")), trim_blocks=True, lstrip_blocks=True, ) environment.globals.update( categories=content.CATEGORIES, sort_options=content.SORT_OPTIONS, status_options=content.STATUS_OPTIONS, difficulty_options=content.DIFFICULTY_OPTIONS, language_labels=content.LANGUAGE_LABELS, all_topics=content.ALL_TOPICS, all_languages=content.ALL_LANGUAGES, ) environment.filters["language_label"] = content.language_label environment.filters["difficulty_label"] = content.difficulty_label environment.filters["category_label"] = content.category_label environment.filters["sort_label"] = content.sort_label def render(name: str, **context: Any) -> str: return environment.get_template(name).render(**context)