[Checkins] SVN: Sandbox/malthe/chameleon.core/ Added support for XML declarations.

Malthe Borch mborch at gmail.com
Wed Nov 12 11:54:57 EST 2008


Log message for revision 92889:
  Added support for XML declarations.

Changed:
  U   Sandbox/malthe/chameleon.core/CHANGES.txt
  U   Sandbox/malthe/chameleon.core/src/chameleon/core/template.txt
  U   Sandbox/malthe/chameleon.core/src/chameleon/core/translation.py

-=-
Modified: Sandbox/malthe/chameleon.core/CHANGES.txt
===================================================================
--- Sandbox/malthe/chameleon.core/CHANGES.txt	2008-11-12 16:18:55 UTC (rev 92888)
+++ Sandbox/malthe/chameleon.core/CHANGES.txt	2008-11-12 16:54:57 UTC (rev 92889)
@@ -4,6 +4,9 @@
 HEAD
 ~~~~
 
+- XML declarations should be printed with or without a doctype
+  element, and only if the template includes it. [malthe]
+
 1.0b5 (released 12/11/2008)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

Modified: Sandbox/malthe/chameleon.core/src/chameleon/core/template.txt
===================================================================
--- Sandbox/malthe/chameleon.core/src/chameleon/core/template.txt	2008-11-12 16:18:55 UTC (rev 92888)
+++ Sandbox/malthe/chameleon.core/src/chameleon/core/template.txt	2008-11-12 16:54:57 UTC (rev 92889)
@@ -67,6 +67,19 @@
     </div>
   </div>
 
+XML-support
+-----------
+  
+  >>> print Template("""\
+  ... <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
+  ... <div xmlns="http://www.w3.org/1999/xhtml">
+  ...   Hello World!
+  ... </div>""", mock_parser)()
+  <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
+  <div xmlns="http://www.w3.org/1999/xhtml">
+    Hello World!
+  </div>  
+
 Error handling
 --------------
 

Modified: Sandbox/malthe/chameleon.core/src/chameleon/core/translation.py
===================================================================
--- Sandbox/malthe/chameleon.core/src/chameleon/core/translation.py	2008-11-12 16:18:55 UTC (rev 92888)
+++ Sandbox/malthe/chameleon.core/src/chameleon/core/translation.py	2008-11-12 16:54:57 UTC (rev 92889)
@@ -525,6 +525,7 @@
     doctype regardless of what the template defines."""
 
     doctype = None
+    xml_declaration = None
     implicit_doctype = ""
     
     def __init__(self, body, parser, implicit_doctype=None,
@@ -534,9 +535,12 @@
         # definitions; this represents a 'convention' over
         # 'configuration' approach to template documents
         no_doctype_declaration = '<!DOCTYPE' not in body
-        no_xml_declaration = '<?xml ' not in body
+        no_xml_declaration = not body.startswith('<?xml ')
         require_wrapping = no_xml_declaration and no_doctype_declaration
-        
+
+        if no_xml_declaration is False:
+            self.xml_declaration = body[:body.find('\n')+1]
+            
         # add default namespace declaration if no explicit document
         # type has been set
         if implicit_doctype and explicit_doctype is None and require_wrapping:
@@ -630,16 +634,22 @@
             (stream.symbols.out, stream.symbols.write, stream.symbols.scope) + \
             tuple(parameters)))
 
-        # output doctype if any
-        if self.doctype and not macro:
-            doctype = self.doctype + '\n'
-            if self.encoding:
-                doctype = doctype.encode(self.encoding)
-            out = clauses.Out(doctype)
-            stream.scope.append(set())
-            stream.begin([out])
-            stream.end([out])
-            stream.scope.pop()
+        # output XML headers, if applicable
+        if not macro:
+            header = ""
+            if self.xml_declaration is not None:
+                header += self.xml_declaration
+            if self.doctype:
+                doctype = self.doctype + '\n'
+                if self.encoding:
+                    doctype = doctype.encode(self.encoding)
+                header += doctype
+            if header:
+                out = clauses.Out(header)
+                stream.scope.append(set())
+                stream.begin([out])
+                stream.end([out])
+                stream.scope.pop()
 
         # start generation
         self.root.start(stream)



More information about the Checkins mailing list