Commits

Aram Dulyan  committed 04e378c

Fixed issues caused by simple tags being broken up by block tags such as headings, and a tiny bit of refactoring.

  • Participants
  • Parent commits 0843a03

Comments (0)

Files changed (2)

File html2textile.py

             break
         return False
     
+    def _rstrip_open_simple_tags(self):
+        """
+        Strips any simple tags in just opened state from the end of the stack.
+        Returns an empty string if the strip was productive, otherwise returns
+        the string tag sequence to close all open simple tags.
+        
+        This unorthodox return value is because this functionality 
+        is invariably used to determine whether the stack ends with the simple
+        tags opened on a blank line, in which case the strip would be
+        successful and the tags wouldn't need to be closed.
+        """
+        stripped = False
+        for open_tag in self.open_simple_tags:
+            stripped = stripped or self._rstrip_open_tag(open_tag)
+        return '' if stripped else ''.join(self.open_simple_tags)
+    
     def _ends_with_newline(self):
         """
         Returns True if the current stack ends with a newline, allowing for
                 new_tag = SIMPLE_TAGS[tag]
                 newline = ''
                 self.open_simple_tags.insert(0, SIMPLE_TAGS[tag])
-        elif tag in ('h1', 'h2', 'h3', 'h4', 'h5', 'h6'):
+        elif tag in ('h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'div'):
             override_block = True
             self._rstrip()
-            newline = '\n\n'
-            new_tag = tag
-            dot = '. '
-        elif tag in ['p', 'div']:
-            override_block = True
-            self._rstrip()
-            # Simple tags should not span across paragraphs
-            newline = '%s\n\n' % ''.join(self.open_simple_tags)
+            # Simple tags should not span across blocks
+            newline = '%s\n\n' % self._rstrip_open_simple_tags()
             self.open_simple_tags = []
-            if any((style_value, lang_value, class_value)):
-                new_tag = 'p'
+            if tag in ['p', 'div']:
+                if any((style_value, lang_value, class_value)):
+                    new_tag = 'p'
+                    dot = '. '
+                else: 
+                    new_tag = ''
+                    dot = ''
+            else:
+                new_tag = tag
                 dot = '. '
-            else: 
-                new_tag = ''
-                dot = ''
         elif tag == 'br':
             if self.just_opened:
                 textile = ''
             else:
                 # Since Textile doesn't allow simple tags to span across
                 # line breaks, close them and reopen them after the break
-                blank_line = False
-                for open_tag in self.open_simple_tags:
-                    blank_line = blank_line or self._rstrip_open_tag(open_tag)
                 textile = '%s\n%s' % (
-                    ''.join(self.open_simple_tags) if not blank_line else '',
+                    self._rstrip_open_simple_tags(),
                     ''.join(reversed(self.open_simple_tags)),
                 )
         elif tag == 'blockquote':
 Three.
 Four.
 
+Test that other block tags also behave sensibly
+>>> convert('<span><h1><span>Title.</span></h1></span>')
+h1. %Title.%
+
 Test that blank lines remain blank
 >>> convert('<span><span>One.<br /></span><br />Two.</span>')
-%Text.%
+%One.%
 <BLANKLINE>
 %Two.%