Commits

Aram Dulyan committed 310d7c5

Even though <a> tags around <p> tags are invalid, people do it anyway, so trying to parse them in a sane manner.

  • Participants
  • Parent commits 388aa7d

Comments (0)

Files changed (2)

                 break
     
     def start(self, tag, attrib):
+        override_block = False
         textile = None
         new_tag = None
         newline = '\n'
             new_tag = SIMPLE_TAGS[tag]
             newline = ''
         elif tag in ('h1', 'h2', 'h3', 'h4', 'h5', 'h6'):
+            override_block = True
             new_tag = tag
             dot = '. '
         elif tag == 'p':
+            override_block = True
             self._rstrip()
             newline = '\n\n'
             if any((style_value, lang_value, class_value, id_value)):
         if textile is not None:
             self.just_closed = False 
             self.just_opened = tag in SIMPLE_TAGS
-            if not self.block:
+            if not self.block or override_block:
                 self.final_output.append(textile)
             else:
                 self.haystack.append(textile)
         
     def end(self, tag):
+        textile = None
         change_tag_status = True
+        override_block = False
         if tag in SIMPLE_TAGS:
             self._rstrip()
             self.just_opened = False
             self.just_closed = True
-            self.final_output.append(SIMPLE_TAGS[tag])
+            textile = SIMPLE_TAGS[tag]
             change_tag_status = False
         elif tag in ('h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p'):
-            self.final_output.append('\n')
+            override_block = True
+            textile = '\n'
         elif tag == 'a':
             if self.a_part['title']:
                 textilized = ' "%s (%s)":%s ' % (
             self.ul_ident -= 1
             self.list_types.pop()
             if len(self.list_types) == 0:
-                self.final_output.append('\n')
+                textile = '\n'
         elif tag == 'ol':
             self.ol_ident -= 1
             self.list_types.pop()
             if len(self.list_types) == 0:
-                self.final_output.append('\n')
+                textile = '\n'
         else:
             change_tag_status = False
         
             self.just_opened = False
             self.just_closed = False
         
+        if textile is not None:
+            stack = self.haystack \
+                if self.block and not override_block \
+                else self.final_output
+            stack.append(textile)
+        
     def data(self, data):
         #we dont want any linebreaks inside our tags
         node_data = data.replace('\n','')
 p(first). Paragraph.
 <BLANKLINE>
 p(second). Paragraph.
+
+Test sane handling of paragraphs enclosed in links.
+>>> convert('<a href="http://example.com"><p><em>Example.</em></p></a>')
+" _Example._":http://example.com
+
+KNOWN ISSUE (more than one paragraph inside a link):
+>>> #convert('<a href="http://example.com"><p class="first">1</p><p class="second">2</p></a>')
 """
 
 if __name__ == "__main__":