Skip to content

Commit 19d2e40

Browse files
authored
Merge pull request #49 from basnijholt/javascript_support
add javascript support
2 parents 449e7da + 6478aa6 commit 19d2e40

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

jupyter_sphinx/execute.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,6 @@ def cell_output_to_nodes(cell, data_priority, dir):
405405
)
406406
except StopIteration:
407407
continue
408-
409408
data = output['data'][mime_type]
410409
if mime_type.startswith('image'):
411410
# Sphinx treats absolute paths as being rooted at the source
@@ -432,6 +431,12 @@ def cell_output_to_nodes(cell, data_priority, dir):
432431
text=data,
433432
rawsource=data,
434433
))
434+
elif mime_type == 'application/javascript':
435+
to_add.append(docutils.nodes.raw(
436+
text='<script type="{mime_type}">{data}</script>'
437+
.format(mime_type=mime_type, data=data),
438+
format='html',
439+
))
435440
elif mime_type == WIDGET_VIEW_MIMETYPE:
436441
to_add.append(JupyterWidgetViewNode(data))
437442

@@ -558,6 +563,7 @@ def setup(app):
558563
'jupyter_execute_data_priority',
559564
[
560565
WIDGET_VIEW_MIMETYPE,
566+
'application/javascript',
561567
'text/html',
562568
'image/svg+xml',
563569
'image/png',

tests/test_execute.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from sphinx.testing.util import SphinxTestApp, path
88
from sphinx.errors import ExtensionError
9+
from docutils.nodes import raw
910

1011
import pytest
1112

@@ -177,3 +178,16 @@ def test_widgets(doctree):
177178
tree = doctree(source)
178179
assert len(list(tree.traverse(JupyterWidgetViewNode))) == 1
179180
assert len(list(tree.traverse(JupyterWidgetStateNode))) == 1
181+
182+
183+
def test_javascript(doctree):
184+
source = '''
185+
.. jupyter-execute::
186+
187+
from IPython.display import display_javascript, Javascript
188+
Javascript('window.alert("Hello world!")')
189+
'''
190+
tree = doctree(source)
191+
node, = list(tree.traverse(raw))
192+
text, = node.children
193+
assert 'world' in text

0 commit comments

Comments
 (0)