diff --git a/jupytext/cell_reader.py b/jupytext/cell_reader.py index 06fdc0b2c..b97fa17cb 100644 --- a/jupytext/cell_reader.py +++ b/jupytext/cell_reader.py @@ -171,6 +171,8 @@ def find_code_cell_end(self, lines): if self.start_code_re.match(line) or (self.markdown_prefix and line.startswith(self.markdown_prefix)): if i > 1 and _BLANK_LINE.match(lines[i - 1]): + if i > 2 and _BLANK_LINE.match(lines[i - 2]): + return i - 2, i, False return i - 1, i, False return i, i, False diff --git a/tests/notebooks/ipynb_py/Notebook with function and cell metadata 164.ipynb b/tests/notebooks/ipynb_py/Notebook with function and cell metadata 164.ipynb new file mode 100644 index 000000000..48bd4278c --- /dev/null +++ b/tests/notebooks/ipynb_py/Notebook with function and cell metadata 164.ipynb @@ -0,0 +1,136 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1 + 1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A markdown cell\n", + "And below, the cell for function f has non trivial cell metadata. And the next cell as well." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "attributes": { + "classes": [], + "id": "", + "n": "10" + } + }, + "outputs": [], + "source": [ + "def f(x):\n", + " return x" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "attributes": { + "classes": [], + "id": "", + "n": "10" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f(5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "More text" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2 + 2" + ] + } + ], + "metadata": { + "celltoolbar": "Edit Metadata", + "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.6" + }, + "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/tests/notebooks/mirror/ipynb_to_Rmd/Notebook with function and cell metadata 164.Rmd b/tests/notebooks/mirror/ipynb_to_Rmd/Notebook with function and cell metadata 164.Rmd new file mode 100644 index 000000000..c68e4ab65 --- /dev/null +++ b/tests/notebooks/mirror/ipynb_to_Rmd/Notebook with function and cell metadata 164.Rmd @@ -0,0 +1,29 @@ +--- +jupyter: + kernelspec: + display_name: Python 3 + language: python + name: python3 +--- + +```{python} +1 + 1 +``` + +A markdown cell +And below, the cell for function f has non trivial cell metadata. And the next cell as well. + +```{python attributes={'classes': [], 'id': '', 'n': '10'}} +def f(x): + return x +``` + +```{python attributes={'classes': [], 'id': '', 'n': '10'}} +f(5) +``` + +More text + +```{python} +2 + 2 +``` diff --git a/tests/notebooks/mirror/ipynb_to_md/Notebook with function and cell metadata 164.md b/tests/notebooks/mirror/ipynb_to_md/Notebook with function and cell metadata 164.md new file mode 100644 index 000000000..ae270a4c6 --- /dev/null +++ b/tests/notebooks/mirror/ipynb_to_md/Notebook with function and cell metadata 164.md @@ -0,0 +1,29 @@ +--- +jupyter: + kernelspec: + display_name: Python 3 + language: python + name: python3 +--- + +```python +1 + 1 +``` + +A markdown cell +And below, the cell for function f has non trivial cell metadata. And the next cell as well. + +```python +def f(x): + return x +``` + +```python +f(5) +``` + +More text + +```python +2 + 2 +``` diff --git a/tests/notebooks/mirror/ipynb_to_percent/Notebook with function and cell metadata 164.py b/tests/notebooks/mirror/ipynb_to_percent/Notebook with function and cell metadata 164.py new file mode 100644 index 000000000..bba621507 --- /dev/null +++ b/tests/notebooks/mirror/ipynb_to_percent/Notebook with function and cell metadata 164.py @@ -0,0 +1,29 @@ +# --- +# jupyter: +# kernelspec: +# display_name: Python 3 +# language: python +# name: python3 +# --- + +# %% +1 + 1 + + +# %% [markdown] +# A markdown cell +# And below, the cell for function f has non trivial cell metadata. And the next cell as well. + +# %% {"attributes": {"classes": [], "id": "", "n": "10"}} +def f(x): + return x + + +# %% {"attributes": {"classes": [], "id": "", "n": "10"}} +f(5) + +# %% [markdown] +# More text + +# %% +2 + 2 diff --git a/tests/notebooks/mirror/ipynb_to_script/Notebook with function and cell metadata 164.py b/tests/notebooks/mirror/ipynb_to_script/Notebook with function and cell metadata 164.py new file mode 100644 index 000000000..c144b03ef --- /dev/null +++ b/tests/notebooks/mirror/ipynb_to_script/Notebook with function and cell metadata 164.py @@ -0,0 +1,26 @@ +# --- +# jupyter: +# kernelspec: +# display_name: Python 3 +# language: python +# name: python3 +# --- + +1 + 1 + + +# A markdown cell +# And below, the cell for function f has non trivial cell metadata. And the next cell as well. + +# + {"attributes": {"classes": [], "id": "", "n": "10"}} +def f(x): + return x + + +# + {"attributes": {"classes": [], "id": "", "n": "10"}} +f(5) +# - + +# More text + +2 + 2 diff --git a/tests/test_mirror.py b/tests/test_mirror.py index deca09c03..045ec514a 100644 --- a/tests/test_mirror.py +++ b/tests/test_mirror.py @@ -189,7 +189,7 @@ def test_spin_to_ipynb(nb_file): assert_conversion_same_as_mirror(nb_file, 'ipynb:spin', 'script_to_ipynb') -@pytest.mark.parametrize('nb_file', list_notebooks('ipynb_py', skip='(raw|hash|frozen|magic)')) +@pytest.mark.parametrize('nb_file', list_notebooks('ipynb_py', skip='(raw|hash|frozen|magic|164)')) def test_ipynb_to_python_sphinx(nb_file): assert_conversion_same_as_mirror(nb_file, 'py:sphinx', 'ipynb_to_sphinx')