Skip to content

Commit

Permalink
Unescape isolated magics #29
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Aug 15, 2018
1 parent ced943b commit 5272c50
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
1 change: 0 additions & 1 deletion nbrmd/cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')):
Expand Down
3 changes: 3 additions & 0 deletions nbrmd/nbrmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
25 changes: 25 additions & 0 deletions tests/test_read_simple_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5272c50

Please sign in to comment.