Repository: mwouts/jupytext

Issue:
empty line in notebook metadata section prevent it from openning
jupytext seems remove the indent space in empty lines when syncing the metadata from ipynb to python file. And empty line in YAML, while legal, prevent jupytext from working with error message:

```
Unreadable Notebook: c.py ScannerError('while scanning a quoted scalar', <yaml.error.Mark object at 0x7f9f90243190>, 'found unexpected end of stream', <yaml.error.Mark object at 0x7f9f90535be0>)
```

reproduce step:

1. create a new python file with following content:

```python
# ---
# jupyter:
#   jupytext:
#     cell_markers: '"""
#       
#       ,
#       
#       """'
#     formats: .notebook//ipynb,py:percent
#     notebook_metadata_filter: -all,jupytext
#     text_representation:
#       extension: .py
#       format_name: percent
# ---
```
it configs the jupytext to use percent format and """\n \n""" as the markdown seperator.
note that there are **indent space in the two empty lines**

2. open the python file in jupyter notebook mode, it will open it successfully
3. press the save button to save the file, in order to trigger jupytext to save the metadata back to python file
4. you will notice that the **indent space were removed in the two empty lines by jupytext**
5. refresh the notebook, or reopen it again, there's an error
```
Unreadable Notebook: c.py ScannerError('while scanning a quoted scalar', <yaml.error.Mark object at 0x7f9f90243190>, 'found unexpected end of stream', <yaml.error.Mark object at 0x7f9f90535be0>)
```

Comments:

Patch:
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 9c10543..42f41c6 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -37,14 +37,13 @@ A pull request for which you do not need to contact us in advance is the additio
 Most of Jupytext's code is written in Python. To develop the Python part of Jupytext, you should clone Jupytext, then create a dedicated Python env:
 ```
 cd jupytext
-conda create -n jupytext-dev jupyterlab pyyaml
+conda env create --file environment.yml
 conda activate jupytext-dev
-pip install -r requirements-dev.txt
+pip install -e .
 ```
 
-Tests are executed with `pytest`. Install `pytest-xdist` to allow running the tests in parallel:
+Tests are executed with `pytest`. You can run them in parallel with for instance
 ```
-pip install pytest-xdist
 pytest -n 5
 ``` 
 
diff --git a/environment.yml b/environment.yml
new file mode 100644
index 0000000..5d3cff2
--- /dev/null
+++ b/environment.yml
@@ -0,0 +1,21 @@
+name: jupytext-dev
+channels:
+  - default
+  - conda-forge
+dependencies:
+  - python==3.7
+  - jupyter
+  - pyyaml
+  - nbconvert
+  - jupyter_contrib_nbextensions
+  - pytest
+  - pytest-xdist
+  - pylint
+  - flake8
+  - black
+  - autopep8
+  - sphinx-gallery
+  - nodejs
+  - pip
+  - setuptools
+  - pandoc
diff --git a/jupytext/header.py b/jupytext/header.py
index b477b55..c23ff72 100644
--- a/jupytext/header.py
+++ b/jupytext/header.py
@@ -173,7 +173,7 @@ def header_to_metadata_and_cell(lines, header_prefix, ext=None):
 
         if _JUPYTER_RE.match(line):
             injupyter = True
-        elif not _LEFTSPACE_RE.match(line):
+        elif line and not _LEFTSPACE_RE.match(line):
             injupyter = False
 
         if injupyter:


Test patch:
diff --git a/tests/test_header.py b/tests/test_header.py
index 762456d..9f9c86d 100644
--- a/tests/test_header.py
+++ b/tests/test_header.py
@@ -99,3 +99,24 @@ def test_notebook_from_plain_script_has_metadata_filter(script="""print('Hello w
     script2 = jupytext.writes(nb, '.py')
 
     compare(script2, script)
+
+
+def test_multiline_metadata(
+        no_jupytext_version_number,
+        notebook=new_notebook(metadata={'multiline': """A multiline string
+
+with a blank line""", 'jupytext': {'notebook_metadata_filter': 'all'}}),
+        markdown="""---
+jupyter:
+  jupytext:
+    notebook_metadata_filter: all
+  multiline: 'A multiline string
+
+
+    with a blank line'
+---
+"""):
+    actual = jupytext.writes(notebook, '.md')
+    compare(actual, markdown)
+    nb2 = jupytext.reads(markdown, '.md')
+    compare(nb2, notebook)