diff options
author | Waylan Limberg <waylan@gmail.com> | 2013-02-06 16:05:52 -0500 |
---|---|---|
committer | Waylan Limberg <waylan@gmail.com> | 2013-02-06 16:05:52 -0500 |
commit | d5a94c21313915ebbd50c061bf19e68ef6dee115 (patch) | |
tree | 0bf1cd8c1c68b42458a225418efc157132d021a2 | |
parent | 4ff74e33a48a8c4e101d2f5e259d7b911c03d9f9 (diff) | |
download | markdown-d5a94c21313915ebbd50c061bf19e68ef6dee115.tar.gz markdown-d5a94c21313915ebbd50c061bf19e68ef6dee115.tar.bz2 markdown-d5a94c21313915ebbd50c061bf19e68ef6dee115.zip |
Preserve empty lines in code blocks
Partial fix for #183. Some lines are still being lost. When the processors are run, one line is lost. When their calling code is comments out (completely skiped) a line is still lost if more than 3 exist in a row.
Also need to add some tests for this.
-rw-r--r-- | markdown/__init__.py | 2 | ||||
-rw-r--r-- | markdown/blockprocessors.py | 13 |
2 files changed, 8 insertions, 7 deletions
diff --git a/markdown/__init__.py b/markdown/__init__.py index 959f387..fbd2879 100644 --- a/markdown/__init__.py +++ b/markdown/__init__.py @@ -283,8 +283,8 @@ class Markdown: source = source.replace(util.STX, "").replace(util.ETX, "") source = source.replace("\r\n", "\n").replace("\r", "\n") + "\n\n" - source = re.sub(r'\n\s+\n', '\n\n', source) source = source.expandtabs(self.tab_length) + source = re.sub(r'\n +\n', '\n\n', source) # Split into lines and run the line preprocessors. self.lines = source.split("\n") diff --git a/markdown/blockprocessors.py b/markdown/blockprocessors.py index 1e6160f..3c320f8 100644 --- a/markdown/blockprocessors.py +++ b/markdown/blockprocessors.py @@ -499,7 +499,7 @@ class EmptyBlockProcessor(BlockProcessor): # Detect a block that only contains whitespace # or only whitespace on the first line. - RE = re.compile(r'^\s*\n') + RE = re.compile(r'(^ *$)|(^ *\n)') def test(self, parent, block): return bool(self.RE.match(block)) @@ -508,13 +508,14 @@ class EmptyBlockProcessor(BlockProcessor): block = blocks.pop(0) m = self.RE.match(block) if m: - # Add remaining line to master blocks for later. - blocks.insert(0, block[m.end():]) + theRest = block[m.end():] + 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 sibling[0] and \ - sibling[0].tag == 'code': + 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/n/n' % sibling[0].text ) + sibling[0].text = util.AtomicString('%s\n' % sibling[0].text ) class ParagraphProcessor(BlockProcessor): |