Skip to content

Commit

Permalink
Empty code cells are preserved #53
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Sep 5, 2018
1 parent 169981b commit 171256b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
43 changes: 23 additions & 20 deletions jupytext/cell_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ def count_lines_to_next_cell(cell_end_marker, next_cell_start,
return 1


def last_two_lines_blank(source):
"""Are the two last lines blank, and not the third last one?"""
if len(source) < 3:
return False
return (not _BLANK_LINE.match(source[-3]) and
_BLANK_LINE.match(source[-2]) and
_BLANK_LINE.match(source[-1]))


class CellReader():
"""A class that can read notebook cells from their text representation"""

Expand Down Expand Up @@ -199,18 +208,11 @@ def find_cell_end_code(self, lines, cell_end_re,
end of cell marker, and position of next cell start"""
self.cell_type = 'code'
parser = StringParser('python' if self.ext in ['.py', '.jl'] else 'R')
empty = True
for i, line in enumerate(lines):
# skip cell header
if self.metadata is not None and i == 0:
continue

# Read something!
if not _BLANK_LINE.match(line):
empty = False
elif empty:
continue

if parser.is_quoted():
parser.read_line(line)
continue
Expand All @@ -228,7 +230,10 @@ def find_cell_end_code(self, lines, cell_end_re,
return i, i + 1, True
elif _BLANK_LINE.match(line):
if not next_code_is_indented(lines[i:]):
return i, i + 1, False
if i > 0:
return i, i + 1, False
if len(lines) == 1 or _BLANK_LINE.match(lines[1]):
return 1, 2, False

return len(lines), len(lines), False

Expand Down Expand Up @@ -273,10 +278,8 @@ def find_cell_content(self, lines):
self.content = source

# Exactly two empty lines at the end?
if (self.ext == '.py' and explicit_eoc and len(source) > 2 and
not _BLANK_LINE.match(source[-3]) and
_BLANK_LINE.match(source[-2]) and
_BLANK_LINE.match(source[-1])):
if (self.ext == '.py' and explicit_eoc and
last_two_lines_blank(source)):
self.content = source[:-2]
self.metadata['lines_to_end_of_cell_marker'] = 2

Expand All @@ -288,16 +291,16 @@ def find_cell_content(self, lines):
del self.metadata['active']
self.cell_type = 'raw'

# Does the next cell start one/two lines later?
if (next_cell_start + 2 < len(lines) and
# Explicit end of cell marker?
if (next_cell_start + 1 < len(lines) and
_BLANK_LINE.match(lines[next_cell_start]) and
_BLANK_LINE.match(lines[next_cell_start + 1]) and
not _BLANK_LINE.match(lines[next_cell_start + 2])):
next_cell_start += 2
elif (next_cell_start + 1 < len(lines) and
_BLANK_LINE.match(lines[next_cell_start]) and
not _BLANK_LINE.match(lines[next_cell_start + 1])):
not _BLANK_LINE.match(lines[next_cell_start + 1])):
next_cell_start += 1
elif (explicit_eoc and next_cell_start + 2 < len(lines) and
_BLANK_LINE.match(lines[next_cell_start]) and
_BLANK_LINE.match(lines[next_cell_start + 1]) and
not _BLANK_LINE.match(lines[next_cell_start + 2])):
next_cell_start += 2

self.lines_to_next_cell = count_lines_to_next_cell(
cell_end_marker,
Expand Down
2 changes: 1 addition & 1 deletion jupytext/cell_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def explicit_start_marker(self, source):
return True
if all([line.startswith('#') for line in self.source]):
return True
if CellReader(self.ext).read(source)[1] != len(source):
if CellReader(self.ext).read(source)[1] < len(source):
return True

return False
Expand Down
1 change: 0 additions & 1 deletion tests/test_preserve_empty_cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def test_file_with_blank_lines(blank_lines):
compare(py_script, py_script2)


@pytest.mark.skip(reason='#53')
@pytest.mark.parametrize('blank_cells', range(1, 3))
def test_notebook_with_empty_cells(blank_cells):
notebook = new_notebook(cells=[new_markdown_cell('markdown cell one')] +
Expand Down
8 changes: 3 additions & 5 deletions tests/test_read_simple_python.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-

import pytest
import jupytext
from testfixtures import compare
from .python_notebook_sample import f, g
Expand Down Expand Up @@ -495,10 +494,9 @@ def h(x):
assert len(notebook.cells) >= 6
for cell in notebook.cells:
lines = cell.source.splitlines()
if len(lines) == 1:
continue
assert lines[0]
assert lines[-1]
if len(lines) != 1:
assert lines[0]
assert lines[-1]

script2 = jupytext.writes(notebook, ext='.py')

Expand Down

0 comments on commit 171256b

Please sign in to comment.