[Checkins] SVN: z3c.pt/trunk/src/z3c/pt/ Allow PageTemplate-derived instances to override the doctype specified in the template itself via its constructor.

Chris McDonough chrism at plope.com
Fri Aug 29 18:50:52 EDT 2008


Log message for revision 90602:
  Allow PageTemplate-derived instances to override the doctype specified in the template itself via its constructor.
  

Changed:
  A   z3c.pt/trunk/src/z3c/pt/doctypes.py
  U   z3c.pt/trunk/src/z3c/pt/pagetemplate.py
  U   z3c.pt/trunk/src/z3c/pt/template.py
  U   z3c.pt/trunk/src/z3c/pt/tests/test_edgecases.py
  U   z3c.pt/trunk/src/z3c/pt/translation.py

-=-
Added: z3c.pt/trunk/src/z3c/pt/doctypes.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/doctypes.py	                        (rev 0)
+++ z3c.pt/trunk/src/z3c/pt/doctypes.py	2008-08-29 22:50:51 UTC (rev 90602)
@@ -0,0 +1,13 @@
+def dt(name, pubid, system):
+    return '<!DOCTYPE %s PUBLIC "%s" "%s">' % (name, pubid, system)
+
+html_strict  = dt('HTML', '-//W3C//DTD HTML 4.01//EN',
+                  'http://www.w3.org/TR/html4/strict.dtd')
+html         = dt('HTML', '-//W3C//DTD HTML 4.01 Transitional//EN',
+                  'http://www.w3.org/TR/html4/loose.dtd')
+xhtml_strict = dt('html', '-//W3C//DTD XHTML 1.0 Strict//EN',
+                  'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd')
+xhtml        = dt('html', '-//W3C//DTD XHTML 1.0 Transitional//EN',
+                  'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd')
+no_doctype = ()
+


Property changes on: z3c.pt/trunk/src/z3c/pt/doctypes.py
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: z3c.pt/trunk/src/z3c/pt/pagetemplate.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/pagetemplate.py	2008-08-29 22:06:43 UTC (rev 90601)
+++ z3c.pt/trunk/src/z3c/pt/pagetemplate.py	2008-08-29 22:50:51 UTC (rev 90602)
@@ -1,7 +1,5 @@
 import zope.i18n
 
-import translation
-import generation
 import template
 import config
 import zpt
@@ -26,10 +24,10 @@
 class PageTemplate(template.BaseTemplate):
     __doc__ = template.BaseTemplate.__doc__ # for Sphinx autodoc
 
-    def __init__(self, body, parser=None):
+    def __init__(self, body, parser=None, format=None, doctype=None):
         if parser is None:
             parser = zpt.ZopePageTemplateParser
-        super(PageTemplate, self).__init__(body, parser)
+        super(PageTemplate, self).__init__(body, parser, format, doctype)
 
     def prepare(self, kwargs):
         super(PageTemplate, self).prepare(kwargs)
@@ -38,10 +36,12 @@
 class PageTemplateFile(template.BaseTemplateFile):
     __doc__ = template.BaseTemplateFile.__doc__ # for Sphinx autodoc
     
-    def __init__(self, filename, parser=None, **kwargs):
+    def __init__(self, filename, parser=None, format=None,
+                 doctype=None, **kwargs):
         if parser is None:
             parser = zpt.ZopePageTemplateParser
-        super(PageTemplateFile, self).__init__(filename, parser, **kwargs)
+        super(PageTemplateFile, self).__init__(filename, parser, format,
+                                               doctype, **kwargs)
 
     def prepare(self, kwargs):
         super(PageTemplateFile, self).prepare(kwargs)

Modified: z3c.pt/trunk/src/z3c/pt/template.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/template.py	2008-08-29 22:06:43 UTC (rev 90601)
+++ z3c.pt/trunk/src/z3c/pt/template.py	2008-08-29 22:50:51 UTC (rev 90602)
@@ -17,13 +17,14 @@
 
     format = 'xml'
     
-    def __init__(self, body, parser, format=None):
+    def __init__(self, body, parser, format=None, doctype=None):
         self.body = body
         self.parser = parser        
         self.signature = hash(body)
         self.registry = {}
         if format is not None:
             self.format = format
+        self.doctype = doctype
             
     @property
     def translate(self):
@@ -35,13 +36,13 @@
 
     @property
     def compiler(self):
-        return self.compilers[self.format](self.body, self.parser)
+        return self.compilers[self.format](self.body, self.parser, self.doctype)
     
     def cook(self, **kwargs):
         return self.compiler(**kwargs)
     
     def cook_check(self, macro, params):
-        key = self.signature, macro, params
+        key = self.signature, macro, params, self.doctype
         template = self.registry.get(key, None)
         if template is None:
             template = self.cook(macro=macro, params=params)
@@ -73,9 +74,10 @@
     
     global_registry = {}
     
