Skip to content

Commit 603e49c

Browse files
authored
Merge pull request #114 from AakashGfude/image-mimetype
Checking if the image path is inside a subdir of dir
2 parents 3407de5 + 648eac2 commit 603e49c

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

jupyter_sphinx/ast.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,14 @@ def cell_output_to_nodes(cell, data_priority, write_stderr, dir, thebe_config):
274274
# directory, so make a relative path, which Sphinx treats
275275
# as being relative to the current working directory.
276276
filename = os.path.basename(output.metadata["filenames"][mime_type])
277+
278+
# checks if file dir path is inside a subdir of dir
279+
filedir = os.path.dirname(output.metadata["filenames"][mime_type])
280+
subpaths = filedir.split(dir)
281+
if subpaths and len(subpaths) > 1:
282+
subpath = subpaths[1]
283+
dir += subpath
284+
277285
uri = os.path.join(dir, filename)
278286
to_add.append(docutils.nodes.image(uri=uri))
279287
elif mime_type == "text/html":

tests/test_execute.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
from sphinx.testing.util import SphinxTestApp, path
88
from sphinx.errors import ExtensionError
99
from docutils.nodes import raw
10+
from nbformat import from_dict
1011

1112
import pytest
1213

1314
from jupyter_sphinx.ast import (
1415
JupyterCellNode,
1516
JupyterWidgetViewNode,
1617
JupyterWidgetStateNode,
18+
cell_output_to_nodes,
1719
)
1820
from jupyter_sphinx.thebelab import ThebeSourceNode, ThebeOutputNode, ThebeButtonNode
1921

@@ -512,3 +514,23 @@ def test_latex(doctree):
512514
tree = doctree(source.format(start, end))
513515
(cell,) = tree.traverse(JupyterCellNode)
514516
assert cell.children[1].astext() == r"\int"
517+
518+
519+
def test_image_mimetype_uri(doctree):
520+
# tests the image uri paths on conversion to docutils image nodes
521+
priority = ['image/png', 'image/jpeg', 'text/latex', 'text/plain']
522+
output_dir = '/_build/jupyter_execute'
523+
img_locs = ['/_build/jupyter_execute/docs/image_1.png','/_build/jupyter_execute/image_2.png']
524+
525+
cells = [
526+
{'outputs':
527+
[{'data': {'image/png': 'Vxb6L1wAAAABJRU5ErkJggg==\n', 'text/plain': '<Figure size 432x288 with 1 Axes>'}, 'metadata': {'filenames': {'image/png': img_locs[0]}}, 'output_type': 'display_data'}]
528+
},
529+
{'outputs':
530+
[{'data': {'image/png': 'iVBOJggg==\n', 'text/plain': '<Figure size 432x288 with 1 Axes>'}, 'metadata': {'filenames': {'image/png': img_locs[1]}}, 'output_type': 'display_data'}]
531+
}]
532+
533+
for index, cell in enumerate(cells):
534+
cell = from_dict(cell)
535+
output_node = cell_output_to_nodes(cell, priority, True, output_dir, None)
536+
assert output_node[0].attributes['uri'] == img_locs[index]

0 commit comments

Comments
 (0)