diff --git a/ipypublish/__init__.py b/ipypublish/__init__.py index 505899f..8d1eab0 100644 --- a/ipypublish/__init__.py +++ b/ipypublish/__init__.py @@ -1,3 +1,3 @@ from ipypublish.scripts import nb_setup # noqa: F401 -__version__ = '0.10.3' +__version__ = '0.10.4' diff --git a/ipypublish/filters_pandoc/format_label_elements.py b/ipypublish/filters_pandoc/format_label_elements.py index 70576a9..ab15431 100644 --- a/ipypublish/filters_pandoc/format_label_elements.py +++ b/ipypublish/filters_pandoc/format_label_elements.py @@ -158,6 +158,17 @@ def format_image(image, doc): return pf.RawInline(latex, format="tex") elif doc.format in ("rst",): + if not image.content.list: + # If the container is empty, then pandoc will assign an iterative + # reference identifier to it (image0, image1). + # However, this iterator restarts for each markdown cell, + # which can lead to reference clashes. + # Therefore we specifically assign the identifier here, as its url + # TODO does this identifier need to be sanitized? + # (it works fine in the tests) + identifier = image.url + image.content = pf.ListContainer(pf.Str(str(identifier))) + return image # TODO formatting and span identifier (convert width/height to %) diff --git a/ipypublish/tests/test_convert_markdown_cells.py b/ipypublish/tests/test_convert_markdown_cells.py index 1652080..700b9ce 100644 --- a/ipypublish/tests/test_convert_markdown_cells.py +++ b/ipypublish/tests/test_convert_markdown_cells.py @@ -26,3 +26,17 @@ def test_sphinx_rst(ipynb_app): raise AssertionError("pandoc version must be >= 2.6") ipynb_app.assert_converted_equals_expected( 'sphinx_ipypublish_main.pandoc.2-6') + + +@pytest.mark.ipynb('nb_with_mkdown_images') # out_to_temp=False +def test_sphinx_rst_with_mkdown_images(ipynb_app): + """ test a notebook with multiple images """ + ipynb_app.run({ + "conversion": "sphinx_ipypublish_main.run", + "log_to_file": True, + "default_pporder_kwargs": {"dump_files": True}}) + ipynb_app.assert_converted_exists() + ipynb_app.assert_converted_equals_expected( + 'sphinx_ipypublish_main') + assert ipynb_app.converted_path.joinpath( + "main_files/example.jpg").is_file() diff --git a/ipypublish/tests/test_files/nb_with_mkdown_images/expected/sphinx_ipypublish_main.rst b/ipypublish/tests/test_files/nb_with_mkdown_images/expected/sphinx_ipypublish_main.rst new file mode 100644 index 0000000..7b05318 --- /dev/null +++ b/ipypublish/tests/test_files/nb_with_mkdown_images/expected/sphinx_ipypublish_main.rst @@ -0,0 +1,18 @@ + +.. An html document created by ipypublish + outline: ipypublish.templates.outline_schemas/rst_outline.rst.j2 + with segments: + - nbsphinx-ipypublish-content: ipypublish sphinx content + +Notebook to test markdown cells containing multiple images + +|main_files/example1.png| + +.. |main_files/example1.png| image:: main_files/example1.png + +|main_files/example.jpg| + +|main_files/example2.jpg| + +.. |main_files/example.jpg| image:: main_files/example.jpg +.. |main_files/example2.jpg| image:: main_files/example2.jpg diff --git a/ipypublish/tests/test_files/nb_with_mkdown_images/source/main.ipynb b/ipypublish/tests/test_files/nb_with_mkdown_images/source/main.ipynb new file mode 100644 index 0000000..49b3e75 --- /dev/null +++ b/ipypublish/tests/test_files/nb_with_mkdown_images/source/main.ipynb @@ -0,0 +1,75 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Notebook to test markdown cells containing multiple images" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![](media/example1.png)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![](media/example.jpg)\n", + "\n", + "![](media/example2.jpg)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "celltoolbar": "Edit Metadata", + "hide_input": false, + "ipub": { + "pandoc": { + "use_numref": true + } + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/ipypublish/tests/test_files/nb_with_mkdown_images/source/media/example.jpg b/ipypublish/tests/test_files/nb_with_mkdown_images/source/media/example.jpg new file mode 100644 index 0000000..1c1e83e Binary files /dev/null and b/ipypublish/tests/test_files/nb_with_mkdown_images/source/media/example.jpg differ diff --git a/ipypublish/tests/test_files/nb_with_mkdown_images/source/media/example1.png b/ipypublish/tests/test_files/nb_with_mkdown_images/source/media/example1.png new file mode 100644 index 0000000..a52d9d4 Binary files /dev/null and b/ipypublish/tests/test_files/nb_with_mkdown_images/source/media/example1.png differ diff --git a/ipypublish/tests/test_files/nb_with_mkdown_images/source/media/example2.jpg b/ipypublish/tests/test_files/nb_with_mkdown_images/source/media/example2.jpg new file mode 100644 index 0000000..59cd435 Binary files /dev/null and b/ipypublish/tests/test_files/nb_with_mkdown_images/source/media/example2.jpg differ