Skip to content

Commit f20eab0

Browse files
authored
Merge pull request #130 from choldgraf/inline_nodes
adding inline functionality to nodes function
2 parents 6eb309d + 4558fb8 commit f20eab0

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

jupyter_sphinx/ast.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ def html(self):
229229
)
230230

231231

232-
def cell_output_to_nodes(outputs, data_priority, write_stderr, out_dir, thebe_config):
232+
def cell_output_to_nodes(outputs, data_priority, write_stderr, out_dir,
233+
thebe_config, inline=False):
233234
"""Convert a jupyter cell with outputs and filenames to doctree nodes.
234235
235236
Parameters
@@ -244,12 +245,22 @@ def cell_output_to_nodes(outputs, data_priority, write_stderr, out_dir, thebe_co
244245
to the source folder prefixed with ``/``.
245246
thebe_config: dict
246247
Thebelab configuration object or None
248+
inline: False
249+
Whether the nodes will be placed in-line with the text.
247250
248251
Returns
249252
-------
250253
to_add : list of docutils nodes
251254
Each output, converted into a docutils node.
252255
"""
256+
# If we're in `inline` mode, ensure that we don't add block-level nodes
257+
if inline:
258+
literal_node = docutils.nodes.literal
259+
math_node = docutils.nodes.math
260+
else:
261+
literal_node = docutils.nodes.literal_block
262+
math_node = math_block
263+
253264
to_add = []
254265
for output in outputs:
255266
output_type = output["output_type"]
@@ -267,19 +278,22 @@ def cell_output_to_nodes(outputs, data_priority, write_stderr, out_dir, thebe_co
267278
# Not setting "rawsource" disables Pygment hightlighting, which
268279
# would otherwise add a <div class="highlight">.
269280

270-
container = docutils.nodes.container(classes=["stderr"])
271-
container.append(
272-
docutils.nodes.literal_block(
273-
text=output["text"],
274-
rawsource="", # disables Pygment highlighting
275-
language="none",
276-
classes=["stderr"],
277-
)
281+
literal = literal_node(
282+
text=output["text"],
283+
rawsource="", # disables Pygment highlighting
284+
language="none",
285+
classes=["stderr"],
278286
)
279-
to_add.append(container)
287+
if inline:
288+
# In this case, we don't wrap the text in containers
289+
to_add.append(literal)
290+
else:
291+
container = docutils.nodes.container(classes=["stderr"])
292+
container.append(literal)
293+
to_add.append(container)
280294
else:
281295
to_add.append(
282-
docutils.nodes.literal_block(
296+
literal_node(
283297
text=output["text"],
284298
rawsource=output["text"],
285299
language="none",
@@ -290,7 +304,7 @@ def cell_output_to_nodes(outputs, data_priority, write_stderr, out_dir, thebe_co
290304
traceback = "\n".join(output["traceback"])
291305
text = nbconvert.filters.strip_ansi(traceback)
292306
to_add.append(
293-
docutils.nodes.literal_block(
307+
literal_node(
294308
text=text,
295309
rawsource=text,
296310
language="ipythontb",
@@ -325,7 +339,7 @@ def cell_output_to_nodes(outputs, data_priority, write_stderr, out_dir, thebe_co
325339
)
326340
elif mime_type == "text/latex":
327341
to_add.append(
328-
math_block(
342+
math_node(
329343
text=strip_latex_delimiters(data),
330344
nowrap=False,
331345
number=None,
@@ -334,7 +348,7 @@ def cell_output_to_nodes(outputs, data_priority, write_stderr, out_dir, thebe_co
334348
)
335349
elif mime_type == "text/plain":
336350
to_add.append(
337-
docutils.nodes.literal_block(
351+
literal_node(
338352
text=data,
339353
rawsource=data,
340354
language="none",

tests/test_execute.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from sphinx.addnodes import download_reference
1010
from sphinx.testing.util import assert_node, SphinxTestApp, path
1111
from sphinx.errors import ExtensionError
12-
from docutils.nodes import literal, raw
12+
from docutils.nodes import raw, literal, literal_block, container
1313
from nbformat import from_dict
1414

1515
import pytest
@@ -551,7 +551,7 @@ def test_latex(doctree):
551551
assert celloutput.children[0].astext() == r"\int"
552552

553553

554-
def test_image_mimetype_uri(doctree):
554+
def test_cell_output_to_nodes(doctree):
555555
# tests the image uri paths on conversion to docutils image nodes
556556
priority = ['image/png', 'image/jpeg', 'text/latex', 'text/plain']
557557
output_dir = '/_build/jupyter_execute'
@@ -570,6 +570,19 @@ def test_image_mimetype_uri(doctree):
570570
output_node = cell_output_to_nodes(cell["outputs"], priority, True, output_dir, None)
571571
assert output_node[0].attributes['uri'] == img_locs[index]
572572

573+
# Testing inline functionality
574+
outputs = [
575+
{"name": "stdout", "output_type": "stream", "text": ["hi\n"]},
576+
{"name": "stderr", "output_type": "stream", "text": ["hi\n"]},
577+
]
578+
output_nodes = cell_output_to_nodes(outputs, priority, True, output_dir, None)
579+
for output, kind in zip(output_nodes, [literal_block, container]):
580+
assert isinstance(output, kind)
581+
582+
output_nodes = cell_output_to_nodes(outputs, priority, True, output_dir, None, inline=True)
583+
for output, kind in zip(output_nodes, [literal, literal]):
584+
assert isinstance(output, kind)
585+
573586

574587
@pytest.mark.parametrize('text,reftarget,caption', (
575588
('nb_name', '/../jupyter_execute/path/to/nb_name.ipynb', 'nb_name.ipynb'),
@@ -596,3 +609,4 @@ def test_download_role(text, reftarget, caption, tmp_path):
596609
assert_node(ret[0], [download_reference], reftarget=reftarget)
597610
assert_node(ret[0][0], [literal, caption])
598611
assert msg == []
612+

0 commit comments

Comments
 (0)