Repository: mwouts/jupytext

Issue:
.md round-tripping Broken on %%python3 magic
Just noticed this in a notebook that uses `%%python3` magic to demonstrate code execution in separate cells.

If I'm editing a markdown file in Jupyter notebook UI under Jupytext, if I have a code cell:

```
%%python3
a = 1
print(a)
```

if I save the file and then reopen it, the `%%python3` block cell magic has been stripped.

Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/mwouts/jupytext/pull/368?src=pr&el=h1) Report
> Merging [#368](https://codecov.io/gh/mwouts/jupytext/pull/368?src=pr&el=desc) into [master](https://codecov.io/gh/mwouts/jupytext/commit/4d6bef1c6cc78111ce1b3560b07965332c0c951c?src=pr&el=desc) will **decrease** coverage by `0.02%`.
> The diff coverage is `99.29%`.

[![Impacted file tree graph](https://codecov.io/gh/mwouts/jupytext/pull/368/graphs/tree.svg?width=650&token=a1pNjsiILi&height=150&src=pr)](https://codecov.io/gh/mwouts/jupytext/pull/368?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #368      +/-   ##
==========================================
- Coverage   99.05%   99.03%   -0.03%     
==========================================
  Files          74       74              
  Lines        7311     7157     -154     
==========================================
- Hits         7242     7088     -154     
  Misses         69       69
```


| [Impacted Files](https://codecov.io/gh/mwouts/jupytext/pull/368?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [tests/test\_compare.py](https://codecov.io/gh/mwouts/jupytext/pull/368/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9jb21wYXJlLnB5) | `100% <ø> (ø)` | :arrow_up: |
| [tests/test\_cli.py](https://codecov.io/gh/mwouts/jupytext/pull/368/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9jbGkucHk=) | `100% <100%> (ø)` | :arrow_up: |
| [tests/test\_read\_simple\_rmd.py](https://codecov.io/gh/mwouts/jupytext/pull/368/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9yZWFkX3NpbXBsZV9ybWQucHk=) | `100% <100%> (ø)` | :arrow_up: |
| [tests/test\_escape\_magics.py](https://codecov.io/gh/mwouts/jupytext/pull/368/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9lc2NhcGVfbWFnaWNzLnB5) | `100% <100%> (ø)` | :arrow_up: |
| [tests/test\_black.py](https://codecov.io/gh/mwouts/jupytext/pull/368/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9ibGFjay5weQ==) | `100% <100%> (ø)` | :arrow_up: |
| [tests/test\_read\_simple\_markdown.py](https://codecov.io/gh/mwouts/jupytext/pull/368/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9yZWFkX3NpbXBsZV9tYXJrZG93bi5weQ==) | `100% <100%> (ø)` | :arrow_up: |
| [tests/test\_header.py](https://codecov.io/gh/mwouts/jupytext/pull/368/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9oZWFkZXIucHk=) | `100% <100%> (ø)` | :arrow_up: |
| [tests/test\_active\_cells.py](https://codecov.io/gh/mwouts/jupytext/pull/368/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9hY3RpdmVfY2VsbHMucHk=) | `100% <100%> (ø)` | :arrow_up: |
| [tests/test\_read\_simple\_julia.py](https://codecov.io/gh/mwouts/jupytext/pull/368/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9yZWFkX3NpbXBsZV9qdWxpYS5weQ==) | `100% <100%> (ø)` | :arrow_up: |
| [tests/test\_rmd\_to\_ipynb.py](https://codecov.io/gh/mwouts/jupytext/pull/368/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9ybWRfdG9faXB5bmIucHk=) | `100% <100%> (ø)` | :arrow_up: |
| ... and [49 more](https://codecov.io/gh/mwouts/jupytext/pull/368/diff?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/mwouts/jupytext/pull/368?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/mwouts/jupytext/pull/368?src=pr&el=footer). Last update [4d6bef1...c4d7477](https://codecov.io/gh/mwouts/jupytext/pull/368?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/HISTORY.rst b/HISTORY.rst
index 9508d62..2cc996b 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -28,6 +28,7 @@ Release History
 - Fixed an inconsistent round trip (code cell with ``"cat"`` being converted to a markdown cell) in the ``py:light`` format (#339)
 - ``jupytext --test textfile.ext`` now really compares the text file to its round trip (rather than the corresponding notebook) (#339)
 - Markdown cells that contain code are now preserved in a round trip through the Markdown and R Markdown formats (#361)
+- Code cells with a ``%%python3`` cell magic are now preserved in a round trip through the Markdown format (#365)
 
 
 1.2.4 (2019-09-19)
diff --git a/jupytext/cell_reader.py b/jupytext/cell_reader.py
index 307a966..b8154ae 100644
--- a/jupytext/cell_reader.py
+++ b/jupytext/cell_reader.py
@@ -262,7 +262,7 @@ class BaseCellReader(object):
 class MarkdownCellReader(BaseCellReader):
     """Read notebook cells from Markdown documents"""
     comment = ''
-    start_code_re = re.compile(r"^```({})(.*)".format('|'.join(
+    start_code_re = re.compile(r"^```({})($|\s(.*)$)".format('|'.join(
         _JUPYTER_LANGUAGES.union({str.upper(lang) for lang in _JUPYTER_LANGUAGES})).replace('+', '\\+')))
     non_jupyter_code_re = re.compile(r"^```")
     end_code_re = re.compile(r"^```\s*$")


Test patch:
diff --git a/tests/test_read_simple_markdown.py b/tests/test_read_simple_markdown.py
index 7a074b8..8e9c38e 100644
--- a/tests/test_read_simple_markdown.py
+++ b/tests/test_read_simple_markdown.py
@@ -590,3 +590,34 @@ def test_two_markdown_cell_with_no_language_code_works(nb=new_notebook(cells=[
     text = jupytext.writes(nb, 'md')
     nb2 = jupytext.reads(text, 'md')
     compare_notebooks(nb2, nb)
+
+
+def test_notebook_with_python3_magic(no_jupytext_version_number,
+                                     nb=new_notebook(metadata={
+                                         'kernelspec': {'display_name': 'Python 3', 'language': 'python',
+                                                        'name': 'python3'}},
+                                                     cells=[new_code_cell('%%python2\na = 1\nprint a'),
+                                                            new_code_cell('%%python3\nb = 2\nprint(b)')]),
+                                     text="""---
+jupyter:
+  kernelspec:
+    display_name: Python 3
+    language: python
+    name: python3
+---
+
+```python2
+a = 1
+print a
+```
+
+```python3
+b = 2
+print(b)
+```
+"""):
+    md = jupytext.writes(nb, 'md')
+    compare(md, text)
+
+    nb2 = jupytext.reads(md, 'md')
+    compare_notebooks(nb2, nb)