Skip to content

Commit 90ed179

Browse files
committed
Restructure nodes in setup
1 parent 03d65fa commit 90ed179

File tree

1 file changed

+45
-33
lines changed

1 file changed

+45
-33
lines changed

jupyter_sphinx/execute.py

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -746,43 +746,17 @@ def setup(app):
746746

747747
app.add_config_value('jupyter_sphinx_thebelab_url', THEBELAB_URL_DEFAULT, 'html')
748748

749-
# JupyterKernelNode is just a doctree marker for the
750-
# ExecuteJupyterCells transform, so we don't actually render it.
749+
# Used for nodes that do not need to be rendered
751750
def skip(self, node):
752751
raise docutils.nodes.SkipNode
753752

754-
app.add_node(
755-
JupyterKernelNode,
756-
html=(skip, None),
757-
latex=(skip, None),
758-
textinfo=(skip, None),
759-
text=(skip, None),
760-
man=(skip, None),
761-
)
762-
763-
764-
# JupyterCellNode is a container that holds the input and
765-
# any output, so we render it as a container.
753+
# Renders the children of a container
766754
render_container = (
767755
lambda self, node: self.visit_container(node),
768756
lambda self, node: self.depart_container(node),
769757
)
770758

771-
app.add_node(
772-
JupyterCellNode,
773-
html=render_container,
774-
latex=render_container,
775-
textinfo=render_container,
776-
text=render_container,
777-
man=render_container,
778-
)
779-
780-
# JupyterWidgetViewNode holds widget view JSON,
781-
# but is only rendered properly in HTML documents.
782-
def visit_widget_html(self, node):
783-
self.body.append(node.html())
784-
raise docutils.nodes.SkipNode
785-
759+
# Used to render the container and its children as HTML
786760
def visit_container_html(self, node):
787761
self.body.append(node.visit_html())
788762
self.visit_container(node)
@@ -791,6 +765,12 @@ def depart_container_html(self, node):
791765
self.depart_container(node)
792766
self.body.append(node.depart_html())
793767

768+
# Used to render an element node as HTML
769+
def visit_element_html(self, node):
770+
self.body.append(node.html())
771+
raise docutils.nodes.SkipNode
772+
773+
# Used to render the ThebeSourceNode conditionally for non-HTML builders
794774
def visit_thebe_source(self, node):
795775
if node['hide_code']:
796776
raise docutils.nodes.SkipNode
@@ -802,9 +782,34 @@ def visit_thebe_source(self, node):
802782
lambda self, node: self.depart_container(node)
803783
)
804784

785+
786+
# JupyterKernelNode is just a doctree marker for the
787+
# ExecuteJupyterCells transform, so we don't actually render it.
788+
app.add_node(
789+
JupyterKernelNode,
790+
html=(skip, None),
791+
latex=(skip, None),
792+
textinfo=(skip, None),
793+
text=(skip, None),
794+
man=(skip, None),
795+
)
796+
797+
# JupyterCellNode is a container that holds the input and
798+
# any output, so we render it as a container.
799+
app.add_node(
800+
JupyterCellNode,
801+
html=render_container,
802+
latex=render_container,
803+
textinfo=render_container,
804+
text=render_container,
805+
man=render_container,
806+
)
807+
808+
# JupyterWidgetViewNode holds widget view JSON,
809+
# but is only rendered properly in HTML documents.
805810
app.add_node(
806811
JupyterWidgetViewNode,
807-
html=(visit_widget_html, None),
812+
html=(visit_element_html, None),
808813
latex=(skip, None),
809814
textinfo=(skip, None),
810815
text=(skip, None),
@@ -814,22 +819,27 @@ def visit_thebe_source(self, node):
814819
# but is only rendered in HTML documents.
815820
app.add_node(
816821
JupyterWidgetStateNode,
817-
html=(visit_widget_html, None),
822+
html=(visit_element_html, None),
818823
latex=(skip, None),
819824
textinfo=(skip, None),
820825
text=(skip, None),
821826
man=(skip, None),
822827
)
823828

829+
# ThebeSourceNode holds the source code and is rendered if
830+
# hide-code is not specified. For HTML it is always rendered,
831+
# but hidden using the stylesheet
824832
app.add_node(
825833
ThebeSourceNode,
826-
html=(visit_widget_html, None),
834+
html=(visit_element_html, None),
827835
latex=render_thebe_source,
828836
textinfo=render_thebe_source,
829837
text=render_thebe_source,
830838
man=render_thebe_source,
831839
)
832840

841+
# ThebeOutputNode holds the output of the Jupyter cells
842+
# and is rendered if hide-output is not specified.
833843
app.add_node(
834844
ThebeOutputNode,
835845
html=(visit_container_html, depart_container_html),
@@ -839,9 +849,11 @@ def visit_thebe_source(self, node):
839849
man=render_container,
840850
)
841851

852+
# ThebeButtonNode is the button that activates thebelab
853+
# and is only rendered for the HTML builder
842854
app.add_node(
843855
ThebeButtonNode,
844-
html=(visit_widget_html, None),
856+
html=(visit_element_html, None),
845857
latex=(skip, None),
846858
textinfo=(skip, None),
847859
text=(skip, None),

0 commit comments

Comments
 (0)