77from docutils .statemachine import StringList
88
99from ipypublish .sphinx .utils import import_sphinx
10- from ipypublish .sphinx .notebook .nodes import (
11- AdmonitionNode , CodeAreaNode , FancyOutputNode
12- )
10+ from ipypublish .sphinx .notebook .nodes import (AdmonitionNode , CodeAreaNode , FancyOutputNode )
1311
1412
1513class NbAdmonition (rst .Directive ):
@@ -51,13 +49,14 @@ class NbInput(rst.Directive):
5149 'empty-lines-after' : rst .directives .nonnegative_int ,
5250 'no-output' : rst .directives .flag ,
5351 'caption' : rst .directives .unchanged ,
54- 'name' : rst .directives .unchanged
52+ 'name' : rst .directives .unchanged ,
53+ 'add-toggle' : rst .directives .flag
5554 }
5655 has_content = True
5756
5857 def run (self ):
5958 """This is called by the reST parser."""
60- self .state .document ['nbsphinx_include_css ' ] = True
59+ self .state .document ['ipysphinx_include_css ' ] = True
6160 return _create_nbcell_nodes (self )
6261
6362
@@ -78,7 +77,7 @@ class NbOutput(rst.Directive):
7877
7978 def run (self ):
8079 """This is called by the reST parser."""
81- self .state .document ['nbsphinx_include_css ' ] = True
80+ self .state .document ['ipysphinx_include_css ' ] = True
8281 return _create_nbcell_nodes (self )
8382
8483
@@ -114,15 +113,14 @@ def _create_nbcell_nodes(directive):
114113 if directive .arguments and directive .arguments [0 ] in ['rst' , 'ansi' ]:
115114 fancy_output = True
116115 else :
117- raise AssertionError (" directive should be NbInput or NbOutput" )
116+ raise AssertionError (' directive should be NbInput or NbOutput' )
118117
119118 outer_node = docutils .nodes .container (classes = outer_classes )
120119
121120 # add prompts
122121 if config .ipysphinx_show_prompts and execution_count :
123122 prompt = prompt_template .format (count = execution_count )
124- prompt_node = docutils .nodes .literal_block (
125- prompt , prompt , language = 'none' , classes = ['prompt' ])
123+ prompt_node = docutils .nodes .literal_block (prompt , prompt , language = 'none' , classes = ['prompt' ])
126124 elif config .ipysphinx_show_prompts :
127125 prompt = ''
128126 prompt_node = docutils .nodes .container (classes = ['prompt' , 'empty' ])
@@ -132,21 +130,17 @@ def _create_nbcell_nodes(directive):
132130
133131 if fancy_output :
134132 inner_node = docutils .nodes .container (classes = inner_classes )
135- sphinx .util .nodes .nested_parse_with_titles (
136- directive .state , directive .content , inner_node )
133+ sphinx .util .nodes .nested_parse_with_titles (directive .state , directive .content , inner_node )
137134 outtype = directive .arguments [0 ]
138135 if outtype == 'rst' :
139136 outer_node += FancyOutputNode ('' , inner_node , prompt = prompt )
140137 elif outtype == 'ansi' :
141138 outer_node += inner_node
142139 else :
143- raise AssertionError (
144- "`.. nboutput:: type` should be 'rst' or 'ansi', "
145- "not: {}" .format (outtype ))
140+ raise AssertionError ("`.. nboutput:: type` should be 'rst' or 'ansi', " 'not: {}' .format (outtype ))
146141 else :
147142 text = '\n ' .join (directive .content .data )
148- inner_node = docutils .nodes .literal_block (
149- text , text , language = language , classes = inner_classes )
143+ inner_node = docutils .nodes .literal_block (text , text , language = language , classes = inner_classes )
150144 codearea_node = CodeAreaNode ('' , inner_node , prompt = prompt )
151145 # create a literal text block (e.g. with the code-block directive),
152146 # that starts or ends with a blank line
@@ -157,34 +151,34 @@ def _create_nbcell_nodes(directive):
157151 codearea_node [attr ] = value
158152
159153 # add caption and label, see:
160- if directive .options .get ("caption" , False ):
161- caption = directive .options .get ("caption" )
162- wrapper = container_wrapper (
163- directive , inner_node , caption , inner_classes )
154+ if directive .options .get ('caption' , False ):
155+ caption = directive .options .get ('caption' )
156+ wrapper = container_wrapper (directive , inner_node , caption , inner_classes )
164157 # add label
165158 directive .add_name (wrapper )
166159 outer_node += wrapper
167160 else :
168161 outer_node += codearea_node
169162
163+ if isinstance (directive , NbInput ) and (config .ipysphinx_code_toggle or 'add-toggle' in directive .options ):
164+ outer_node += sphinx .addnodes .only (
165+ '' , docutils .nodes .container (classes = ['toggle-nbinput' , 'empty' ]), expr = 'html' )
166+
170167 return [outer_node ]
171168
172169
173170def container_wrapper (directive , literal_node , caption , classes ):
174171 """adapted from
175172 https://github.com/sphinx-doc/sphinx/blob/master/sphinx/directives/code.py
176173 """
177- container_node = docutils .nodes .container (
178- '' , literal_block = True , classes = classes ) # ['literal-block-wrapper']
174+ container_node = docutils .nodes .container ('' , literal_block = True , classes = classes ) # ['literal-block-wrapper']
179175 parsed = docutils .nodes .Element ()
180- directive .state .nested_parse (StringList ([caption ], source = '' ),
181- directive .content_offset , parsed )
176+ directive .state .nested_parse (StringList ([caption ], source = '' ), directive .content_offset , parsed )
182177 if isinstance (parsed [0 ], docutils .nodes .system_message ):
183178 msg = 'Invalid caption: %s' % parsed [0 ].astext ()
184179 raise ValueError (msg )
185180 elif isinstance (parsed [0 ], docutils .nodes .Element ):
186- caption_node = docutils .nodes .caption (parsed [0 ].rawsource , '' ,
187- * parsed [0 ].children )
181+ caption_node = docutils .nodes .caption (parsed [0 ].rawsource , '' , * parsed [0 ].children )
188182 caption_node .source = literal_node .source
189183 caption_node .line = literal_node .line
190184 container_node += caption_node
0 commit comments