Skip to content

Commit dddf065

Browse files
committed
refactoring setup functions
1 parent ec6df86 commit dddf065

File tree

2 files changed

+46
-48
lines changed

2 files changed

+46
-48
lines changed

jupyter_sphinx/__init__.py

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,47 @@
2626

2727
logger = logging.getLogger(__name__)
2828

29+
##############################################################################
30+
# Constants and functions we'll use later
2931

32+
# Used for nodes that do not need to be rendered
33+
def skip(self, node):
34+
raise docutils.nodes.SkipNode
35+
36+
# Renders the children of a container
37+
render_container = (
38+
lambda self, node: self.visit_container(node),
39+
lambda self, node: self.depart_container(node),
40+
)
41+
42+
# Used to render the container and its children as HTML
43+
def visit_container_html(self, node):
44+
self.body.append(node.visit_html())
45+
self.visit_container(node)
46+
47+
def depart_container_html(self, node):
48+
self.depart_container(node)
49+
self.body.append(node.depart_html())
50+
51+
# Used to render an element node as HTML
52+
def visit_element_html(self, node):
53+
self.body.append(node.html())
54+
raise docutils.nodes.SkipNode
55+
56+
# Used to render the ThebeSourceNode conditionally for non-HTML builders
57+
def visit_thebe_source(self, node):
58+
if node["hide_code"]:
59+
raise docutils.nodes.SkipNode
60+
else:
61+
self.visit_container(node)
62+
63+
render_thebe_source = (
64+
visit_thebe_source,
65+
lambda self, node: self.depart_container(node),
66+
)
67+
68+
##############################################################################
69+
# Sphinx callback functions
3070
def builder_inited(app):
3171
"""
3272
2 cases
@@ -75,7 +115,9 @@ def build_finished(app, env):
75115
copy_asset(src, dst)
76116

77117

78-
def _setup(app):
118+
##############################################################################
119+
# Main setup
120+
def setup(app):
79121
"""A temporary setup function so that we can use it here and in execute.
80122
81123
This should be removed and converted into `setup` after a deprecation
@@ -110,49 +152,12 @@ def _setup(app):
110152

111153
# thebelab config, can be either a filename or a dict
112154
app.add_config_value("jupyter_sphinx_thebelab_config", None, "html")
113-
114155
app.add_config_value("jupyter_sphinx_thebelab_url", THEBELAB_URL_DEFAULT, "html")
115156

116157
# linenos config
117158
app.add_config_value("jupyter_sphinx_linenos", False, "env")
118159
app.add_config_value("jupyter_sphinx_continue_linenos", False, "env")
119160

120-
# Used for nodes that do not need to be rendered
121-
def skip(self, node):
122-
raise docutils.nodes.SkipNode
123-
124-
# Renders the children of a container
125-
render_container = (
126-
lambda self, node: self.visit_container(node),
127-
lambda self, node: self.depart_container(node),
128-
)
129-
130-
# Used to render the container and its children as HTML
131-
def visit_container_html(self, node):
132-
self.body.append(node.visit_html())
133-
self.visit_container(node)
134-
135-
def depart_container_html(self, node):
136-
self.depart_container(node)
137-
self.body.append(node.depart_html())
138-
139-
# Used to render an element node as HTML
140-
def visit_element_html(self, node):
141-
self.body.append(node.html())
142-
raise docutils.nodes.SkipNode
143-
144-
# Used to render the ThebeSourceNode conditionally for non-HTML builders
145-
def visit_thebe_source(self, node):
146-
if node["hide_code"]:
147-
raise docutils.nodes.SkipNode
148-
else:
149-
self.visit_container(node)
150-
151-
render_thebe_source = (
152-
visit_thebe_source,
153-
lambda self, node: self.depart_container(node),
154-
)
155-
156161
# JupyterKernelNode is just a doctree marker for the
157162
# ExecuteJupyterCells transform, so we don't actually render it.
158163
app.add_node(
@@ -245,12 +250,3 @@ def visit_thebe_source(self, node):
245250
app.connect("build-finished", build_finished)
246251

247252
return {"version": __version__, "parallel_read_safe": True}
248-
249-
250-
def setup(app):
251-
"""The main setup function.
252-
253-
This should be replaced with `_setup` after a deprecation cycle.
254-
"""
255-
out = _setup(app)
256-
return out

jupyter_sphinx/execute.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ def setup(app):
280280
281281
This should be removed after a deprecation cycle.
282282
"""
283+
# To avoid circular imports we'll lazily import
284+
from . import setup as jssetup
283285
js.logger.warning(
284286
(
285287
"`jupyter-sphinx` was initialized with the "
@@ -289,5 +291,5 @@ def setup(app):
289291
"version 0.3"
290292
)
291293
)
292-
out = js._setup(app)
294+
out = jssetup(app)
293295
return out

0 commit comments

Comments
 (0)