[Checkins] SVN: z3c.rml/trunk/src/z3c/rml/ Implemented standalone page template support for z3c.rml. Now I am ready

Stephan Richter srichter at cosmos.phy.tufts.edu
Thu Mar 29 07:34:12 EDT 2007


Log message for revision 73904:
  Implemented standalone page template support for z3c.rml. Now I am ready 
  to work on an auto-generated RML Specification document (rendered as PDF, 
  of course). :-)
  
  

Changed:
  A   z3c.rml/trunk/src/z3c/rml/pagetemplate.py
  A   z3c.rml/trunk/src/z3c/rml/pagetemplate.txt
  A   z3c.rml/trunk/src/z3c/rml/tests/test_pagetemplate.py

-=-
Added: z3c.rml/trunk/src/z3c/rml/pagetemplate.py
===================================================================
--- z3c.rml/trunk/src/z3c/rml/pagetemplate.py	2007-03-29 11:33:29 UTC (rev 73903)
+++ z3c.rml/trunk/src/z3c/rml/pagetemplate.py	2007-03-29 11:34:12 UTC (rev 73904)
@@ -0,0 +1,52 @@
+##############################################################################
+#
+# Copyright (c) 2007 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Page Template Support
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+import zope
+from z3c.rml import rml2pdf
+
+try:
+    import zope.pagetemplate.pagetemplatefile
+except ImportError:
+    # zope.pagetemplate package has not been installed
+    import types
+    zope.pagetemplate = types.ModuleType('barcode')
+    zope.pagetemplate.pagetemplatefile = types.ModuleType('barcode')
+    zope.pagetemplate.pagetemplatefile.PageTemplateFile = object
+
+
+class RMLPageTemplateFile(zope.pagetemplate.pagetemplatefile.PageTemplateFile):
+
+    def pt_getContext(self, args=(), options=None, **ignore):
+        rval = {'template': self,
+                'args': args,
+                'nothing': None,
+                'context': options
+                }
+        rval.update(self.pt_getEngine().getBaseNames())
+        return rval
+
+    def __call__(self, *args, **kwargs):
+        rml = super(RMLPageTemplateFile, self).__call__(*args, **kwargs)
+
+        # RML is a unicode string, but oftentimes documents declare their
+        # encoding using <?xml ...>. Unfortuantely, I cannot tell lxml to
+        # ignore that directive. Thus we remove it.
+        if rml.startswith('<?xml'):
+            rml = rml.split('\n', 1)[-1]
+
+        return rml2pdf.parseString(rml).getvalue()


Property changes on: z3c.rml/trunk/src/z3c/rml/pagetemplate.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: z3c.rml/trunk/src/z3c/rml/pagetemplate.txt
===================================================================
--- z3c.rml/trunk/src/z3c/rml/pagetemplate.txt	2007-03-29 11:33:29 UTC (rev 73903)
+++ z3c.rml/trunk/src/z3c/rml/pagetemplate.txt	2007-03-29 11:34:12 UTC (rev 73904)
@@ -0,0 +1,54 @@
+==================
+RML Page Templates
+==================
+
+This package also provides optional support a helper class to use page
+templates with RML without using the entire Zope framework. This document will
+demonstrate how to use page templates and RML together.
+
+In this example, we will simply iterate through a list of names and display
+them in the PDF.
+
+The first step is to create a page template:
+
+  >>> import tempfile
+  >>> ptFileName = tempfile.mktemp('.pt')
+  >>> open(ptFileName, 'w').write('''\
+  ... <?xml version="1.0" encoding="UTF-8" ?>
+  ... <!DOCTYPE document SYSTEM "rml.dtd">
+  ... <document filename="template.pdf"
+  ...     xmlns:tal="http://xml.zope.org/namespaces/tal">
+  ...
+  ...   <template pageSize="(21cm, 29cm)">
+  ...     <pageTemplate id="main">
+  ...       <frame id="main" x1="2cm" y1="2cm"
+  ...              width="17cm" height="25cm" />
+  ...     </pageTemplate>
+  ...   </template>
+  ...
+  ...   <story>
+  ...     <para
+  ...         tal:repeat="name context/names"
+  ...         tal:content="name" />
+  ...   </story>
+  ...
+  ... </document>
+  ... ''')
+
+The ``context`` namespace will be created during rendering. I get back to this
+later. In th enext step we instantiate the page template:
+
+  >>> from z3c.rml import pagetemplate
+  >>> rmlPageTemplate = pagetemplate.RMLPageTemplateFile(ptFileName)
+
+All we have to do now is to render the template. The context of the template
+is effectively the keyword arguments dictionary:
+
+  >>> rmlPageTemplate(names=(u'Roy', u'Daniel', u'Julian', u'Stephan'))
+  '%PDF-1.3...'
+
+You can uncomment the following line to write out the PDF in the current
+working directory:
+
+  #>>> open('pagetemplate-test.pdf', 'w').write(
+  #...   rmlPageTemplate(names=(u'Roy', u'Daniel', u'Julian', u'Stephan')))


Property changes on: z3c.rml/trunk/src/z3c/rml/pagetemplate.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.rml/trunk/src/z3c/rml/tests/test_pagetemplate.py
===================================================================
--- z3c.rml/trunk/src/z3c/rml/tests/test_pagetemplate.py	2007-03-29 11:33:29 UTC (rev 73903)
+++ z3c.rml/trunk/src/z3c/rml/tests/test_pagetemplate.py	2007-03-29 11:34:12 UTC (rev 73904)
@@ -0,0 +1,27 @@
+# -*- coding: utf-8 -*-
+###############################################################################
+#
+# Copyright (c) 2005 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+###############################################################################
+"""Tests for the Book Documentation Module
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+import unittest
+from zope.testing import doctest
+
+def test_suite():
+    return unittest.TestSuite((
+        doctest.DocFileSuite('../pagetemplate.txt',
+            optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS),
+        ))


Property changes on: z3c.rml/trunk/src/z3c/rml/tests/test_pagetemplate.py
___________________________________________________________________
Name: svn:keywords
   + Id



More information about the Checkins mailing list