From d5a94c21313915ebbd50c061bf19e68ef6dee115 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Wed, 6 Feb 2013 16:05:52 -0500 Subject: 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. --- markdown/__init__.py | 2 +- 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): -- cgit v1.2.3