Skip to content

Commit 8c88140

Browse files
committed
Allow text blocks with spaces around first and last newlines
1 parent a1c31e5 commit 8c88140

4 files changed

Lines changed: 16 additions & 3 deletions

File tree

CHANGELOG.textile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
12
h1. Textile Changelog
23

4+
h2. Version 4.0.4
5+
6+
* Bugfixes:
7+
** Allow text blocks with spaces around first and last newlines
8+
39
h2. Version 4.0.3
410
* Update supported Python versions to 3.8 - 3.12 ("#83":https://github.com/textile/python-textile/issues/83)
511
* Replace html5lib with nh3 for html sanitization

tests/test_textile.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,9 @@ def test_relURL():
242242
t = textile.Textile()
243243
t.restricted = True
244244
assert t.relURL("gopher://gopher.com/") == '#'
245+
246+
247+
def test_whitespace_at_beginning_and_end():
248+
expect = textile.textile(' \n Testing 1 2 3 \n ', html_type='html5')
249+
result = '\t<p>Testing 1 2 3</p>'
250+
assert result == expect

textile/core.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,8 @@ def block(self, text):
442442

443443
tag = 'p'
444444
atts = cite = ext = ''
445-
446445
out = []
447-
446+
block = None
448447
for line in text:
449448
# the line is just whitespace, add it to the output, and move on
450449
if not line.strip():

textile/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ def list_type(list_string):
149149

150150
def normalize_newlines(string):
151151
out = re.sub(r'\r\n?', '\n', string)
152-
out = re.compile(r'^[ \t]*\n', flags=re.M).sub('\n', out)
152+
# strip spaces around first and last newline
153+
out = re.compile(r'^[ \t]*\n[ \t]*', flags=re.M).sub('\n', out)
154+
out = re.compile(r'[ \t]*\n[ \t]*$', flags=re.M).sub('\n', out)
153155
out = out.strip('\n')
154156
return out
155157

0 commit comments

Comments
 (0)