@@ -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.
0 commit comments