-    def __init__(self, filename, parser, format=None, auto_reload=False):
+    def __init__(self, filename, parser, format=None,  doctype=None,
+                 auto_reload=False):
         BaseTemplate.__init__(
-            self, None, parser, format=format)
+            self, None, parser, format=format, doctype=doctype)
 
         self.auto_reload = auto_reload
         self.filename = filename = os.path.abspath(
@@ -97,7 +99,8 @@
         
     def clone(self, filename, format=None):
         cls = type(self)
-        return cls(filename, self.parser, format=format)
+        return cls(filename, self.parser, format=format,
+                   doctype=self.doctype, auto_reload=self.auto_reload)
         
     def _get_filename(self):
         return getattr(self, '_filename', None)

Modified: z3c.pt/trunk/src/z3c/pt/tests/test_edgecases.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/tests/test_edgecases.py	2008-08-29 22:06:43 UTC (rev 90601)
+++ z3c.pt/trunk/src/z3c/pt/tests/test_edgecases.py	2008-08-29 22:50:51 UTC (rev 90602)
@@ -112,6 +112,72 @@
         new = pickle.loads(dumped)
         self.assertEqual(len(actions), len(new))
 
+class TestExplicitDoctypes(unittest.TestCase, PlacelessSetup):
+    def setUp(self):
+        PlacelessSetup.setUp(self)
+
+    def tearDown(self):
+        PlacelessSetup.tearDown(self)
+
+    def test_doctype_declared_in_constructor_adds_doctype(self):
+        import z3c.pt
+        from zope.configuration import xmlconfig
+        xmlconfig.file('configure.zcml', z3c.pt)
+        from z3c.pt.pagetemplate import PageTemplate
+        from z3c.pt import doctypes
+        body = """\
+        <html xmlns="http://www.w3.org/1999/xhtml">
+        </html>
+        """
+        expected = """\
+        <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN"
+        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+        <html>
+        </html>"""
+        t = PageTemplate(body, doctype=doctypes.xhtml_strict)
+        self.assertEqual(norm(t.render()), norm(expected))
+
+    def test_doctype_declared_in_constructor_overrides_template_doctype(self):
+        import z3c.pt
+        from zope.configuration import xmlconfig
+        xmlconfig.file('configure.zcml', z3c.pt)
+        from z3c.pt.pagetemplate import PageTemplate
+        from z3c.pt import doctypes
+        body = """\
+        <!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">
+        </html>
+        """
+        expected = """\
+        <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN"
+        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+        <html>
+        </html>"""
+        t = PageTemplate(body, doctype=doctypes.xhtml_strict)
+        self.assertEqual(norm(t.render()), norm(expected))
+
+    def test_doctype_assigned_to_instance_overrides_constructor_doctype(self):
+        import z3c.pt
+        from zope.configuration import xmlconfig
+        xmlconfig.file('configure.zcml', z3c.pt)
+        from z3c.pt.pagetemplate import PageTemplate
+        from z3c.pt import doctypes
+        body = """\
+        <!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">
+        </html>
+        """
+        expected = """\
+        <!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML4.01 Transitional//EN"
+        "http://www.w3.org/TR/html4/loose.dtd">
+        <html>
+        </html>"""
+        t = PageTemplate(body, doctype=doctypes.xhtml_strict)
+        t.doctype = doctypes.html
+        self.assertEqual(norm(t.render()), norm(expected))
+
 def norm(s):
     return s.replace(' ', '').replace('\n', '')
 

Modified: z3c.pt/trunk/src/z3c/pt/translation.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/translation.py	2008-08-29 22:06:43 UTC (rev 90601)
+++ z3c.pt/trunk/src/z3c/pt/translation.py	2008-08-29 22:50:51 UTC (rev 90602)
@@ -1,12 +1,8 @@
-from zope import component
-
 from StringIO import StringIO
 
 import generation
 import codegen
 import clauses
-import interfaces
-import expressions
 import itertools
 import types
 import utils
@@ -431,14 +427,16 @@
 class Compiler(object):
     """Template compiler."""
     
-    def __init__(self, body, parser):
-        self.root, self.doctype = parser.parse(body)
+    def __init__(self, body, parser, doctype=None):
+        self.root, parsed_doctype = parser.parse(body)
+        self.doctype = doctype or parsed_doctype
         self.parser = parser
 
     @classmethod
-    def from_text(cls, body, parser):
+    def from_text(cls, body, parser, doctype=None):
         compiler = Compiler(
-            "<html xmlns='%s'></html>" % config.XHTML_NS, parser)
+            "<html xmlns='%s'></html>" % config.XHTML_NS, parser,
+            doctype=doctype)
         compiler.root.text = body
         compiler.root.attrib[utils.meta_attr('omit-tag')] = ""
         return compiler



More information about the Checkins mailing list