]> git.sommitrealweird.co.uk Git - rss2maildir.git/commitdiff
* Small improvements to the HTML2Text code
authorBrett Parker <iDunno@sommitrealweird.co.uk>
Sun, 6 Jan 2008 11:43:44 +0000 (11:43 +0000)
committerBrett Parker <iDunno@sommitrealweird.co.uk>
Sun, 6 Jan 2008 11:43:44 +0000 (11:43 +0000)
* Reorganize unittests for parsing to make it easier to add more tests later

rss2maildir.py
tests/expected/definitionlist-badlyformed.txt [new file with mode: 0644]
tests/expected/definitionlist-wellformed.txt
tests/html/definitionlist-badlyformed.html [new file with mode: 0644]
tests/unittests/DefinitionListTests.py
tests/unittests/ParagraphTests.py
tests/unittests/ParsingTests.py [new file with mode: 0755]
tests/unittests/UnorderedListTests.py

index 739c1f315186c9cec746f891516fe7ec74696065..a51209ce0749e70fd5e9d40ff696d8cf6a2243e1 100755 (executable)
@@ -123,6 +123,9 @@ class HTML2Text(HTMLParser):
         if len(self.opentags) == 0:
             return
 
         if len(self.opentags) == 0:
             return
 
+        if len(self.curdata) == 0:
+            return
+
         tag_thats_done = self.opentags[-1]
 
         if tag_thats_done in self.blockleveltags:
         tag_thats_done = self.opentags[-1]
 
         if tag_thats_done in self.blockleveltags:
@@ -169,6 +172,26 @@ class HTML2Text(HTMLParser):
                 + u'\n   '.join( \
                     textwrap.wrap(item, self.textwidth - 3))
             self.curdata = u''
                 + u'\n   '.join( \
                     textwrap.wrap(item, self.textwidth - 3))
             self.curdata = u''
+        elif tag_thats_done == "dt":
+            definition = self.curdata.encode("utf-8").strip()
+            if len(self.text) > 0 and self.text[-1] != u'\n':
+                self.text = self.text + u'\n\n'
+            elif len(self.text) > 0 and self.text[-2] != u'\n':
+                self.text = self.text + u'\n'
+            definition = definition + "::"
+            self.text = self.text \
+                + '\n '.join(
+                    textwrap.wrap(definition, self.textwidth - 1))
+            self.curdata = u''
+        elif tag_thats_done == "dd":
+            definition = self.curdata.encode("utf-8").strip()
+            if len(self.text) > 0 and self.text[-1] != u'\n':
+                self.text = self.text + u'\n'
+            self.text = self.text \
+                + '    ' \
+                + '\n    '.join( \
+                    textwrap.wrap(definition, self.textwidth - 4))
+            self.curdata = u''
         elif tag_thats_done in self.liststarttags:
             pass
         else:
         elif tag_thats_done in self.liststarttags:
             pass
         else:
@@ -224,6 +247,10 @@ class HTML2Text(HTMLParser):
         if len(self.text) == 0 or self.text[-1] != u'\n':
             self.text = self.text + u'\n'
         self.opentags = []
         if len(self.text) == 0 or self.text[-1] != u'\n':
             self.text = self.text + u'\n'
         self.opentags = []
+        if len(self.text) > 0:
+            while len(self.text) > 1 and self.text[-1] == u'\n':
+                self.text = self.text[:-1]
+            self.text = self.text + u'\n'
         return self.text
 
 def open_url(method, url):
         return self.text
 
 def open_url(method, url):
diff --git a/tests/expected/definitionlist-badlyformed.txt b/tests/expected/definitionlist-badlyformed.txt
new file mode 100644 (file)
index 0000000..3d9e83f
--- /dev/null
@@ -0,0 +1,6 @@
+An item::
+    It's definition
+
+Another item::
+    And it's got a much longer definition because we like to make sure
+    that we've got the test wrapping right don't we.
index 98beb1b28354d44ba18a2df2db78c8f33dc184d7..3d9e83f81d41fd5e4317941cea13930c83ae0807 100644 (file)
@@ -1,6 +1,6 @@
-An item
+An item::
     It's definition
 
     It's definition
 
