-
-
Notifications
You must be signed in to change notification settings - Fork 77
Add RTL support to python-docs-theme #324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
091917c
e2cc426
5870154
6eb161f
13de683
1ce690e
09fce2b
a9ec66e
779f8b9
e9a4b29
a4d0f82
4a8dd39
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems quite overcomplicated for something used once. |
||
| 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" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not |
||
|
|
||
| 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( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a fan of this, what if Sphinx adds an attribute? |
||
| 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__, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now an undeclared direct dependency...