Skip to content

Commit

Permalink
Implement .noeval attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Oct 18, 2019
1 parent d840a58 commit 85105fd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
21 changes: 19 additions & 2 deletions jupytext/cell_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions tests/test_read_simple_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 85105fd

Please sign in to comment.