Skip to content

Commit

Permalink
Cell marker has the same indentation as the first line in the cell #562
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Jul 30, 2020
1 parent 3a95305 commit 2cfc8c4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
**Changed**
- Install Jupytext from source on MyBinder to avoid cache issues (#567)
- Skip the tests that execute a notebook on Windows to avoid timeout issues (#489)
- The `# %%` cell marker has the same indentation as the first line in the cell (#562)

**Fixed**
- Configured coverage targets in `codecov.yml`
Expand Down
4 changes: 2 additions & 2 deletions jupytext/cell_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,9 +747,9 @@ def __init__(self, fmt, default_language=None):
script = _SCRIPT_EXTENSIONS[self.ext]
self.default_language = default_language or script["language"]
self.comment = script["comment"]
self.start_code_re = re.compile(r"^{}\s*%%(%*)\s(.*)$".format(self.comment))
self.start_code_re = re.compile(r"^\s*{}\s*%%(%*)\s(.*)$".format(self.comment))
self.alternative_start_code_re = re.compile(
r"^{}\s*(%%|<codecell>|In\[[0-9 ]*\]:?)\s*$".format(self.comment)
r"^\s*{}\s*(%%|<codecell>|In\[[0-9 ]*\]:?)\s*$".format(self.comment)
)
self.explicit_soc = True

Expand Down
12 changes: 10 additions & 2 deletions jupytext/cell_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,18 @@ def cell_to_text(self):
options = metadata_to_double_percent_options(
self.metadata, self.cell_metadata_json
)
indent = ""
if self.is_code() and active and self.source:
first_line = self.source[0]
if first_line.strip():
left_space = re.compile(r"^(\s*)").match(first_line)
if left_space:
indent = left_space.groups()[0]

if options.startswith("%") or not options:
lines = [self.comment + " %%" + options]
lines = [indent + self.comment + " %%" + options]
else:
lines = [self.comment + " %% " + options]
lines = [indent + self.comment + " %% " + options]

if self.is_code() and active:
source = copy(self.source)
Expand Down
29 changes: 29 additions & 0 deletions tests/test_read_simple_percent.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,32 @@ def test_docstring_with_quadruple_quote(
py = jupytext.writes(nb, "py:percent")
nb2 = jupytext.reads(py, "py")
compare_notebooks(nb2, nb)


def test_cell_marker_has_same_indentation_as_code(
text="""# %%
if __name__ == '__main__':
print(1)
# %%
# INDENTED COMMENT
print(2)
""",
nb_expected=new_notebook(
cells=[
new_code_cell(
"""if __name__ == '__main__':
print(1)"""
),
new_code_cell(
""" # INDENTED COMMENT
print(2)"""
),
]
),
):
"""The cell marker should have the same indentation as the first code line. See issue #562"""
nb_actual = jupytext.reads(text, fmt="py:percent")
compare_notebooks(nb_actual, nb_expected)
text_actual = jupytext.writes(nb_actual, fmt="py:percent")
compare(text_actual, text)

0 comments on commit 2cfc8c4

Please sign in to comment.