Skip to content
Browse files

[614664] Tolerate wikimarkup's habit of including adjacent newlines i…

…n the paragraphs the stripper makes for block-level {for}s.

More specifically, make the unstripper work in more (all?) cases so the rebalancer doesn't move the {/for} up into the empty paragraph.
  • Loading branch information...
1 parent 5e65126 commit 09c452b88d2f29189047e10bde04581278c54b28 @erikrose erikrose committed Nov 24, 2010
Showing with 25 additions and 4 deletions.
  1. +18 −4 apps/wiki/parser.py
  2. +7 −0 apps/wiki/tests/test_parser.py
View
22 apps/wiki/parser.py
@@ -286,10 +286,24 @@ def preceding_whitespace(str, pos):
# Dratted wiki formatter likes to put <p> tags around my token when it sits
# on a line by itself, so tolerate and consume that foolishness:
- _PARSED_STRIPPED_FOR = re.compile(r'<p>\s*\x07(\d+)\x07\s*</p>'
- r'|\x07(\d+)\x07')
- _PARSED_STRIPPED_FOR_CLOSER = re.compile(r'<p>\s*\x07/sf\x07\s*</p>'
- r'|\x07/sf\x07')
+ _PARSED_STRIPPED_FOR = re.compile(
+ # Whitespace, a {for} token, then more whitespace (including <br>s):
+ r'<p>'
+ r'(?:\s|<br\s*/?>)*'
+ r'\x07(\d+)\x07' # The {for} token
+ r'(?:\s|<br\s*/?>)*'
+ r'</p>'
+ # Alternately, a lone {for} token that didn't get wrapped in a <p>:
+ r'|\x07(\d+)\x07')
+ _PARSED_STRIPPED_FOR_CLOSER = re.compile(
+ # Similar to above, a {/for} token wrapped in <p> and whitespace:
+ r'<p>'
+ r'(?:\s|<br\s*/?>)*'
+ r'\x07/sf\x07' # {/for} token
+ r'(?:\s|<br\s*/?>)*'
+ r'</p>'
+ # Or a lone {/for} token:
+ r'|\x07/sf\x07')
@classmethod
def unstrip_fors(cls, html, dehydrations):
View
7 apps/wiki/tests/test_parser.py
@@ -577,6 +577,13 @@ def test_adjacent_blocks(self):
# The two div.fors should be siblings, not nested:
eq_([], pq(html)('div.for div.for'))
+ def test_leading_newlines(self):
+ """Make sure leading newlines don't cause a block-level {for} to be
+ sucked into the leading blank paragraph, causing the actual text to
+ always be shown."""
+ doc = pq(WikiParser().parse('\n\n{for linux}\nunixify\n{/for}'))
+ eq_('unixify', doc('.for').text().strip())
+
def test_big_swath(self):
"""Enclose a big section containing many tags."""
parsed_eq('<div class="for"><h1 id="w_h1">H1</h1>'

0 comments on commit 09c452b

Please sign in to comment.
Something went wrong with that request. Please try again.