Skip to content

Commit

Permalink
Merge pull request #95 from chrisjsewell/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell authored Jul 23, 2019
2 parents 8b4f1ac + 6598ec7 commit f9f82b5
Show file tree
Hide file tree
Showing 14 changed files with 882 additions and 115 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ recursive-include ipypublish *.json
recursive-include ipypublish *.j2
recursive-include ipypublish *.yaml
recursive-include ipypublish/sphinx/notebook/css *.css
recursive-include ipypublish/sphinx/notebook/js *.js
include ipypublish/tests/test_files/example.jpg
6 changes: 6 additions & 0 deletions docs/source/releases.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ Releases
Version 0.10
------------

v0.10.6
~~~~~~~

Added sphinx option for toggling code cells;
see :ref:`sphinx_ext_notebook_toggle_in` example.

v0.10.5
~~~~~~~

Expand Down
50 changes: 44 additions & 6 deletions docs/source/sphinx_ext_notebook.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,27 @@ setup by adding to the conf.py:
.. table:: Configuration values to use in conf.py
:name: tbl:sphinx_config

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

Examples
--------

Basic input
~~~~~~~~~~~

.. code-block:: rst
.. nbinput:: python
Expand All @@ -142,6 +147,10 @@ Examples

print("hallo")


Basic output
~~~~~~~~~~~~

.. code-block:: rst
.. nboutput::
Expand All @@ -154,6 +163,36 @@ Examples

hallo

.. _sphinx_ext_notebook_toggle_in:

Toggle input
~~~~~~~~~~~~

.. code-block:: rst
.. nbinput:: python
:add-toggle:
:execution-count: 3
j = 0
for i in range(3):
print(i)
j += i
print(j)
.. nbinput:: python
:add-toggle:
:execution-count: 3

j = 0
for i in range(3):
print(i)
j += i
print(j)

Information and Warnings
~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: rst
.. nbinfo:: Some information
Expand All @@ -165,4 +204,3 @@ Examples
.. nbwarning:: This is a warning
.. nbwarning:: This is a warning

2 changes: 1 addition & 1 deletion ipypublish/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from ipypublish.scripts import nb_setup # noqa: F401

__version__ = '0.10.5'
__version__ = '0.10.6'
19 changes: 18 additions & 1 deletion ipypublish/sphinx/notebook/css/nb_cells.css
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,21 @@ div.rendered_html tbody tr:hover {
background: rgba(66, 165, 245, 0.2);
}


.toggle-nbinput {
min-width: 2ex;
padding-top: 0.4em;
padding-left: 0.1em;
text-align: right;
flex: 0;
margin-left:auto;
margin-right:0;
}
.toggle-nbinput p {
overflow: hidden;
}
.toggle-nbinput:after {
content: " ▼";
}
.toggle-nbinput.open:after {
content: " ▲";
}
46 changes: 20 additions & 26 deletions ipypublish/sphinx/notebook/directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
from docutils.statemachine import StringList

from ipypublish.sphinx.utils import import_sphinx
from ipypublish.sphinx.notebook.nodes import (
AdmonitionNode, CodeAreaNode, FancyOutputNode
)
from ipypublish.sphinx.notebook.nodes import (AdmonitionNode, CodeAreaNode, FancyOutputNode)


class NbAdmonition(rst.Directive):
Expand Down Expand Up @@ -51,13 +49,14 @@ class NbInput(rst.Directive):
'empty-lines-after': rst.directives.nonnegative_int,
'no-output': rst.directives.flag,
'caption': rst.directives.unchanged,
'name': rst.directives.unchanged
'name': rst.directives.unchanged,
'add-toggle': rst.directives.flag
}
has_content = True

def run(self):
"""This is called by the reST parser."""
self.state.document['nbsphinx_include_css'] = True
self.state.document['ipysphinx_include_css'] = True
return _create_nbcell_nodes(self)


Expand All @@ -78,7 +77,7 @@ class NbOutput(rst.Directive):

def run(self):
"""This is called by the reST parser."""
self.state.document['nbsphinx_include_css'] = True
self.state.document['ipysphinx_include_css'] = True
return _create_nbcell_nodes(self)


