Skip to content

Commit 64a25de

Browse files
committed
Remove add_html_dir_to_context from __init__ and move RTL detection to layout.html
1 parent 4a8dd39 commit 64a25de

2 files changed

Lines changed: 3 additions & 35 deletions

File tree

python_docs_theme/__init__.py

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from pathlib import Path
44

5-
from markupsafe import Markup, escape
65
from sphinx.locale import get_translation
76

87
TYPE_CHECKING = False
@@ -19,12 +18,6 @@
1918
MESSAGE_CATALOG_NAME = "python-docs-theme"
2019

2120

22-
def _tobool(val: object) -> bool:
23-
if isinstance(val, str):
24-
return val.lower() in {"true", "1", "yes", "on"}
25-
return bool(val)
26-
27-
2821
def add_translation_to_context(
2922
app: Sphinx,
3023
pagename: str,
@@ -44,40 +37,15 @@ def combined(message: str) -> str:
4437
context["_"] = context["gettext"] = context["ngettext"] = combined
4538

4639

47-
def add_html_dir_to_context(
48-
app: Sphinx,
49-
pagename: str,
50-
templatename: str,
51-
context: dict[str, Any],
52-
doctree: None,
53-
) -> None:
54-
language = app.config.language or "en"
55-
56-
is_rtl_option = context.get("theme_is_rtl", "")
57-
if is_rtl_option in (None, ""):
58-
is_rtl = False
59-
else:
60-
is_rtl = _tobool(is_rtl_option)
61-
62-
dir_attr = "rtl" if is_rtl else "ltr"
63-
64-
content_root = context.get("content_root", "")
65-
lang_part = f' lang="{escape(language)}"' if language is not None else ""
66-
context["html_tag"] = Markup(
67-
f'<html{lang_part} dir="{dir_attr}" data-content_root="{escape(content_root)}">'
68-
)
69-
70-
7140
def setup(app: Sphinx) -> ExtensionMetadata:
7241
app.require_sphinx("7.3")
7342

7443
app.add_html_theme("python_docs_theme", str(THEME_PATH))
7544
app.add_message_catalog(MESSAGE_CATALOG_NAME, LOCALE_DIR)
7645
app.connect("html-page-context", add_translation_to_context)
77-
app.connect("html-page-context", add_html_dir_to_context)
7846

7947
return {
8048
"version": __version__,
8149
"parallel_read_safe": True,
8250
"parallel_write_safe": True,
83-
}
51+
}

python_docs_theme/layout.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ <h3>{{ _('Navigation') }}</h3>
8989
{%- endblock -%}
9090

9191
{%- block body_tag %}
92-
{{ super() }}
92+
<body{% if theme_is_rtl|tobool %} dir="rtl"{% endif %}>
9393
{%- if builder != 'htmlhelp' %}
9494
<div class="mobile-nav">
9595
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
@@ -169,4 +169,4 @@ <h3>{{ _('Navigation') }}</h3>
169169

170170
{% trans sphinx_version=sphinx_version|e %}Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> {{ sphinx_version }}.{% endtrans %}
171171
</div>
172-
{% endblock %}
172+
{% endblock %}

0 commit comments

Comments
 (0)