-Another item
+Another item::
     And it's got a much longer definition because we like to make sure
     that we've got the test wrapping right don't we.
     And it's got a much longer definition because we like to make sure
     that we've got the test wrapping right don't we.
diff --git a/tests/html/definitionlist-badlyformed.html b/tests/html/definitionlist-badlyformed.html
new file mode 100644 (file)
index 0000000..562bad4
--- /dev/null
@@ -0,0 +1,6 @@
+<dl>
+    <dt>An item
+    <dd>It's definition
+    <dt>Another item
+    <dd>And it's got a much longer definition because we like to make sure that we've got the test wrapping right don't we.
+</dl>
index 905897307c31b3a198f5b164ea40befcf1836f51..a1abae02874dde93b70746a9d244f9425cece831 100755 (executable)
@@ -1,33 +1,21 @@
 #!/usr/bin/python
 
 import unittest
 #!/usr/bin/python
 
 import unittest
-import sys
 import os
 
 import os
 
-class DefinitionListTests(unittest.TestCase):
-    def setUp(self):
-        self.inputpath = os.path.sep.join(os.path.dirname(os.path.realpath(__file__)).split(os.path.sep)[0:-1])
+import ParsingTests
 
 
+class DefinitionListTests(ParsingTests.ParsingTest):
     def testWellFormedDefinitionList(self):
     def testWellFormedDefinitionList(self):
-        try:
-            from rss2maildir import HTML2Text
-        except:
-            sys.path.append(os.path.sep.join(self.inputpath.split(os.path.sep)[0:-1]))
-            try:
-                from rss2maildir import HTML2Text
-            except:
-                self.assert_(False)
-        input_path = os.path.sep.join(os.path.dirname(os.path.realpath(__file__)).split(os.path.sep)[0:-1])
-        input = open(os.path.join(input_path, "html", "definitionlist-wellformed.html")).read()
-        expectedoutput = open(os.path.join(input_path, "expected", "definitionlist-wellformed.txt")).read()
-        parser = HTML2Text()
-        parser.feed(input)
-        output = parser.gettext()
-        self.assertEqual(output, expectedoutput)
+        return self.runParsingTest("definitionlist-wellformed")
+
+    def testBadlyFormedDefinitionList(self):
+        return self.runParsingTest("definitionlist-badlyformed")
 
 def suite():
     suite = unittest.TestSuite()
     suite.addTest(DefinitionListTests("testWellFormedDefinitionList"))
 
 def suite():
     suite = unittest.TestSuite()
     suite.addTest(DefinitionListTests("testWellFormedDefinitionList"))
+    suite.addTest(DefinitionListTests("testBadlyFormedDefinitionList"))
     return suite
 
 if __name__ == "__main__":
     return suite
 
 if __name__ == "__main__":
index 9111ecdcf51ffc71d98bc07ebc352f749b1a9242..8b98a535a54d2669892be35ca9007c472ebc217f 100755 (executable)
@@ -4,26 +4,11 @@ import unittest
 import sys
 import os
 
 import sys
 import os
 
-class ParagraphTests(unittest.TestCase):
-    def setUp(self):
-        self.inputpath = os.path.sep.join(os.path.dirname(os.path.realpath(__file__)).split(os.path.sep)[0:-1])
+import ParsingTests
 
 
+class ParagraphTests(ParsingTests.ParsingTest):
     def testWellFormedParagraphs(self):
     def testWellFormedParagraphs(self):
-        try:
-            from rss2maildir import HTML2Text
-        except:
-            sys.path.append(os.path.sep.join(self.inputpath.split(os.path.sep)[0:-1]))
-            try:
-                from rss2maildir import HTML2Text
-            except:
-                self.assert_(False)
-        input_path = os.path.sep.join(os.path.dirname(os.path.realpath(__file__)).split(os.path.sep)[0:-1])
-        input = open(os.path.join(input_path, "html", "multiparagraph-wellformed.html")).read()
-        expectedoutput = open(os.path.join(input_path, "expected", "multiparagraph-wellformed.txt")).read()
-        parser = HTML2Text()
-        parser.feed(input)
-        output = parser.gettext()
-        self.assertEqual(output, expectedoutput)
+       return self.runParsingTest("multiparagraph-wellformed") 
 
 def suite():
     suite = unittest.TestSuite()
 
 def suite():
     suite = unittest.TestSuite()
