Skip to content

Commit 9aa16a1

Browse files
committed
added classes to stderr output
1 parent b66ee7e commit 9aa16a1

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

jupyter_sphinx/execute.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -494,13 +494,48 @@ def cell_output_to_nodes(cell, data_priority, write_stderr, dir, thebe_config):
494494
if (
495495
output_type == 'stream'
496496
):
497-
if not write_stderr and output["name"] == "stderr":
498-
continue
499-
to_add.append(docutils.nodes.literal_block(
500-
text=output['text'],
501-
rawsource=output['text'],
502-
language='none',
503-
))
497+
if output["name"] == "stderr":
498+
if not write_stderr:
499+
continue
500+
else:
501+
# Produce a container with an unhighlighted literal block for
502+
# `stderr` messages.
503+
#
504+
# Adds a "stderr" class that can be customized by the user for both
505+
# the container and the literal_block.
506+
#
507+
# Also adds "error" as a base class, which is fairly common
508+
# class in Sphinx themes. It should result in differenciation
509+
# from stdout in most standard themes.
510+
#
511+
# Not setting "rawsource" disables Pygment hightlighting, which
512+
# would otherwise add a <div class="highlight"> to the container
513+
# that would hold the literal_block (<pre>).
514+
515+
container = docutils.nodes.container(classes=["error", "stderr"])
516+
container.append(docutils.nodes.literal_block(
517+
text=output['text'],
518+
rawsource='', # disables Pygment highlighting
519+
language='none',
520+
classes=["error", "stderr"]
521+
))
522+
to_add.append(container)
523+
524+
# Alternative, without container
525+
526+
# to_add.append(docutils.nodes.literal_block(
527+
# text=output['text'],
528+
# rawsource='', # disables Pygment highlighting
529+
# language='none',
530+
# classes=["error", "stderr"]
531+
# ))
532+
533+
else:
534+
to_add.append(docutils.nodes.literal_block(
535+
text=output['text'],
536+
rawsource=output['text'],
537+
language='none',
538+
))
504539
elif (
505540
output_type == 'error'
506541
):

tests/test_execute.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ def test_stderr(doctree):
267267
tree = doctree(source)
268268
cell, = tree.traverse(JupyterCellNode)
269269
assert len(cell.children) == 2
270-
assert cell.children[1].rawsource.strip() == "hello world"
270+
assert 'stderr' in cell.children[1].attributes['classes']
271+
assert 'error' in cell.children[1].attributes['classes']
272+
assert cell.children[1].astext().strip() == "hello world"
271273

272274

273275
thebe_config = "jupyter_sphinx_thebelab_config = {\"dummy\": True}"

0 commit comments

Comments
 (0)