Skip to content

Commit

Permalink
Support magics in markdown cells in light format #99
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Oct 14, 2018
1 parent 7ede7c6 commit 9210760
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
4 changes: 2 additions & 2 deletions jupytext/cell_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def options_to_metadata(self, options):
raise NotImplementedError()

def uncomment_code_and_magics(self, lines):
if self.cell_type == 'code':
if self.cell_type == 'code' or self.comment != "#'":
if self.comment_magics:
if is_active(self.ext, self.metadata):
uncomment_magic(lines, self.language or self.default_language)
Expand Down Expand Up @@ -443,7 +443,7 @@ def find_cell_content(self, lines):

if self.cell_type != 'code':
source = uncomment(source, self.comment)
elif self.comment_magics:
if self.comment_magics:
source = self.uncomment_code_and_magics(source)

self.content = source
Expand Down
7 changes: 5 additions & 2 deletions jupytext/cell_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def cell_to_text(self):

def markdown_to_text(self, source):
"""Escape the given source, for a markdown cell"""
if self.comment and self.comment != "#'":
source = copy(source)
comment_magic(source, self.language, self.comment_magics)

return comment_lines(source, self.comment)

def code_to_text(self):
Expand Down Expand Up @@ -311,8 +315,7 @@ def cell_to_text(self):
source = copy(self.source)
if not source:
source == ['']
comment_magic(source, self.language, self.comment_magics)
return source
return comment_magic(source, self.language, self.comment_magics)

if 'cell_marker' in self.metadata:
cell_marker = self.metadata.pop('cell_marker')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ vectorᵢ
;; It has breakpoints (click in left margin). You must press Stop to exit the debugger.

;; ```scheme
;; %%debug
;; ;; %%debug
;;
;; (begin
;; (define x 1)
Expand Down
1 change: 0 additions & 1 deletion tests/test_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ def test_ipynb_to_R(nb_file):
assert_conversion_same_as_mirror(nb_file, '.R', 'ipynb_to_script')


@pytest.mark.skip(reason='Magics in markdown cells break this test')
@pytest.mark.parametrize('nb_file', list_notebooks('ipynb_scheme'))
def test_ipynb_to_scheme(nb_file):
assert_conversion_same_as_mirror(nb_file, '.ss', 'ipynb_to_script')
Expand Down
9 changes: 9 additions & 0 deletions tests/test_read_simple_python.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-

from nbformat.v4.nbbase import new_markdown_cell, new_notebook
from testfixtures import compare
import jupytext

Expand Down Expand Up @@ -495,3 +496,11 @@ def h(x):
script2 = jupytext.writes(notebook, ext='.py')

compare(script, script2)


def test_round_trip_markdown_cell_with_magic():
notebook = new_notebook(cells=[new_markdown_cell('IPython has magic commands like\n%quickref')],
metadata={'jupytext': {'main_language': 'python'}})
text = jupytext.writes(notebook, ext='.py')
notebook2 = jupytext.reads(text, ext='.py')
compare(notebook, notebook2)

0 comments on commit 9210760

Please sign in to comment.