Add language button and remove all others
This commit is contained in:
parent
916c9e60e4
commit
19113626fb
@ -54,6 +54,7 @@ sphinx:
|
||||
local_extensions:
|
||||
slide_meta: "extensions"
|
||||
# thebe_local: "extensions"
|
||||
fix_buttons: "extensions"
|
||||
config:
|
||||
html_theme_options:
|
||||
use_fullscreen_button: false
|
||||
|
||||
50
extensions/fix_buttons/__init__.py
Normal file
50
extensions/fix_buttons/__init__.py
Normal file
@ -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,
|
||||
}
|
||||
19
extensions/fix_buttons/static/switch-language.js
Normal file
19
extensions/fix_buttons/static/switch-language.js
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user