Skip to content

Make it possible to define language_map in several extensions. #65

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions sphinx_togglebutton/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@
import os
from docutils.parsers.rst import Directive, directives
from docutils import nodes
from sphinx.config import Config
from sphinx.application import Sphinx

__version__ = "0.3.2"


def st_static_path(app):
static_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "_static"))
app.config.html_static_path.append(static_path)

def define_texts(app: Sphinx, config: Config) -> None:
if config.language is not None and config.language_map is not None:
lg = config.language_map[config.language]
if "togglebutton_hint" in lg:
config.togglebutton_hint = lg["togglebutton_hint"]
if "togglebutton_hint_hide" in lg:
config.togglebutton_hint_hide = lg["togglebutton_hint_hide"]
if "togglebutton_open_on_print" in lg:
config.togglebutton_open_on_print = lg["togglebutton_open_on_print"]


def initialize_js_assets(app, config):
define_texts(app, config)
# Update the global context
app.add_js_file(None, body=f"let toggleHintShow = '{config.togglebutton_hint}';")
app.add_js_file(None, body=f"let toggleHintHide = '{config.togglebutton_hint_hide}';")
Expand Down Expand Up @@ -59,6 +72,8 @@ def setup(app):
# Add the string we'll use to select items in the JS
# Tell Sphinx about this configuration variable
app.add_config_value("togglebutton_selector", ".toggle, .admonition.dropdown", "html")
if not hasattr(app.config, "language_map"):
app.add_config_value("language_map", None, "env") # A map of translated strings
app.add_config_value("togglebutton_hint", "Click to show", "html")
app.add_config_value("togglebutton_hint_hide", "Click to hide", "html")
app.add_config_value("togglebutton_open_on_print", True, "html")
Expand Down