Skip to content

Commit

Permalink
Identify Sphinx gallery cell header with a regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Sep 23, 2018
1 parent fb9d04f commit 2e1b538
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
6 changes: 3 additions & 3 deletions jupytext/cell_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ class SphinxGalleryScriptCellReader(ScriptCellReader):

comment = '#'
default_language = 'python'
twenty_hash = '#' * 20
twenty_hash = re.compile('^#( |)#{19,}\s*$')
markdown_marker = None
rst2md = False

Expand All @@ -486,7 +486,7 @@ def start_of_new_markdown_cell(self, line):
if line.startswith(triple_quote):
return triple_quote

if line.startswith('#') and self.twenty_hash in line:
if self.twenty_hash.match(line):
return line

return None
Expand Down Expand Up @@ -576,7 +576,7 @@ def find_cell_content(self, lines):
last = lines[cell_end_marker - 1]
lines[cell_end_marker - 1] = \
last[:last.rfind(self.markdown_marker)]
if self.markdown_marker.startswith(self.twenty_hash):
if self.twenty_hash.match(self.markdown_marker):
cell_start = 1
else:
self.metadata = {}
Expand Down
10 changes: 8 additions & 2 deletions tests/test_read_simple_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ def test_read_more_complex_file(script="""'''This is a markdown cell'''
Another markdown cell'''
1 + 2 + 3 + 4
# ################################
# A fifth one
'''And a last one
'''
"""):
Expand Down Expand Up @@ -97,6 +100,9 @@ def test_read_more_complex_file(script="""'''This is a markdown cell'''
assert nb.cells[6].source == '1 + 2 + 3 + 4'

assert nb.cells[7].cell_type == 'markdown'
assert nb.cells[7].source == 'And a last one'
assert nb.cells[7].source == 'A fifth one'

assert nb.cells[8].cell_type == 'markdown'
assert nb.cells[8].source == 'And a last one'

assert len(nb.cells) == 8
assert len(nb.cells) == 9

0 comments on commit 2e1b538

Please sign in to comment.