[Lxml-checkins] r49910 - in lxml/trunk: . doc src/lxml src/lxml/tests

scoder at codespeak.net scoder at codespeak.net
Tue Dec 18 22:29:10 CET 2007


Author: scoder
Date: Tue Dec 18 22:29:09 2007
New Revision: 49910

Modified:
   lxml/trunk/   (props changed)
   lxml/trunk/CHANGES.txt
   lxml/trunk/doc/api.txt
   lxml/trunk/doc/elementsoup.txt
   lxml/trunk/doc/parsing.txt
   lxml/trunk/doc/tutorial.txt
   lxml/trunk/src/lxml/serializer.pxi
   lxml/trunk/src/lxml/tests/test_etree.py
   lxml/trunk/src/lxml/tests/test_htmlparser.py
Log:
 r3107 at delle:  sbehnel | 2007-12-18 19:01:53 +0100
 always append a newline when pretty printing on serialisation (not only for ElementTrees)


Modified: lxml/trunk/CHANGES.txt
==============================================================================
--- lxml/trunk/CHANGES.txt	(original)
+++ lxml/trunk/CHANGES.txt	Tue Dec 18 22:29:09 2007
@@ -31,8 +31,10 @@
   method, which should be used in new code wherever possible.
 
 * The 'pretty printed' serialisation of ElementTree objects now
-  appends a newline at the end of the document and also inserts
-  newlines between the top-level processing instructions and comments
+  inserts newlines at the root level between processing instructions,
+  comments and the root tag.
+
+* A 'pretty printed' serialisation is now terminated with a newline.
 
 * Second argument to ``lxml.etree.Extension()`` helper is no longer
   required, third argument is now a keyword-only argument ``ns``.

Modified: lxml/trunk/doc/api.txt
==============================================================================
--- lxml/trunk/doc/api.txt	(original)
+++ lxml/trunk/doc/api.txt	Tue Dec 18 22:29:09 2007
@@ -266,11 +266,14 @@
   >>> print etree.tostring(root)
   <root><test/></root>
 
-  >>> print etree.tostring(root, pretty_print=True)
+  >>> print etree.tostring(root, pretty_print=True),
   <root>
     <test/>
   </root>
 
+Note the newline that is appended at the end when pretty printing the
+output.  It was added in lxml 2.0.
+
 By default, lxml (just as ElementTree) outputs the XML declaration only if it
 is required by the standard::
 

Modified: lxml/trunk/doc/elementsoup.txt
==============================================================================
--- lxml/trunk/doc/elementsoup.txt	(original)
+++ lxml/trunk/doc/elementsoup.txt	Tue Dec 18 22:29:09 2007
@@ -30,7 +30,7 @@
 To see what we have here, you can serialise it::
 
     >>> from lxml.etree import tostring
-    >>> print tostring(root, pretty_print=True)
+    >>> print tostring(root, pretty_print=True),
     <html>
       <meta/>
       <head>

Modified: lxml/trunk/doc/parsing.txt
==============================================================================
--- lxml/trunk/doc/parsing.txt	(original)
+++ lxml/trunk/doc/parsing.txt	Tue Dec 18 22:29:09 2007
@@ -139,7 +139,7 @@
   >>> parser = etree.HTMLParser()
   >>> tree   = etree.parse(StringIO(broken_html), parser)
 
-  >>> print etree.tostring(tree.getroot(), pretty_print=True)
+  >>> print etree.tostring(tree.getroot(), pretty_print=True),
   <html>
     <head>
       <title>test</title>
@@ -153,7 +153,7 @@
 ElementTree::
 
   >>> html = etree.HTML(broken_html)
-  >>> print etree.tostring(html, pretty_print=True)
+  >>> print etree.tostring(html, pretty_print=True),
   <html>
     <head>
       <title>test</title>

Modified: lxml/trunk/doc/tutorial.txt
==============================================================================
--- lxml/trunk/doc/tutorial.txt	(original)
+++ lxml/trunk/doc/tutorial.txt	Tue Dec 18 22:29:09 2007
@@ -104,7 +104,7 @@
 
 To see that this is really XML, you can serialise the tree you have created::
 
-    >>> print etree.tostring(root, pretty_print=True)
+    >>> print etree.tostring(root, pretty_print=True),
     <root>
       <child1/>
       <child2/>
@@ -306,7 +306,7 @@
     >>> etree.SubElement(root, "child").text = "Child 2"
     >>> etree.SubElement(root, "another").text = "Child 3"
 
-    >>> print etree.tostring(root, pretty_print=True)
+    >>> print etree.tostring(root, pretty_print=True),
     <root>
       <child>Child 1</child>
       <child>Child 2</child>
@@ -337,10 +337,10 @@
 Serialisation
 -------------
 
-Serialisation commonly uses with the ``tostring()`` function that
-returns a string, or the ``ElementTree.write()`` method that writes to
-a file or file-like object.  Both accept the same keyword arguments
-like ``pretty_print`` for formatted output or ``encoding`` to select a
+Serialisation commonly uses the ``tostring()`` function that returns a
+string, or the ``ElementTree.write()`` method that writes to a file or
+file-like object.  Both accept the same keyword arguments like
+``pretty_print`` for formatted output or ``encoding`` to select a
 specific output encoding other than plain ASCII::
 
    >>> root = etree.XML('<root><a><b/></a></root>')
@@ -356,13 +356,16 @@
    <?xml version='1.0' encoding='iso-8859-1'?>
    <root><a><b/></a></root>
 
