Skip to content

Commit f9f82b5

Browse files
authored
Merge pull request #95 from chrisjsewell/develop
2 parents 8b4f1ac + 6598ec7 commit f9f82b5

File tree

14 files changed

+882
-115
lines changed

14 files changed

+882
-115
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ recursive-include ipypublish *.json
55
recursive-include ipypublish *.j2
66
recursive-include ipypublish *.yaml
77
recursive-include ipypublish/sphinx/notebook/css *.css
8+
recursive-include ipypublish/sphinx/notebook/js *.js
89
include ipypublish/tests/test_files/example.jpg

docs/source/releases.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ Releases
1313
Version 0.10
1414
------------
1515

16+
v0.10.6
17+
~~~~~~~
18+
19+
Added sphinx option for toggling code cells;
20+
see :ref:`sphinx_ext_notebook_toggle_in` example.
21+
1622
v0.10.5
1723
~~~~~~~
1824

docs/source/sphinx_ext_notebook.rst

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,27 @@ setup by adding to the conf.py:
110110
.. table:: Configuration values to use in conf.py
111111
:name: tbl:sphinx_config
112112

113-
============================= =========================== ==================================================================
113+
============================= =========================== ===================================================================
114114
Name Default Description
115-
============================= =========================== ==================================================================
115+
============================= =========================== ===================================================================
116116
ipysphinx_export_config "sphinx_ipypublish_all.ext" ipypublish configuration file to use for conversion to .rst
117117
ipysphinx_folder_suffix "_nbfiles" <fname><suffix> for dumping internal images, etc
118118
ipysphinx_overwrite_existing False raise error if nb_name.rst already exists
119119
ipysphinx_config_folders () additional folders containing ipypublish configuration files
120120
ipysphinx_show_prompts False show cell prompts
121-
ipysphinx_input_prompt "[{count}]:" format of input prompts
122-
ipysphinx_output_prompt "[{count}]:" format of output prompts
121+
ipysphinx_input_prompt "[{count}]:" format of input prompts
122+
ipysphinx_output_prompt "[{count}]:" format of output prompts
123+
ipysphinx_code_toggle False add a button at the right side of input cells, to toggle show/hide
124+
ipysphinx_code_hide False for input cells with a toggle, whether to initialise them as hidden
123125
ipysphinx_preconverters {} a mapping of additional file extensions to preconversion functions
124-
============================= =========================== ==================================================================
126+
============================= =========================== ===================================================================
125127

126128
Examples
127129
--------
128130

131+
Basic input
132+
~~~~~~~~~~~
133+
129134
.. code-block:: rst
130135
131136
.. nbinput:: python
@@ -142,6 +147,10 @@ Examples
142147

143148
print("hallo")
144149

150+
151+
Basic output
152+
~~~~~~~~~~~~
153+
145154
.. code-block:: rst
146155
147156
.. nboutput::
@@ -154,6 +163,36 @@ Examples
154163

155164
hallo
156165

166+
.. _sphinx_ext_notebook_toggle_in:
167+
168+
Toggle input
169+
~~~~~~~~~~~~
170+
171+
.. code-block:: rst
172+
173+
.. nbinput:: python
174+
:add-toggle:
175+
:execution-count: 3
176+
177+
j = 0
178+
for i in range(3):
179+
print(i)
180+
j += i
181+
print(j)
182+
183+
.. nbinput:: python
184+
:add-toggle:
185+
:execution-count: 3
186+
187+
j = 0
188+
for i in range(3):
189+
print(i)
190+
j += i
191+
print(j)
192+
193+
Information and Warnings
194+
~~~~~~~~~~~~~~~~~~~~~~~~
195+
157196
.. code-block:: rst
158197
159198
.. nbinfo:: Some information
@@ -165,4 +204,3 @@ Examples
165204
.. nbwarning:: This is a warning
166205
167206
.. nbwarning:: This is a warning
168-

ipypublish/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from ipypublish.scripts import nb_setup # noqa: F401
22

3-
__version__ = '0.10.5'
3+
__version__ = '0.10.6'

ipypublish/sphinx/notebook/css/nb_cells.css

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,21 @@ div.rendered_html tbody tr:hover {
202202
background: rgba(66, 165, 245, 0.2);
203203
}
204204

205-
205+
.toggle-nbinput {
206+
min-width: 2ex;
207+
padding-top: 0.4em;
208+
padding-left: 0.1em;
209+
text-align: right;
210+
flex: 0;
211+
margin-left:auto;
212+
margin-right:0;
213+
}
214+
.toggle-nbinput p {
215+
overflow: hidden;
216+
}
217+
.toggle-nbinput:after {
218+
content: " ▼";
219+
}
220+
.toggle-nbinput.open:after {
221+
content: " ▲";
222+
}

ipypublish/sphinx/notebook/directives.py

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
from docutils.statemachine import StringList
88

99
from 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

1513
class 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

173170
def 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

Comments
 (0)