Expand Down Expand Up @@ -114,15 +113,14 @@ def _create_nbcell_nodes(directive):
if directive.arguments and directive.arguments[0] in ['rst', 'ansi']:
fancy_output = True
else:
raise AssertionError("directive should be NbInput or NbOutput")
raise AssertionError('directive should be NbInput or NbOutput')

outer_node = docutils.nodes.container(classes=outer_classes)

# add prompts
if config.ipysphinx_show_prompts and execution_count:
prompt = prompt_template.format(count=execution_count)
prompt_node = docutils.nodes.literal_block(
prompt, prompt, language='none', classes=['prompt'])
prompt_node = docutils.nodes.literal_block(prompt, prompt, language='none', classes=['prompt'])
elif config.ipysphinx_show_prompts:
prompt = ''
prompt_node = docutils.nodes.container(classes=['prompt', 'empty'])
Expand All @@ -132,21 +130,17 @@ def _create_nbcell_nodes(directive):

if fancy_output:
inner_node = docutils.nodes.container(classes=inner_classes)
sphinx.util.nodes.nested_parse_with_titles(
directive.state, directive.content, inner_node)
sphinx.util.nodes.nested_parse_with_titles(directive.state, directive.content, inner_node)
outtype = directive.arguments[0]
if outtype == 'rst':
outer_node += FancyOutputNode('', inner_node, prompt=prompt)
elif outtype == 'ansi':
outer_node += inner_node
else:
raise AssertionError(
"`.. nboutput:: type` should be 'rst' or 'ansi', "
"not: {}".format(outtype))
raise AssertionError("`.. nboutput:: type` should be 'rst' or 'ansi', " 'not: {}'.format(outtype))
else:
text = '\n'.join(directive.content.data)
inner_node = docutils.nodes.literal_block(
text, text, language=language, classes=inner_classes)
inner_node = docutils.nodes.literal_block(text, text, language=language, classes=inner_classes)
codearea_node = CodeAreaNode('', inner_node, prompt=prompt)
# create a literal text block (e.g. with the code-block directive),
# that starts or ends with a blank line
Expand All @@ -157,34 +151,34 @@ def _create_nbcell_nodes(directive):
codearea_node[attr] = value

# add caption and label, see:
if directive.options.get("caption", False):
caption = directive.options.get("caption")
wrapper = container_wrapper(
directive, inner_node, caption, inner_classes)
if directive.options.get('caption', False):
caption = directive.options.get('caption')
wrapper = container_wrapper(directive, inner_node, caption, inner_classes)
# add label
directive.add_name(wrapper)
outer_node += wrapper
else:
outer_node += codearea_node

if isinstance(directive, NbInput) and (config.ipysphinx_code_toggle or 'add-toggle' in directive.options):
outer_node += sphinx.addnodes.only(
'', docutils.nodes.container(classes=['toggle-nbinput', 'empty']), expr='html')

return [outer_node]


def container_wrapper(directive, literal_node, caption, classes):
"""adapted from
https://github.com/sphinx-doc/sphinx/blob/master/sphinx/directives/code.py
"""
container_node = docutils.nodes.container(
'', literal_block=True, classes=classes) # ['literal-block-wrapper']
container_node = docutils.nodes.container('', literal_block=True, classes=classes) # ['literal-block-wrapper']
parsed = docutils.nodes.Element()
directive.state.nested_parse(StringList([caption], source=''),
directive.content_offset, parsed)
directive.state.nested_parse(StringList([caption], source=''), directive.content_offset, parsed)
if isinstance(parsed[0], docutils.nodes.system_message):
msg = 'Invalid caption: %s' % parsed[0].astext()
raise ValueError(msg)
elif isinstance(parsed[0], docutils.nodes.Element):
caption_node = docutils.nodes.caption(parsed[0].rawsource, '',
*parsed[0].children)
caption_node = docutils.nodes.caption(parsed[0].rawsource, '', *parsed[0].children)
caption_node.source = literal_node.source
caption_node.line = literal_node.line
container_node += caption_node
Expand Down
Loading

0 comments on commit f9f82b5

Please sign in to comment.