Skip to content

Commit ccedec8

Browse files
committed
continuing fixes for #47.
anticipating 6 known failures.
1 parent a74bceb commit ccedec8

4 files changed

Lines changed: 40 additions & 22 deletions

File tree

tests/test_block.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,8 @@ def test_extended_pre_block_with_many_newlines():
9191
yet anothe word</pre>'''
9292
result = textile.textile(text)
9393
assert result == expect
94+
95+
text = 'p. text text\n\n\nh1. Hello\n'
96+
expect = '\t<p>text text</p>\n\n\n\t<h1>Hello</h1>'
97+
result = textile.textile(text)
98+
assert result == expect

tests/test_values.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@
178178
('@monospaced text@, followed by text',
179179
'\t<p><code>monospaced text</code>, followed by text</p>'),
180180

181-
('h2. A header\n\n\n\n\n\nsome text', '\t<h2>A header</h2>\n\n\t<p>some text</p>'),
181+
('h2. A header\n\n\n\n\n\nsome text', '\t<h2>A header</h2>\n\n\n\n\n\n\t<p>some text</p>'),
182182

183183
('pre.. foo bar baz\nquux', '<pre>foo bar baz\nquux</pre>'),
184184

textile/core.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -420,31 +420,41 @@ def block(self, text):
420420
tre = '|'.join(self.btag)
421421
else:
422422
tre = '|'.join(self.btag_lite)
423-
text = text.split('\n\n')
423+
424+
# split the text by two or more newlines, retaining the newlines in the
425+
# split list
426+
text = re.split(r'(\n{2,})', text)
427+
428+
eat_whitespace = False
424429

425430
tag = 'p'
426-
atts = cite = graf = ext = ''
431+
atts = cite = ext = ''
427432

428-
last_item_is_a_shelf = False
429433
out = []
430434

431435
for line in text:
436+
if not line.strip():
437+
if not eat_whitespace:
438+
out.append(line)
439+
continue
440+
441+
eat_whitespace = False
442+
432443
pattern = (r'^(?P<tag>{0})(?P<atts>{1}{2})\.(?P<ext>\.?)'
433444
r'(?::(?P<cite>\S+))? (?P<content>.*)$'.format(tre,
434445
align_re_s, cls_re_s))
435446
match = re.search(pattern, line, flags=re.S | re.U)
436-
if out:
437-
last_item_is_a_shelf = out[-1] in self.shelf
438447
# tag specified on this line.
439448
if match:
440449
# if we had a previous extended tag but not this time, close up
441450
# the tag
442-
if ext and match.group('tag') and last_item_is_a_shelf:
443-
content = out.pop()
451+
if ext and out:
452+
content = out[-2]
444453
content = generate_tag(block.inner_tag, content,
445454
block.inner_atts)
446-
out.append(generate_tag(block.outer_tag, content,
447-
block.outer_atts))
455+
content = generate_tag(block.outer_tag, content,
456+
block.outer_atts)
457+
out[-2] = content
448458
tag, atts, ext, cite, content = match.groups()
449459
block = Block(self, **match.groupdict())
450460
inner_block = generate_tag(block.inner_tag, block.content,
@@ -465,16 +475,10 @@ def block(self, text):
465475
# if we're inside an extended block, add the text from the
466476
# previous extension to the front
467477
if ext and out:
468-
line = '{0}\n\n{1}'.format(out.pop(), line)
469-
whitespace = ' \t\n\r\f\v'
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:
478+
line = '{0}{1}'.format(out.pop(), line)
479+
if not ext or line[0].strip():
475480
block = Block(self, tag, atts, ext, cite, line)
476-
if (block.tag == 'p' and not has_raw_text(block.content)
477-
or last_item_is_a_shelf):
481+
if block.tag == 'p' and not has_raw_text(block.content):
478482
line = block.content
479483
else:
480484
line = generate_tag(block.outer_tag, block.content,
@@ -486,19 +490,27 @@ def block(self, text):
486490
line = self.doPBr(line)
487491
line = line.replace('<br>', '<br />')
488492

489-
if line.strip():
493+
if ext and not match:
494+
try:
495+
last_item = out.pop()
496+
except IndexError:
497+
last_item = ''
498+
out.append('{0}{1}'.format(last_item, line))
499+
elif not block.eat:
490500
out.append(line)
491501

492502
if not ext:
493503
tag = 'p'
494504
atts = ''
495505
cite = ''
496-
graf = ''
506+
507+
if block.eat:
508+
eat_whitespace = True
497509

498510
if ext and out:
499511
out.append(generate_tag(block.outer_tag, out.pop(),
500512
block.outer_atts))
501-
return '\n\n'.join(out)
513+
return ''.join(out)
502514

503515
def footnoteRef(self, text):
504516
# somehow php-textile gets away with not capturing the space.

textile/objects/block.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def process(self):
4949
# It will be empty if the regex matched and ate it.
5050
if '' == notedef:
5151
self.content = notedef
52+
self.eat = True
5253

5354
fns = re.search(r'fn(?P<fnid>{0}+)'.format(regex_snippets['digit']),
5455
self.tag, flags=re.U)

0 commit comments

Comments
 (0)