Skip to content

Commit

Permalink
Fixes: #61. Allows subsequent text blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioggstream committed Nov 16, 2017
1 parent 1aca640 commit a63a128
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
33 changes: 13 additions & 20 deletions notedown/notedown.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,37 +264,30 @@ def parse_blocks(self, text):
code_matches = [m for m in self.code_pattern.finditer(text)]

# determine where the limits of the non code bits are
# based on the code block edges
# based on the code block edges and save the block
# edge to merge code and text blocks.
text_starts = [0] + [m.end() for m in code_matches]
text_stops = [m.start() for m in code_matches] + [len(text)]
text_limits = list(zip(text_starts, text_stops))

# list of the groups from the code blocks
code_blocks = [self.new_code_block(**m.groupdict())
code_blocks = [(m.start(), self.new_code_block(**m.groupdict()))
for m in code_matches]

text_blocks = [self.new_text_block(content=text[i:j])
text_blocks = [(i, self.new_text_block(content=text[i:j]))
for i, j in text_limits]

# remove indents
list(map(self.pre_process_code_block, code_blocks))
for pos, block in code_blocks:
self.pre_process_code_block(block)
# remove blank line at start and end of markdown
list(map(self.pre_process_text_block, text_blocks))

# create a list of the right length
all_blocks = list(range(len(text_blocks) + len(code_blocks)))

# NOTE: the behaviour here is a bit fragile in that we
# assume that cells must alternate between code and
# markdown. This isn't the case, as we could have
# consecutive code cells, and we get around this by
# stripping out empty cells. i.e. two consecutive code cells
# have an empty markdown cell between them which is stripped
# out because it is empty.

# cells must alternate in order
all_blocks[::2] = text_blocks
all_blocks[1::2] = code_blocks
for pos, block in text_blocks:
self.pre_process_text_block(block)

# Merge back text and code blocks using the
# start position.
all_blocks = sorted(text_blocks + code_blocks, key=lambda x: x[0])
all_blocks = [block for pos, block in all_blocks]

# remove possible empty text cells
all_blocks = [cell for cell in all_blocks if cell['content']]
Expand Down
6 changes: 6 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[tox]
envlist=py27,py34

[testenv]
deps= nose
commands=nosetests

0 comments on commit a63a128

Please sign in to comment.