diff --git a/_config.yml b/_config.yml index 98bebd0..3b3c77c 100644 --- a/_config.yml +++ b/_config.yml @@ -54,6 +54,7 @@ sphinx: local_extensions: slide_meta: "extensions" # thebe_local: "extensions" + fix_buttons: "extensions" config: html_theme_options: use_fullscreen_button: false diff --git a/extensions/fix_buttons/__init__.py b/extensions/fix_buttons/__init__.py new file mode 100644 index 0000000..13e7228 --- /dev/null +++ b/extensions/fix_buttons/__init__.py @@ -0,0 +1,50 @@ +# import json +from pathlib import Path + +from sphinx.util import logging +# from docutils import nodes + +__version__ = "0.1" + +def add_static_sources(app): + static_path = Path(__file__).parent / "static" + app.config.html_static_path.append(static_path.as_posix()) + + +def remove_buttons(app, pagename, _templatename, context, _doctree): + if not pagename.startswith("U0"): + # not one of the units, don't do anything + return + + is_engish = pagename.endswith(".en") + + buttons = context["header_buttons"] + + keep = [] + for button in buttons: + try: + if button["tooltip"] == "Start presenting": + keep.append(button) + except Exception as _e: + pass + + keep.append({ + "type": "javascript", + "javascript": "switchLanguage()", + "tooltip": "Deutsch" if is_engish else "English", + "icon": "fas fa-language", + }) + app.add_js_file("switch-language.js") + + context["header_buttons"] = keep + + +def setup(app): + app.connect("builder-inited", add_static_sources) + app.connect("html-page-context", remove_buttons, priority=9999) + + return { + "version": __version__, + "parallel_read_safe": True, + "parallel_write_safe": True, + } diff --git a/extensions/fix_buttons/static/switch-language.js b/extensions/fix_buttons/static/switch-language.js new file mode 100644 index 0000000..5a70b84 --- /dev/null +++ b/extensions/fix_buttons/static/switch-language.js @@ -0,0 +1,19 @@ +const switchLanguage = () => { + // Get current URL + let url = window.location.href; + let path = window.location.pathname; + let filename = path.split("/").pop(); + + // Detect if it's an English page + if (filename.endsWith(".en.html")) { + // Switch to German: remove ".en" + let newFilename = filename.replace(".en.html", ".html"); + let newUrl = url.replace(filename, newFilename); + window.location.href = newUrl; + } else if (filename.endsWith(".html")) { + // Switch to English: add ".en" + let newFilename = filename.replace(".html", ".en.html"); + let newUrl = url.replace(filename, newFilename); + window.location.href = newUrl; + } +}