11"""A small sphinx extension to add "toggle" buttons to items."""
2+
23import os
34from docutils .parsers .rst import Directive , directives
45from docutils import nodes
56
67from sphinx .locale import get_translation
8+
79MESSAGE_CATALOG_NAME = "togglebutton"
810translate = get_translation (MESSAGE_CATALOG_NAME )
911
@@ -17,16 +19,22 @@ def st_static_path(app):
1719
1820def initialize_js_assets (app , config ):
1921 # Update the global context
22+
2023 app .add_js_file (None , body = f"let toggleHintShow = '{ config .togglebutton_hint } ';" )
21- app .add_js_file (None , body = f"let toggleHintHide = '{ config .togglebutton_hint_hide } ';" )
24+ app .add_js_file (
25+ None , body = f"let toggleHintHide = '{ config .togglebutton_hint_hide } ';"
26+ )
2227 open_print = str (config .togglebutton_open_on_print ).lower ()
2328 app .add_js_file (None , body = f"let toggleOpenOnPrint = '{ open_print } ';" )
2429 app .add_js_file ("togglebutton.js" )
2530
2631
2732# This function reads in a variable and inserts it into JavaScript
33+
34+
2835def insert_custom_selection_config (app ):
2936 # This is a configuration that you've specified for users in `conf.py`
37+
3038 selector = app .config ["togglebutton_selector" ]
3139 js_text = "var togglebuttonSelector = '%s';" % selector
3240 app .add_js_file (None , body = js_text )
@@ -46,34 +54,43 @@ def run(self):
4654 classes = ["toggle" ]
4755 if "show" in self .options :
4856 classes .append ("toggle-shown" )
49-
5057 parent = nodes .container (classes = classes )
5158 self .state .nested_parse (self .content , self .content_offset , parent )
5259 return [parent ]
5360
5461
5562# We connect this function to the step after the builder is initialized
63+
64+
5665def setup (app ):
5766 # add translations
67+
5868 package_dir = os .path .abspath (os .path .dirname (__file__ ))
5969 locale_dir = os .path .join (package_dir , "translations" , "locales" )
6070 app .add_message_catalog (MESSAGE_CATALOG_NAME , locale_dir )
6171
62-
6372 # Add our static path
73+
6474 app .connect ("builder-inited" , st_static_path )
6575
6676 # Add relevant code to headers
77+
6778 app .add_css_file ("togglebutton.css" )
6879
6980 # Add the string we'll use to select items in the JS
7081 # Tell Sphinx about this configuration variable
71- app .add_config_value ("togglebutton_selector" , ".toggle, .admonition.dropdown" , "html" )
82+
83+ app .add_config_value (
84+ "togglebutton_selector" , ".toggle, .admonition.dropdown" , "html"
85+ )
7286 app .add_config_value ("togglebutton_hint" , f"{ translate ('Click to show' )} " , "html" )
73- app .add_config_value ("togglebutton_hint_hide" , f"{ translate ('Click to hide' )} " , "html" )
87+ app .add_config_value (
88+ "togglebutton_hint_hide" , f"{ translate ('Click to hide' )} " , "html"
89+ )
7490 app .add_config_value ("togglebutton_open_on_print" , True , "html" )
7591
7692 # Run the function after the builder is initialized
93+
7794 app .connect ("builder-inited" , insert_custom_selection_config )
7895 app .connect ("config-inited" , initialize_js_assets )
7996 app .add_directive ("toggle" , Toggle )
0 commit comments