44from docutils .statemachine import StringList
55from sphinx .application import Sphinx
66from sphinx .util .docutils import SphinxDirective
7- from sphinx_panels .tabs import TabbedDirective
7+ from sphinx_design .tabs import TabSetDirective
88
99
1010here = Path (__file__ ).parent
@@ -17,7 +17,10 @@ class WidgetExample(SphinxDirective):
1717 required_arguments = 1
1818 _next_id = 0
1919
20- option_spec = {"linenos" : directives .flag }
20+ option_spec = {
21+ "linenos" : directives .flag ,
22+ "live-example-is-default-tab" : directives .flag ,
23+ }
2124
2225 def run (self ):
2326 example_name = self .arguments [0 ]
@@ -30,45 +33,39 @@ def run(self):
3033 f"Missing example file named { py_ex_path } referenced by document { src_file } :{ line_num } "
3134 )
3235
33- py_code_tab = TabbedDirective (
34- "WidgetExample" ,
35- ["Python Code" ],
36- {},
37- _literal_include_py_lines (
36+ labeled_tab_items = {
37+ "Python Code" : _literal_include_py (
3838 name = example_name ,
3939 linenos = show_linenos ,
4040 ),
41- self .lineno - 1 ,
42- self .content_offset ,
43- "" ,
44- self .state ,
45- self .state_machine ,
46- ).run ()
41+ "Live Example" : _interactive_widget (
42+ name = example_name ,
43+ use_activate_button = "live-example-is-default-tab" not in self .options ,
44+ ),
45+ }
4746
4847 if (examples / f"{ example_name } .js" ).exists ():
49- js_code_tab = TabbedDirective (
50- "WidgetExample" ,
51- ["Javascript Code" ],
52- {},
53- _literal_include_js_lines (
54- name = example_name ,
55- linenos = show_linenos ,
56- ),
57- self .lineno - 1 ,
58- self .content_offset ,
59- "" ,
60- self .state ,
61- self .state_machine ,
62- ).run ()
63- else :
64- js_code_tab = []
65-
66- example_tab = TabbedDirective (
48+ labeled_tab_items ["Javascript Code" ] = _literal_include_js (
49+ name = example_name ,
50+ linenos = show_linenos ,
51+ )
52+
53+ tab_label_order = (
54+ ["Live Example" , "Python Code" , "Javascript Code" ]
55+ if "live-example-is-default-tab" in self .options
56+ else ["Python Code" , "Javascript Code" , "Live Example" ]
57+ )
58+
59+ return TabSetDirective (
6760 "WidgetExample" ,
68- ["Live Example" ],
61+ [],
6962 {},
70- _string_to_nested_lines (
71- _interactive_widget_template .format (name = example_name )
63+ _make_tab_items (
64+ [
65+ (label , labeled_tab_items [label ])
66+ for label in tab_label_order
67+ if label in labeled_tab_items
68+ ]
7269 ),
7370 self .lineno - 1 ,
7471 self .content_offset ,
@@ -77,33 +74,52 @@ def run(self):
7774 self .state_machine ,
7875 ).run ()
7976
80- return py_code_tab + js_code_tab + example_tab
8177
82-
83- def _literal_include_py_lines (name , linenos ):
84- return _string_to_nested_lines (
85- _literal_include_template .format (
86- name = name ,
87- ext = "py" ,
88- language = "python" ,
89- linenos = ":linenos:" if linenos else "" ,
78+ def _make_tab_items (labeled_content_tuples ):
79+ tab_items = ""
80+ for label , content in labeled_content_tuples :
81+ tab_items += _tab_item_template .format (
82+ label = label ,
83+ content = content .replace ("\n " , "\n " ),
9084 )
85+ return _string_to_nested_lines (tab_items )
86+
87+
88+ def _literal_include_py (name , linenos ):
89+ return _literal_include_template .format (
90+ name = name ,
91+ ext = "py" ,
92+ language = "python" ,
93+ linenos = ":linenos:" if linenos else "" ,
9194 )
9295
9396
94- def _literal_include_js_lines (name , linenos ):
95- return _string_to_nested_lines (
96- _literal_include_template .format (
97- name = name ,
98- ext = "js" ,
99- language = "javascript" ,
100- linenos = ":linenos:" if linenos else "" ,
101- )
97+ def _literal_include_js (name , linenos ):
98+ return _literal_include_template .format (
99+ name = name ,
100+ ext = "js" ,
101+ language = "javascript" ,
102+ linenos = ":linenos:" if linenos else "" ,
102103 )
103104
104105
106+ def _interactive_widget (name , use_activate_button ):
107+ return _interactive_widget_template .format (
108+ name = name ,
109+ activate_button_opt = "" if use_activate_button else ":no-activate-button:" ,
110+ )
111+
112+
113+ _tab_item_template = """
114+ .. tab-item:: {label}
115+
116+ {content}
117+ """
118+
119+
105120_interactive_widget_template = """
106121.. interactive-widget:: {name}
122+ {activate_button_opt}
107123"""
108124
109125
0 commit comments