diff --git a/python_docs_theme/__init__.py b/python_docs_theme/__init__.py index f0ddc06..ca5a424 100644 --- a/python_docs_theme/__init__.py +++ b/python_docs_theme/__init__.py @@ -2,6 +2,7 @@ from pathlib import Path +from markupsafe import Markup, escape from sphinx.locale import get_translation TYPE_CHECKING = False @@ -18,6 +19,12 @@ MESSAGE_CATALOG_NAME = "python-docs-theme" +def _tobool(val: object) -> bool: + if isinstance(val, str): + return val.lower() in {"true", "1", "yes", "on"} + return bool(val) + + def add_translation_to_context( app: Sphinx, pagename: str, @@ -37,12 +44,37 @@ def combined(message: str) -> str: context["_"] = context["gettext"] = context["ngettext"] = combined +def add_html_dir_to_context( + app: Sphinx, + pagename: str, + templatename: str, + context: dict[str, Any], + doctree: None, +) -> None: + language = app.config.language or "en" + + is_rtl_option = context.get("theme_is_rtl", "") + if is_rtl_option in (None, ""): + is_rtl = False + else: + is_rtl = _tobool(is_rtl_option) + + dir_attr = "rtl" if is_rtl else "ltr" + + content_root = context.get("content_root", "") + lang_part = f' lang="{escape(language)}"' if language is not None else "" + context["html_tag"] = Markup( + f'' + ) + + def setup(app: Sphinx) -> ExtensionMetadata: app.require_sphinx("7.3") app.add_html_theme("python_docs_theme", str(THEME_PATH)) app.add_message_catalog(MESSAGE_CATALOG_NAME, LOCALE_DIR) app.connect("html-page-context", add_translation_to_context) + app.connect("html-page-context", add_html_dir_to_context) return { "version": __version__, diff --git a/python_docs_theme/layout.html b/python_docs_theme/layout.html index 1903fad..cbdee99 100644 --- a/python_docs_theme/layout.html +++ b/python_docs_theme/layout.html @@ -6,7 +6,7 @@

{{ _('Navigation') }}