-   >>> print etree.tostring(root, pretty_print=True)
+   >>> print etree.tostring(root, pretty_print=True),
    <root>
      <a>
        <b/>
      </a>
    </root>
 
+Note the newline that is appended at the end when pretty printing the
+output.
+
 Since lxml 2.0 (and ElementTree 1.3), the serialisation functions can
 do more than XML serialisation.  You can serialise to HTML or extract
 the text content by passing the ``method`` keyword::
@@ -378,7 +381,7 @@
    >>> print etree.tostring(root, method='html')
    <html><head></head><body><p>Hello<br>World</p></body></html>
 
-   >>> print etree.tostring(root, method='html', pretty_print=True)
+   >>> print etree.tostring(root, method='html', pretty_print=True),
    <html>
    <head></head>
    <body><p>Hello<br>World</p></body>
@@ -657,7 +660,7 @@
     >>> body = etree.SubElement(xhtml, "{http://www.w3.org/1999/xhtml}body")
     >>> body.text = "Hello World"
 
-    >>> print etree.tostring(xhtml, pretty_print=True)
+    >>> print etree.tostring(xhtml, pretty_print=True),
     <html:html xmlns:html="http://www.w3.org/1999/xhtml">
       <html:body>Hello World</html:body>
     </html:html>
@@ -680,7 +683,7 @@
     >>> body = etree.SubElement(xhtml, XHTML + "body")
     >>> body.text = "Hello World"
 
-    >>> print etree.tostring(xhtml, pretty_print=True)
+    >>> print etree.tostring(xhtml, pretty_print=True),
     <html xmlns="http://www.w3.org/1999/xhtml">
       <body>Hello World</body>
     </html>
@@ -689,7 +692,7 @@
 
     >>> body.set(XHTML + "bgcolor", "#CCFFAA")
 
-    >>> print etree.tostring(xhtml, pretty_print=True)
+    >>> print etree.tostring(xhtml, pretty_print=True),
     <html xmlns="http://www.w3.org/1999/xhtml">
       <body bgcolor="#CCFFAA">Hello World</body>
     </html>
@@ -736,7 +739,7 @@
     ...   )
     ... )
 
-    >>> print etree.tostring(page, pretty_print=True)
+    >>> print etree.tostring(page, pretty_print=True),
     <html>
       <head>
         <title>This is a sample document</title>
@@ -777,7 +780,7 @@
     ...   )
     ... )
 
-    >>> print etree.tostring(my_doc, pretty_print=True)
+    >>> print etree.tostring(my_doc, pretty_print=True),
     <p:doc xmlns:p="http://my.de/fault/namespace">
       <p:title>The dog and the hog</p:title>
       <p:section>

Modified: lxml/trunk/src/lxml/serializer.pxi
==============================================================================
--- lxml/trunk/src/lxml/serializer.pxi	(original)
+++ lxml/trunk/src/lxml/serializer.pxi	Tue Dec 18 22:29:09 2007
@@ -172,8 +172,8 @@
     _writeTail(c_buffer, c_node, encoding, pretty_print)
     if write_complete_document:
         _writeNextSiblings(c_buffer, c_node, encoding, pretty_print)
-        if pretty_print:
-            tree.xmlOutputBufferWriteString(c_buffer, "\n")
+    if pretty_print:
+        tree.xmlOutputBufferWriteString(c_buffer, "\n")
 
 cdef void _writeDeclarationToBuffer(tree.xmlOutputBuffer* c_buffer,
                                     char* version, char* encoding) nogil:

Modified: lxml/trunk/src/lxml/tests/test_etree.py
==============================================================================
--- lxml/trunk/src/lxml/tests/test_etree.py	(original)
+++ lxml/trunk/src/lxml/tests/test_etree.py	Tue Dec 18 22:29:09 2007
@@ -1904,7 +1904,7 @@
         self.assertEquals(result, "<a><b/><c/></a>")
 
         result = tostring(a, pretty_print=True)
-        self.assertEquals(result, "<a>\n  <b/>\n  <c/>\n</a>")
+        self.assertEquals(result, "<a>\n  <b/>\n  <c/>\n</a>\n")
 
     def test_tostring_method_text_encoding(self):
         tostring = self.etree.tostring
@@ -1989,7 +1989,7 @@
         self.assertEquals(result, "<a><b/><c/></a>")
 
         result = tounicode(a, pretty_print=True)
-        self.assertEquals(result, "<a>\n  <b/>\n  <c/>\n</a>")
+        self.assertEquals(result, "<a>\n  <b/>\n  <c/>\n</a>\n")
 
     def _writeElement(self, element, encoding='us-ascii'):
         """Write out element for comparison.

Modified: lxml/trunk/src/lxml/tests/test_htmlparser.py
==============================================================================
--- lxml/trunk/src/lxml/tests/test_htmlparser.py	(original)
+++ lxml/trunk/src/lxml/tests/test_htmlparser.py	Tue Dec 18 22:29:09 2007
@@ -20,7 +20,8 @@
 <html>
 <head><title>test</title></head>
 <body><h1>page title</h1></body>
-</html>"""
+</html>
+"""
     broken_html_str = "<html><head><title>test<body><h1>page title</h3></p></html>"
     uhtml_str = u"<html><head><title>test á\uF8D2</title></head><body><h1>page á\uF8D2 title</h1></body></html>"
 


More information about the lxml-checkins mailing list