diff --git a/nbrmd/cells.py b/nbrmd/cells.py index 029dcb080..9bd01fab0 100644 --- a/nbrmd/cells.py +++ b/nbrmd/cells.py @@ -246,7 +246,6 @@ def no_code_before_next_blank_line(lines): def code_or_raw_cell(source, metadata): - source = unescape_magic(source) source = '\n'.join(source) if 'ipynb' not in re.split('\\.|,', metadata.get('active', 'ipynb')): diff --git a/nbrmd/nbrmd.py b/nbrmd/nbrmd.py index 50caac627..abc33650e 100644 --- a/nbrmd/nbrmd.py +++ b/nbrmd/nbrmd.py @@ -25,6 +25,7 @@ from .languages import get_default_language, find_main_language from .cells import start_code_rmd, start_code_rpy, cell_to_text, text_to_cell from .cells import markdown_to_cell_rmd, markdown_to_cell, code_to_cell +from .cells import unescape_magic # ----------------------------------------------------------------------------- # Code @@ -91,6 +92,8 @@ def to_notebook(self, text, **kwargs): if pos > 0: lines = lines[pos:] + lines = unescape_magic(lines) + while lines: cell, pos = self.text_to_cell(lines) if cell is None: diff --git a/tests/test_read_simple_python.py b/tests/test_read_simple_python.py index a9223aad2..32749c880 100644 --- a/tests/test_read_simple_python.py +++ b/tests/test_read_simple_python.py @@ -147,6 +147,31 @@ def test_read_cell_with_magic(pynb="""# --- compare(pynb, pynb2) +def test_isolated_cell_with_magic(pynb="""# --- +# title: cell with isolated jupyter magic +# --- + +# %matplotlib inline + + +1 + 1 +"""): + nb = nbrmd.reads(pynb, ext='.py') + + assert len(nb.cells) == 3 + assert nb.cells[0].cell_type == 'raw' + assert nb.cells[0].source == '---\ntitle: cell with isolated jupyter ' \ + 'magic\n---' + assert nb.cells[1].cell_type == 'code' + assert nb.cells[1].source == '%matplotlib inline' + + assert nb.cells[2].cell_type == 'code' + assert nb.cells[2].source == '1 + 1' + + pynb2 = nbrmd.writes(nb, ext='.py') + compare(pynb, pynb2) + + def test_read_multiline_comment(pynb="""'''This is a multiline comment with "quotes", 'single quotes' # and comments