From 9210760527c7b73f3320b871c77202a0b779a8cb Mon Sep 17 00:00:00 2001 From: Marc Wouts Date: Sun, 14 Oct 2018 15:11:34 +0200 Subject: [PATCH] Support magics in markdown cells in light format #99 --- jupytext/cell_reader.py | 4 ++-- jupytext/cell_to_text.py | 7 +++++-- .../Reference Guide for Calysto Scheme.ss | 2 +- tests/test_mirror.py | 1 - tests/test_read_simple_python.py | 9 +++++++++ 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/jupytext/cell_reader.py b/jupytext/cell_reader.py index 901dc32d8..448526091 100644 --- a/jupytext/cell_reader.py +++ b/jupytext/cell_reader.py @@ -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) @@ -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 diff --git a/jupytext/cell_to_text.py b/jupytext/cell_to_text.py index 8766e93b9..229700bd7 100644 --- a/jupytext/cell_to_text.py +++ b/jupytext/cell_to_text.py @@ -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): @@ -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') diff --git a/tests/notebooks/mirror/ipynb_to_script/Reference Guide for Calysto Scheme.ss b/tests/notebooks/mirror/ipynb_to_script/Reference Guide for Calysto Scheme.ss index 57461d608..fcaee317d 100644 --- a/tests/notebooks/mirror/ipynb_to_script/Reference Guide for Calysto Scheme.ss +++ b/tests/notebooks/mirror/ipynb_to_script/Reference Guide for Calysto Scheme.ss @@ -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) diff --git a/tests/test_mirror.py b/tests/test_mirror.py index 8efa9bd24..1b60e5d4c 100644 --- a/tests/test_mirror.py +++ b/tests/test_mirror.py @@ -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') diff --git a/tests/test_read_simple_python.py b/tests/test_read_simple_python.py index b05318df1..f61614225 100644 --- a/tests/test_read_simple_python.py +++ b/tests/test_read_simple_python.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +from nbformat.v4.nbbase import new_markdown_cell, new_notebook from testfixtures import compare import jupytext @@ -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)