diff --git a/jupytext/cell_reader.py b/jupytext/cell_reader.py index eee96d51e..ef786db70 100644 --- a/jupytext/cell_reader.py +++ b/jupytext/cell_reader.py @@ -270,6 +270,11 @@ def metadata_and_language_from_option_line(self, line): self.metadata['region_name'] = region_name elif self.start_code_re.match(line): self.language, self.metadata = self.options_to_metadata(self.start_code_re.findall(line)[0]) + # Cells with a .noeval attribute are markdown cells #347 + if self.metadata.get('.noeval', '') is None: + self.cell_type = 'markdown' + self.metadata = {} + self.language = None def options_to_metadata(self, options): if isinstance(options, tuple): @@ -306,12 +311,24 @@ def find_cell_end(self, lines): if in_indented_code_block or in_explicit_code_block: continue - if self.start_code_re.match(line) or self.start_region_re.match(line): + if self.start_region_re.match(line): + if i > 1 and prev_blank: + return i - 1, i, False + return i, i, False + + if self.start_code_re.match(line): + # Cells with a .noeval attribute are markdown cells #347 + _, metadata = self.options_to_metadata(self.start_code_re.findall(line)[0]) + if metadata.get('.noeval', '') is None: + in_explicit_code_block = True + prev_blank = 0 + continue + if i > 1 and prev_blank: return i - 1, i, False return i, i, False - if self.non_jupyter_code_re and self.non_jupyter_code_re.match(line): + if self.non_jupyter_code_re.match(line): in_explicit_code_block = True prev_blank = 0 continue diff --git a/tests/test_read_simple_markdown.py b/tests/test_read_simple_markdown.py index 8fd514ff0..1a49563dd 100644 --- a/tests/test_read_simple_markdown.py +++ b/tests/test_read_simple_markdown.py @@ -455,3 +455,34 @@ def test_inactive_cell_using_tag(text='''```python tags=["active-md"] compare_notebooks(nb, expected) text2 = jupytext.writes(nb, 'md') compare(text2, text) + + +def test_inactive_cell_using_noeval(text='''This is text + +```python .noeval +# This is python code. +# It should not become a code cell +``` +'''): + expected = new_notebook(cells=[new_markdown_cell(text[:-1])]) + nb = jupytext.reads(text, 'md') + compare_notebooks(nb, expected) + text2 = jupytext.writes(nb, 'md') + compare(text2, text) + + +def test_noeval_followed_by_code_works(text='''```python .noeval +# Not a code cell in Jupyter +``` + +```python +1 + 1 +``` +''', expected=new_notebook(cells=[new_markdown_cell('''```python .noeval +# Not a code cell in Jupyter +```'''), + new_code_cell('1 + 1')])): + nb = jupytext.reads(text, 'md') + compare_notebooks(nb, expected) + text2 = jupytext.writes(nb, 'md') + compare(text2, text)