[Checkins] SVN: z3c.pt/trunk/ - Treat comments, processing instructions, and named entities in the

Chris McDonough chrism at plope.com
Thu Aug 7 18:47:18 EDT 2008


Log message for revision 89522:
  - Treat comments, processing instructions, and named entities in the
    source template as "literals", which will be rendered into the
    output unchanged. [chrism]
  
  
  

Changed:
  U   z3c.pt/trunk/CHANGES.txt
  U   z3c.pt/trunk/src/z3c/pt/translation.py
  U   z3c.pt/trunk/src/z3c/pt/translation.txt

-=-
Modified: z3c.pt/trunk/CHANGES.txt
===================================================================
--- z3c.pt/trunk/CHANGES.txt	2008-08-07 22:24:18 UTC (rev 89521)
+++ z3c.pt/trunk/CHANGES.txt	2008-08-07 22:47:17 UTC (rev 89522)
@@ -4,6 +4,10 @@
 Version 0.9.x
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
+- Treat comments, processing instructions, and named entities in the
+  source template as "literals", which will be rendered into the
+  output unchanged. [chrism]
+
 Version 0.9 - August 7, 2008
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

Modified: z3c.pt/trunk/src/z3c/pt/translation.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/translation.py	2008-08-07 22:24:18 UTC (rev 89521)
+++ z3c.pt/trunk/src/z3c/pt/translation.py	2008-08-07 22:47:17 UTC (rev 89522)
@@ -54,8 +54,8 @@
             return
 
         for element in self:
-            if isinstance(element, lxml.etree._Comment):
-                self._wrap_comment(element)
+            if not isinstance(element, Element):
+                self._wrap_literal(element)
 
         self.update()
         self.begin()
@@ -128,7 +128,7 @@
 
         # Step 2: Process "py:match" macros
         for element in self:
-            if element.py_match is None:
+            if getattr(element, 'py_match', None) is None:
                 continue
             
             nsmap = element.nsmap.copy()
@@ -267,7 +267,7 @@
             else:
                 _.append(tag)
 
-        # tag text (if we're not replacing tag body)
+        # tag text (if we're not re.placing tag body)
         if self.text and not dynamic:
             _.append(clauses.Out(self.text.encode('utf-8')))
 
@@ -346,21 +346,19 @@
 
         return _
 
-    def _wrap_comment(self, element):
+    def _wrap_literal(self, element):
         index = self.index(element)
 
-        t = parser.makeelement(utils.tal_attr('comment'))
+        t = parser.makeelement(utils.tal_attr('literal'))
         t.attrib['omit-tag'] = ''
         t.tail = element.tail
-        t.text = '<!--' + element.text + '-->'
-
+        t.text = unicode(element)
         for child in element.getchildren():
             t.append(child)
-
         self.remove(element)
         self.insert(index, t)
         t.update()
-    
+
     def _msgid(self):
         """Create an i18n msgid from the tag contents."""
 
@@ -574,7 +572,7 @@
 
 # set up namespaces for XML parsing
 lookup = lxml.etree.ElementNamespaceClassLookup()
-parser = lxml.etree.XMLParser()
+parser = lxml.etree.XMLParser(resolve_entities=False)
 parser.setElementClassLookup(lookup)
 
 try:

Modified: z3c.pt/trunk/src/z3c/pt/translation.txt
===================================================================
--- z3c.pt/trunk/src/z3c/pt/translation.txt	2008-08-07 22:24:18 UTC (rev 89521)
+++ z3c.pt/trunk/src/z3c/pt/translation.txt	2008-08-07 22:47:17 UTC (rev 89522)
@@ -51,6 +51,45 @@
       <img alt="La Peña" />
     </div>
 
+Literals
+--------
+
+:: Named entities output literally (note doctype is required to prevent
+   lxml from raising a XMLSyntaxError :-( )
+
+  >>> print render("""\
+  ... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
+  ...    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+  ... <html xmlns="http://www.w3.org/1999/xhtml">
+  ...   Hello &nbsp; World!
+  ... </html>""", translate_xml)
+    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+    <html>
+      Hello &nbsp; World!
+    </html>
+
+:: Processing instructions output literally
+
+  >>> print render("""\
+  ... <html xmlns="http://www.w3.org/1999/xhtml">
+  ...   <?xml-stylesheet href="classic.xsl" type="text/xml"?>
+  ...   Hello World!
+  ... </html>""", translate_xml)
+    <html>
+      <?xml-stylesheet href="classic.xsl" type="text/xml"?>
+      Hello World!
+    </html>
+
+:: Literal comments (without embedded expressions) output literally
+
+  >>> print render("""\
+  ... <html xmlns="http://www.w3.org/1999/xhtml">
+  ...   <!-- hello world -->
+  ... </html>""", translate_xml)
+    <html>
+      <!-- hello world -->
+    </html>
+
 Zope TAL
 --------
 



More information about the Checkins mailing list