[Checkins] SVN: z3c.etestbrowser/trunk/ Added function to allow pretty printing of HTML.

Christian Theune ct at gocept.com
Tue Dec 18 04:31:17 EST 2007


Log message for revision 82325:
  Added function to allow pretty printing of HTML.
  

Changed:
  U   z3c.etestbrowser/trunk/CHANGES.txt
  U   z3c.etestbrowser/trunk/src/z3c/etestbrowser/README.txt
  U   z3c.etestbrowser/trunk/src/z3c/etestbrowser/testing.py

-=-
Modified: z3c.etestbrowser/trunk/CHANGES.txt
===================================================================
--- z3c.etestbrowser/trunk/CHANGES.txt	2007-12-18 09:29:25 UTC (rev 82324)
+++ z3c.etestbrowser/trunk/CHANGES.txt	2007-12-18 09:31:16 UTC (rev 82325)
@@ -2,6 +2,12 @@
 CHANGES
 =======
 
+1.1 (unreleased)
+----------------
+
+- Added `pretty_print` function to allow somewhat pretty formatted output of
+  HTML.
+
 1.0.4 (2007-11-01)
 ------------------
 

Modified: z3c.etestbrowser/trunk/src/z3c/etestbrowser/README.txt
===================================================================
--- z3c.etestbrowser/trunk/src/z3c/etestbrowser/README.txt	2007-12-18 09:29:25 UTC (rev 82324)
+++ z3c.etestbrowser/trunk/src/z3c/etestbrowser/README.txt	2007-12-18 09:31:16 UTC (rev 82325)
@@ -13,14 +13,14 @@
  - lxml
 
 
-EtreeTestBrowser
-================
+etree support
+=============
 
-EtreeTestBrowser parses the result of a request into an etree using lxml (if
-it's text/html or text/xml).
+The extended test browser allows parsing of the result of a request into an
+etree using lxml (if the content type is text/html or text/xml).
 
-Useful to perform more detailed analysis of web pages using e.g. XPath and
-related XML technologies.
+This is useful to perform more detailed analysis of web pages using e.g. XPath
+and related XML technologies.
 
 Example::
 
@@ -38,7 +38,7 @@
 
 
 Strict XML
-==========
+----------
 
 It is possible to force the test browser to use the xml parser::
 
@@ -53,7 +53,7 @@
   [<Element {http://www.w3.org/1999/xhtml}body at ...>]
 
 LXML unicode support
-====================
+--------------------
 
 A couple of variations of libxml2 might interpret UTF-8 encoded strings
 incorrectly. We have a workaround for that. Let's have a look at a view that
@@ -63,3 +63,24 @@
   >>> browser.open('http://localhost/lxml.html')
   >>> browser.etree.xpath("//span")[0].text
   u'K\xfcgelblitz.'
+
+
+Pretty printing
+===============
+
+Sometimes a normal `print` of the browsers contents is hard to read for
+debugging:
+
+  >>> browser.open('http://localhost/')
+  >>> print browser.contents
+  <!DOCTYPE html ...
+    ...Name...Title...Created...Modified...
+
+The extended test browser provides a method to provide a formatted version of
+the HTML (using htmllib internally):
+
+  >>> browser.pretty_print()
+  @import url(http://localhost/@@/zope3_tablelayout.css); User: Fallback
+  unauthenticated principal [Login][1] (image)[2] Location:...[top][3] /
+  Navigation
+  Loading... ... Name Title Created Modified ...

Modified: z3c.etestbrowser/trunk/src/z3c/etestbrowser/testing.py
===================================================================
--- z3c.etestbrowser/trunk/src/z3c/etestbrowser/testing.py	2007-12-18 09:29:25 UTC (rev 82324)
+++ z3c.etestbrowser/trunk/src/z3c/etestbrowser/testing.py	2007-12-18 09:31:16 UTC (rev 82325)
@@ -18,6 +18,9 @@
 
 import re
 import StringIO
+import htmllib
+import formatter
+
 import lxml.etree
 
 import zope.testbrowser.testing
@@ -65,3 +68,16 @@
     def _changed(self):
         super(ExtendedTestBrowser, self)._changed()
         self._etree = None
+
+    def pretty_print(self):
+        """Print a pretty (formatted) version of the HTML content.
+
+        If the content is not text/html then it is just printed.
+        """
+        if not self.headers['content-type'].lower().startswith('text/html'):
+            print self.contents
+        else:
+            parser = htmllib.HTMLParser(
+                formatter.AbstractFormatter(formatter.DumbWriter()))
+            parser.feed(self.contents)
+            parser.close()



More information about the Checkins mailing list