Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ coverage.xml
.pytest_cache/

# Translations
*.mo
# *.mo
*.pot

# Django stuff:
Expand Down
8 changes: 8 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
graft doc/

recursive-include sphinx_togglebutton *.js
recursive-include sphinx_togglebutton *.css

recursive-include sphinx_togglebutton *.json
recursive-include sphinx_togglebutton *.mo
recursive-include sphinx_togglebutton *.po
recursive-include sphinx_togglebutton *.py
10 changes: 9 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@
license="MIT License",
packages=find_packages(),
package_data={
"sphinx_togglebutton": ["_static/togglebutton.css", "_static/togglebutton.js", "_static/togglebutton-chevron.svg"]
"sphinx_togglebutton": [
"_static/togglebutton.css",
"_static/togglebutton.js",
"_static/togglebutton-chevron.svg",
"translations/README.md",
"translations/_convert.py",
"translations/jsons/*.json",
"translations/locales/**/*"
]
},
install_requires=["setuptools", "wheel", "sphinx", "docutils"],
extras_require={"sphinx": ["matplotlib", "numpy", "myst_nb", "sphinx_book_theme", "sphinx_design", "sphinx_examples"]},
Expand Down
14 changes: 12 additions & 2 deletions sphinx_togglebutton/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
from docutils.parsers.rst import Directive, directives
from docutils import nodes

from sphinx.locale import get_translation
MESSAGE_CATALOG_NAME = "togglebutton"
translate = get_translation(MESSAGE_CATALOG_NAME)

__version__ = "0.3.2"


Expand Down Expand Up @@ -50,6 +54,12 @@ def run(self):

# We connect this function to the step after the builder is initialized
def setup(app):
# add translations
package_dir = os.path.abspath(os.path.dirname(__file__))
locale_dir = os.path.join(package_dir, "translations", "locales")
app.add_message_catalog(MESSAGE_CATALOG_NAME, locale_dir)


# Add our static path
app.connect("builder-inited", st_static_path)

Expand All @@ -59,8 +69,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")
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_hint", f"{translate('Click to show')}", "html")
app.add_config_value("togglebutton_hint_hide", f"{translate('Click to hide')}", "html")
app.add_config_value("togglebutton_open_on_print", True, "html")

# Run the function after the builder is initialized
Expand Down
46 changes: 46 additions & 0 deletions sphinx_togglebutton/_static/togglebutton.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ var toggleHidden = (button) => {
}
};

// Function to synchronize the data-toggle-hint with the current state
var syncToggleHint = (button) => {
const target = button.dataset["target"];
const itemToToggle = document.getElementById(target);

if (itemToToggle && itemToToggle.classList.contains("toggle-hidden")) {
button.dataset.toggleHint = toggleHintShow;
button.setAttribute("aria-expanded", false);
} else if (itemToToggle) {
button.dataset.toggleHint = toggleHintHide;
button.setAttribute("aria-expanded", true);
}
};

// Function to sync all toggle buttons - can be called by external extensions
var syncAllToggleHints = () => {
document.querySelectorAll('.toggle-button').forEach(syncToggleHint);
};

var toggleClickHandler = (click) => {
// Be cause the admonition title is clickable and extends to the whole admonition
// We only look for a click event on this title to trigger the toggle.
Expand Down Expand Up @@ -170,6 +189,33 @@ const sphinxToggleRunWhenDOMLoaded = (cb) => {
sphinxToggleRunWhenDOMLoaded(addToggleToSelector);
sphinxToggleRunWhenDOMLoaded(initToggleItems);

// Set up MutationObserver to watch for external changes to toggle states
sphinxToggleRunWhenDOMLoaded(() => {
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
const target = mutation.target;
// Check if this is a toggle item that had its class changed
if (target.classList.contains('toggle')) {
// Find the associated toggle button and sync its hint
const button = target.querySelector('.toggle-button');
if (button) {
syncToggleHint(button);
}
}
}
});
});

// Start observing class changes on all toggle elements
document.querySelectorAll('.toggle').forEach((toggleElement) => {
observer.observe(toggleElement, {
attributes: true,
attributeFilter: ['class']
});
});
});

