-
Notifications
You must be signed in to change notification settings - Fork 84
/
__init__.py
105 lines (86 loc) · 3.09 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
__version__ = "0.3.0"
from docutils import nodes
from jupyter_sphinx.ast import ( # noqa: F401
JupyterWidgetStateNode,
JupyterWidgetViewNode,
JupyterCell,
)
from pathlib import Path
from .parser import (
NotebookParser,
CellNode,
CellInputNode,
CellOutputNode,
CellOutputBundleNode,
)
from .transform import CellOutputsToNodes
from .nb_glue import glue # noqa: F401
from .nb_glue.domain import NbGlueDomain
from .nb_glue.transform import PasteNodesToDocutils
def static_path(app):
static_path = Path(__file__).absolute().with_name("_static")
app.config.html_static_path.append(str(static_path))
def update_togglebutton_classes(app, config):
to_add = [
".tag_hide_input div.cell_input",
".tag_hide_output div.cell_output",
".tag_hide_cell.cell",
]
for selector in to_add:
config.togglebutton_selector += f", {selector}"
def save_glue_cache(app, env):
NbGlueDomain.from_env(env).write_cache()
def setup(app):
"""Initialize Sphinx extension."""
# Sllow parsing ipynb files
app.add_source_suffix(".ipynb", "ipynb")
app.add_source_parser(NotebookParser)
app.setup_extension("sphinx_togglebutton")
# Helper functions for the registry, pulled from jupyter-sphinx
def skip(self, node):
raise nodes.SkipNode
# Used to render an element node as HTML
def visit_element_html(self, node):
self.body.append(node.html())
raise nodes.SkipNode
# Shortcut for registering our container nodes
render_container = (
lambda self, node: self.visit_container(node),
lambda self, node: self.depart_container(node),
)
# Register our container nodes, these should behave just like a regular container
for node in [CellNode, CellInputNode, CellOutputNode]:
app.add_node(
node,
override=True,
html=(render_container),
latex=(render_container),
textinfo=(render_container),
text=(render_container),
man=(render_container),
)
# Register the output bundle node.
# No translators should touch this node because we'll replace it in a post-transform
app.add_node(
CellOutputBundleNode,
override=True,
html=(skip, None),
latex=(skip, None),
textinfo=(skip, None),
text=(skip, None),
man=(skip, None),
)
# Register our post-transform which will convert output bundles to nodes
app.add_post_transform(PasteNodesToDocutils)
app.add_post_transform(CellOutputsToNodes)
app.connect("builder-inited", static_path)
app.connect("config-inited", update_togglebutton_classes)
app.connect("env-updated", save_glue_cache)
app.add_css_file("mystnb.css")
# We use `execute` here instead of `jupyter-execute`
app.add_directive("execute", JupyterCell)
app.setup_extension("jupyter_sphinx")
app.add_domain(NbGlueDomain)
# TODO need to deal with key clashes in NbGlueDomain.merge_domaindata
# before this is parallel_read_safe
return {"version": __version__, "parallel_read_safe": False}