aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWaylan Limberg <waylan@gmail.com>2013-02-14 11:46:30 -0500
committerWaylan Limberg <waylan@gmail.com>2013-02-14 11:46:30 -0500
commitfdfc84405ba690705ff343d46ab658bfc50a8836 (patch)
treee148284f3afbd85bfe5970bc7cbb15df1b4f3492
parent5c5612a404f920e08aacfb50fcf4eca08a994d17 (diff)
downloadmarkdown-fdfc84405ba690705ff343d46ab658bfc50a8836.tar.gz
markdown-fdfc84405ba690705ff343d46ab658bfc50a8836.tar.bz2
markdown-fdfc84405ba690705ff343d46ab658bfc50a8836.zip
Preserve all blank lines in code blocks.
Fixes #183. Finally got this working properly. The key was using a regex substitution with non-overlapping matches that removed all whitespace from the begining of *all* blank lines when normalizing whitespace. Once I got that, I could simplfy the EmptyBlockProcessor and easily output one or two blank lines appropriately. A blank block gets two new lines (`'\n\n'`), while a block which starts with a newline gets one.
-rw-r--r--markdown/blockprocessors.py26
-rw-r--r--markdown/preprocessors.py2
2 files changed, 14 insertions, 14 deletions
diff --git a/markdown/blockprocessors.py b/markdown/blockprocessors.py
index b41df6a..8b41a37 100644
--- a/markdown/blockprocessors.py
+++ b/markdown/blockprocessors.py
@@ -495,27 +495,27 @@ class HRProcessor(BlockProcessor):
class EmptyBlockProcessor(BlockProcessor):
- """ Process blocks and start with an empty line. """
-
- # Detect a block that only contains whitespace
- # or only whitespace on the first line.
- RE = re.compile(r'^ *(\n|$)')
+ """ Process blocks that are empty or start with an empty line. """
def test(self, parent, block):
- return bool(self.RE.match(block))
+ return not block or block.startswith('\n')
def run(self, parent, blocks):
block = blocks.pop(0)
- m = self.RE.match(block)
- if m:
- theRest = block[m.end():]
+ filler = '\n\n'
+ if block:
+ # Starts with empty line
+ # Only replace a single line.
+ filler = '\n'
+ # Save the rest for later.
+ theRest = block[1:]
if theRest:
# Add remaining lines to master blocks for later.
blocks.insert(0, theRest)
- sibling = self.lastChild(parent)
- if sibling and sibling.tag == 'pre' and len(sibling) and sibling[0].tag == 'code':
- # Last block is a codeblock. Append to preserve whitespace.
- sibling[0].text = util.AtomicString('%s\n' % sibling[0].text )
+ sibling = self.lastChild(parent)
+ if sibling and sibling.tag == 'pre' and len(sibling) and sibling[0].tag == 'code':
+ # Last block is a codeblock. Append to preserve whitespace.
+ sibling[0].text = util.AtomicString('%s%s' % (sibling[0].text, filler))
class ParagraphProcessor(BlockProcessor):
diff --git a/markdown/preprocessors.py b/markdown/preprocessors.py
index 3751264..6238303 100644
--- a/markdown/preprocessors.py
+++ b/markdown/preprocessors.py
@@ -50,7 +50,7 @@ class NormalizeWhitespace(Preprocessor):
source = source.replace(util.STX, "").replace(util.ETX, "")
source = source.replace("\r\n", "\n").replace("\r", "\n") + "\n\n"
source = source.expandtabs(self.markdown.tab_length)
- source = re.sub(r'\n +\n', '\n\n', source)
+ source = re.sub(r'(?<=\n) +\n', '\n', source)
return source.split('\n')