/** Toggle details blocks to be open when printing */
if (toggleOpenOnPrint == "true") {
window.addEventListener("beforeprint", () => {
Expand Down
3 changes: 3 additions & 0 deletions sphinx_togglebutton/translations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
JSONs created using GitHub Copilot Pro.

To convert to locale files run `_convert.py` in this folder.
58 changes: 58 additions & 0 deletions sphinx_togglebutton/translations/_convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import json
import os
from pathlib import Path
import subprocess

MESSAGE_CATALOG_NAME = "togglebutton"

def convert_json(folder=None):
folder = folder or Path(__file__).parent

# remove exising
for path in (folder / "locales").glob(f"**/{MESSAGE_CATALOG_NAME}.po"):
path.unlink()

# compile po
for path in (folder / "jsons").glob("*.json"):
data = json.loads(path.read_text("utf8"))
assert data[0]["symbol"] == "en"
english = data[0]["text"]
for item in data[1:]:
language = item["symbol"]
out_path = folder / "locales" / language / "LC_MESSAGES" / f"{MESSAGE_CATALOG_NAME}.po"
if not out_path.parent.exists():
out_path.parent.mkdir(parents=True)
if not out_path.exists():
header = f"""
msgid ""
msgstr ""
"Project-Id-Version: Sphinx-ToggleButton\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Language: {language}\\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\\n"
"""
out_path.write_text(header)

with out_path.open("a", encoding="utf8") as f:
f.write("\n")
f.write(f'msgid "{english}"\n')
text = item["text"].replace('"', '\\"')
f.write(f'msgstr "{text}"\n')

# compile mo
for path in (folder / "locales").glob(f"**/{MESSAGE_CATALOG_NAME}.po"):
print(path)
subprocess.check_call(
[
"msgfmt",
os.path.abspath(path),
"-o",
os.path.abspath(path.parent / f"{MESSAGE_CATALOG_NAME}.mo"),
]
)


if __name__ == "__main__":
convert_json()
34 changes: 34 additions & 0 deletions sphinx_togglebutton/translations/jsons/Hide.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
{"language":"English","symbol":"en","text":"Click to hide"},
{"language":"Chinese (Simplified)","symbol":"zh-cn","text":"点击隐藏"},
{"language":"Chinese (Traditional)","symbol":"zh-tw","text":"點擊隱藏"},
{"language":"Hindi","symbol":"hi","text":"छुपाने के लिए क्लिक करें"},
{"language":"Spanish","symbol":"es","text":"Haz clic para ocultar"},
{"language":"French","symbol":"fr","text":"Cliquez pour masquer"},
{"language":"Arabic","symbol":"ar","text":"انقر للإخفاء"},
{"language":"Bengali","symbol":"bn","text":"লুকাতে ক্লিক করুন"},
{"language":"Russian","symbol":"ru","text":"Нажмите, чтобы скрыть"},
{"language":"Portuguese","symbol":"pt","text":"Clique para ocultar"},
{"language":"Indonesian","symbol":"id","text":"Klik untuk menyembunyikan"},
{"language":"Japanese","symbol":"ja","text":"クリックして非表示"},
{"language":"German","symbol":"de","text":"Klicken zum Ausblenden"},
{"language":"Korean","symbol":"ko","text":"숨기려면 클릭"},
{"language":"Turkish","symbol":"tr","text":"Gizlemek için tıklayın"},
{"language":"Vietnamese","symbol":"vi","text":"Nhấp để ẩn"},
{"language":"Tamil","symbol":"ta","text":"மறைக்க கிளிக் செய்யவும்"},
{"language":"Italian","symbol":"it","text":"Clicca per nascondere"},
{"language":"Thai","symbol":"th","text":"คลิกเพื่อซ่อน"},
{"language":"Dutch","symbol":"nl","text":"Klik om te verbergen"},
{"language":"Greek","symbol":"el","text":"Κάντε κλικ για απόκρυψη"},
{"language":"Polish","symbol":"pl","text":"Kliknij, aby ukryć"},
{"language":"Ukrainian","symbol":"uk","text":"Натисніть, щоб приховати"},
{"language":"Persian","symbol":"fa","text":"برای مخفی کردن کلیک کنید"},
{"language":"Malay","symbol":"ms","text":"Klik untuk menyembunyikan"},
{"language":"Swahili","symbol":"sw","text":"Bonyeza kuficha"},
{"language":"Romanian","symbol":"ro","text":"Faceți clic pentru a ascunde"},
{"language":"Czech","symbol":"cs","text":"Klikněte pro skrytí"},
{"language":"Hungarian","symbol":"hu","text":"Kattintson az elrejtéshez"},
{"language":"Hebrew","symbol":"he","text":"לחץ להסתרה"},
{"language":"Swedish","symbol":"sv","text":"Klicka för att dölja"},
{"language":"Norwegian","symbol":"no","text":"Klikk for å skjule"}
]
34 changes: 34 additions & 0 deletions sphinx_togglebutton/translations/jsons/Show.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
{"language":"English","symbol":"en","text":"Click to show"},
{"language":"Chinese (Simplified)","symbol":"zh-cn","text":"点击显示"},
{"language":"Chinese (Traditional)","symbol":"zh-tw","text":"點擊顯示"},
{"language":"Hindi","symbol":"hi","text":"दिखाने के लिए क्लिक करें"},
{"language":"Spanish","symbol":"es","text":"Haz clic para mostrar"},
{"language":"French","symbol":"fr","text":"Cliquez pour afficher"},
{"language":"Arabic","symbol":"ar","text":"انقر للعرض"},
{"language":"Bengali","symbol":"bn","text":"দেখাতে ক্লিক করুন"},
{"language":"Russian","symbol":"ru","text":"Нажмите, чтобы показать"},
{"language":"Portuguese","symbol":"pt","text":"Clique para mostrar"},
{"language":"Indonesian","symbol":"id","text":"Klik untuk menampilkan"},
{"language":"Japanese","symbol":"ja","text":"クリックして表示"},
{"language":"German","symbol":"de","text":"Klicken zum Anzeigen"},
{"language":"Korean","symbol":"ko","text":"표시하려면 클릭"},
{"language":"Turkish","symbol":"tr","text":"Göstermek için tıklayın"},
{"language":"Vietnamese","symbol":"vi","text":"Nhấp để hiển thị"},
{"language":"Tamil","symbol":"ta","text":"காட்ட கிளிக் செய்யவும்"},
{"language":"Italian","symbol":"it","text":"Clicca per mostrare"},
{"language":"Thai","symbol":"th","text":"คลิกเพื่อแสดง"},
{"language":"Dutch","symbol":"nl","text":"Klik om te tonen"},
{"language":"Greek","symbol":"el","text":"Κάντε κλικ για εμφάνιση"},
{"language":"Polish","symbol":"pl","text":"Kliknij, aby pokazać"},
{"language":"Ukrainian","symbol":"uk","text":"Натисніть, щоб показати"},
{"language":"Persian","symbol":"fa","text":"برای نمایش کلیک کنید"},
{"language":"Malay","symbol":"ms","text":"Klik untuk menunjukkan"},
{"language":"Swahili","symbol":"sw","text":"Bonyeza kuonyesha"},
{"language":"Romanian","symbol":"ro","text":"Faceți clic pentru a afișa"},
{"language":"Czech","symbol":"cs","text":"Klikněte pro zobrazení"},
{"language":"Hungarian","symbol":"hu","text":"Kattintson a megjelenítéshez"},
{"language":"Hebrew","symbol":"he","text":"לחץ להצגה"},
{"language":"Swedish","symbol":"sv","text":"Klicka för att visa"},
{"language":"Norwegian","symbol":"no","text":"Klikk for å vise"}
]
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

msgid ""
msgstr ""
"Project-Id-Version: Sphinx-ToggleButton\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: ar\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

msgid "Click to hide"
msgstr "انقر للإخفاء"

msgid "Click to show"
msgstr "انقر للعرض"
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

msgid ""
msgstr ""
"Project-Id-Version: Sphinx-ToggleButton\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: bn\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

msgid "Click to hide"
msgstr "লুকাতে ক্লিক করুন"

msgid "Click to show"
msgstr "দেখাতে ক্লিক করুন"
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

msgid ""
msgstr ""
"Project-Id-Version: Sphinx-ToggleButton\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: cs\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

msgid "Click to hide"
msgstr "Klikněte pro skrytí"

msgid "Click to show"
msgstr "Klikněte pro zobrazení"
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

msgid ""
msgstr ""
"Project-Id-Version: Sphinx-ToggleButton\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: de\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

msgid "Click to hide"
msgstr "Klicken zum Ausblenden"

msgid "Click to show"
msgstr "Klicken zum Anzeigen"
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

msgid ""
msgstr ""
"Project-Id-Version: Sphinx-ToggleButton\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: el\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

msgid "Click to hide"
msgstr "Κάντε κλικ για απόκρυψη"

msgid "Click to show"
msgstr "Κάντε κλικ για εμφάνιση"
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

msgid ""
msgstr ""
"Project-Id-Version: Sphinx-ToggleButton\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: es\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

msgid "Click to hide"
msgstr "Haz clic para ocultar"

msgid "Click to show"
msgstr "Haz clic para mostrar"
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

msgid ""
msgstr ""
"Project-Id-Version: Sphinx-ToggleButton\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: fa\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

msgid "Click to hide"
msgstr "برای مخفی کردن کلیک کنید"

msgid "Click to show"
msgstr "برای نمایش کلیک کنید"
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

msgid ""
msgstr ""
"Project-Id-Version: Sphinx-ToggleButton\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: fr\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

msgid "Click to hide"
msgstr "Cliquez pour masquer"

msgid "Click to show"
msgstr "Cliquez pour afficher"
Binary file not shown.
Loading