Skip to content

Commit cf7faf7

Browse files
committed
revise normalize newlines to match php-textile and fix #47 and related issues.
1 parent 82b1545 commit cf7faf7

4 files changed

Lines changed: 52 additions & 7 deletions

File tree

tests/test_block.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,25 @@ def test_blockcode_comment():
6969
t = textile.Textile()
7070
result = t.parse(input)
7171
assert result == expect
72+
73+
def test_extended_pre_block_with_many_newlines():
74+
"""Extra newlines in an extended pre block should not get cut down to only
75+
two."""
76+
text = '''pre.. word
77+
78+
another
79+
80+
word
81+
82+
83+
yet anothe word'''
84+
expect = '''<pre>word
85+
86+
another
87+
88+
word
89+
90+
91+
yet anothe word</pre>'''
92+
result = textile.textile(text)
93+
assert result == expect

tests/test_github_issues.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,22 @@ def test_github_issue_45():
137137
result = textile.textile(text)
138138
expect = '\t<p><a href="https://myabstractwiki.ru/index.php/%D0%97%D0%B0%D0%B3%D0%BB%D0%B0%D0%B2%D0%BD%D0%B0%D1%8F_%D1%81%D1%82%D1%80%D0%B0%D0%BD%D0%B8%D1%86%D0%B0">test</a></p>'
139139
assert result == expect
140+
141+
def test_github_issue_47():
142+
"""Incorrect wrap pre-formatted value"""
143+
text = '''pre.. word
144+
145+
another
146+
147+
word
148+
149+
yet anothe word'''
150+
result = textile.textile(text)
151+
expect = '''<pre>word
152+
153+
another
154+
155+
word
156+
157+
yet anothe word</pre>'''
158+
assert result == expect

textile/core.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -433,12 +433,12 @@ def block(self, text):
433433
r'(?::(?P<cite>\S+))? (?P<content>.*)$'.format(tre,
434434
align_re_s, cls_re_s))
435435
match = re.search(pattern, line, flags=re.S | re.U)
436+
if out:
437+
last_item_is_a_shelf = out[-1] in self.shelf
436438
# tag specified on this line.
437439
if match:
438440
# if we had a previous extended tag but not this time, close up
439441
# the tag
440-
if out:
441-
last_item_is_a_shelf = out[-1] in self.shelf
442442
if ext and match.group('tag') and last_item_is_a_shelf:
443443
content = out.pop()
444444
content = generate_tag(block.inner_tag, content,
@@ -467,9 +467,14 @@ def block(self, text):
467467
if ext and out:
468468
line = '{0}\n\n{1}'.format(out.pop(), line)
469469
whitespace = ' \t\n\r\f\v'
470-
if ext or not line[0] in whitespace:
470+
try:
471+
line_first_char_blank = line[0] in whitespace
472+
except IndexError:
473+
line_first_char_blank = True
474+
if ext or not line_first_char_blank:
471475
block = Block(self, tag, atts, ext, cite, line)
472-
if block.tag == 'p' and not has_raw_text(block.content):
476+
if (block.tag == 'p' and not has_raw_text(block.content)
477+
or last_item_is_a_shelf):
473478
line = block.content
474479
else:
475480
line = generate_tag(block.outer_tag, block.content,

textile/utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,8 @@ def list_type(list_string):
110110

111111
def normalize_newlines(string):
112112
out = string.strip()
113-
out = re.sub(r'\r\n', '\n', out)
114-
out = re.sub(r'\n{3,}', '\n\n', out)
115-
out = re.sub(r'\n\s*\n', '\n\n', out)
113+
out = re.sub(r'\r\n?', '\n', out)
114+
out = re.sub(r'^[ \t]*\n', '\n', out, flags=re.M)
116115
out = re.sub(r'"$', '" ', out)
117116
return out
118117

0 commit comments

Comments
 (0)