Skip to content
32 changes: 32 additions & 0 deletions python_docs_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from pathlib import Path

from markupsafe import Markup, escape
from sphinx.locale import get_translation

TYPE_CHECKING = False
Expand All @@ -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,
Expand All @@ -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'<html{lang_part} dir="{dir_attr}" data-content_root="{escape(content_root)}">'
)


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__,
Expand Down
2 changes: 1 addition & 1 deletion python_docs_theme/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<h3>{{ _('Navigation') }}</h3>
<ul>
{%- for rellink in rellinks %}
<li class="right" {% if loop.first %}style="margin-right: 10px"{% endif %}>
<li class="right" {% if loop.first %}style="margin-inline-end: 10px"{% endif %}>
<a href="{{ pathto(rellink[0])|e }}" title="{{ rellink[1]|striptags|e }}"
{{ accesskey(rellink[2]) }}>{{ rellink[3] }}
</a>
Expand Down
Loading
Loading