diff --git a/tests/unittests/ParsingTests.py b/tests/unittests/ParsingTests.py
new file mode 100755 (executable)
index 0000000..20fc521
--- /dev/null
@@ -0,0 +1,26 @@
+#!/usr/bin/python
+
+import unittest
+import sys
+import os
+
+class ParsingTest(unittest.TestCase):
+    def setUp(self):
+        self.inputpath = os.path.sep.join(os.path.dirname(os.path.realpath(__file__)).split(os.path.sep)[0:-1])
+
+    def runParsingTest(self, filename):
+        try:
+            from rss2maildir import HTML2Text
+        except:
+            sys.path.append(os.path.sep.join(self.inputpath.split(os.path.sep)[0:-1]))
+            try:
+                from rss2maildir import HTML2Text
+            except:
+                self.assert_(False)
+        input_path = os.path.sep.join(os.path.dirname(os.path.realpath(__file__)).split(os.path.sep)[0:-1])
+        input = open(os.path.join(input_path, "html", filename + ".html")).read()
+        expectedoutput = open(os.path.join(input_path, "expected", filename + ".txt")).read()
+        parser = HTML2Text()
+        parser.feed(input)
+        output = parser.gettext()
+        self.assertEqual(output, expectedoutput)
index 2224e74427b952556a2235f1ca3133402f0d8990..a49546165a18e1a32f6fb4e56e376f353423d6b0 100755 (executable)
@@ -4,44 +4,14 @@ import unittest
 import sys
 import os
 
 import sys
 import os
 
-class UnorderedListTests(unittest.TestCase):
-    def setUp(self):
-        self.inputpath = os.path.sep.join(os.path.dirname(os.path.realpath(__file__)).split(os.path.sep)[0:-1])
+import ParsingTests
 
 
+class UnorderedListTests(ParsingTests.ParsingTest):
     def testWellFormedList(self):
     def testWellFormedList(self):
-        try:
-            from rss2maildir import HTML2Text
-        except:
-            sys.path.append(os.path.sep.join(self.inputpath.split(os.path.sep)[0:-1]))
-            try:
-                from rss2maildir import HTML2Text
-            except:
-                self.assert_(False)
-        input_path = os.path.sep.join(os.path.dirname(os.path.realpath(__file__)).split(os.path.sep)[0:-1])
-        input = open(os.path.join(input_path, "html", "unorderedlist-wellformed.html")).read()
-        expectedoutput = open(os.path.join(input_path, "expected", "unorderedlist-wellformed.txt")).read()
-        parser = HTML2Text()
-        parser.feed(input)
-        output = parser.gettext()
-        self.assertEqual(output, expectedoutput)
+        return self.runParsingTest("unordered-wellformed")
 
     def testBadlyFormedList(self):
 
     def testBadlyFormedList(self):
-        try:
-            from rss2maildir import HTML2Text
-        except:
-            sys.path.append(os.path.sep.join(self.inputpath.split(os.path.sep)[0:-1]))
-            try:
-                from rss2maildir import HTML2Text
-            except:
-                self.assert_(False)
-
-        input_path = os.path.sep.join(os.path.dirname(os.path.realpath(__file__)).split(os.path.sep)[0:-1])
-        input = open(os.path.join(input_path, "html", "unorderedlist-badlyformed.html")).read()
-        expectedoutput = open(os.path.join(input_path, "expected", "unorderedlist-badlyformed.txt")).read()
-        parser = HTML2Text()
-        parser.feed(input)
-        output = parser.gettext()
-        self.assertEqual(output, expectedoutput)
+        return self.runParsingTest("unordered-badlyformed")
 
 def suite():
     suite = unittest.TestSuite()
 
 def suite():
     suite = unittest.TestSuite()