Skip to content

Commit

Permalink
fix image reference clashes in rst (#89)
Browse files Browse the repository at this point in the history
fixes #88 

If an image is set without a caption (e.g. `![](image.png)`), then pandoc will assign an iterative reference identifier to it:

```
|image0|

.. |image0| image:: image.png
```

However, this iterator restarts for each markdown cell, which can lead to reference clashes. Therefore we specifically assign the identifier here, as its url

```
|main_files/example1.png|

.. |main_files/example1.png| image:: main_files/example1.png
```

* flake8 fixes
  • Loading branch information
chrisjsewell authored Jul 9, 2019
1 parent db99c84 commit 05d7055
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ipypublish/filters_pandoc/format_label_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 %)

Expand Down
14 changes: 14 additions & 0 deletions ipypublish/tests/test_convert_markdown_cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 05d7055

Please sign in to comment.