work in progress: new package docgen for document generation

This commit is contained in:
Helmut Merz 2012-01-27 07:51:53 +01:00
parent 505015e791
commit 3dc405d347
6 changed files with 185 additions and 0 deletions

10
docgen/README.txt Normal file
View file

@ -0,0 +1,10 @@
================================================================
docgen - Document Generation from Result Sets and XML Structures
================================================================
>>> from zope import component
>>> from zope.publisher.browser import TestRequest
>>> from cybertools.docgen.base import WordDocument
>>> doc = WordDocument(None, TestRequest)

1
docgen/__init__.py Normal file
View file

@ -0,0 +1 @@
# package

55
docgen/base.py Normal file
View file

@ -0,0 +1,55 @@
#
# Copyright (c) 2012 Helmut Merz helmutm@cy55.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""
View definitions for generation of documents.
"""
from zope.app.pagetemplate import ViewPageTemplateFile
from zope.cachedescriptors.property import Lazy
from zope.publisher.browser import BrowserPage
word_template = ViewPageTemplateFile('word_page.pt')
class Base(BrowserPage):
def __call__(self):
return self.index()
class WordDocument(Base):
index = word_template
def setHeader(self, data, filename='document'):
fn = '%s.doc' % filename
response = self.request.response
response.setHeader('Cache-Control', '')
response.setHeader('Pragma', '')
response.setHeader('Content-Type', 'application/msword;charset=utf-8')
response.setHeader('Content-Length', len(data))
response.setHeader('Content-Disposition', 'filename="%s"' % fn)
mswordXml = """<xml>
<w:WordDocument>
<w:View>Print</w:View>
<w:Zoom>90</w:Zoom>
<w:DoNotOptimizeForBrowser />
</w:WordDocument>
</xml>"""

26
docgen/tests.py Executable file
View file

@ -0,0 +1,26 @@
"""
unit tests, doc tests
"""
import unittest, doctest
from zope.testing.doctestunit import DocFileSuite
from zope.interface.verify import verifyClass
from zope.interface import implements
class Test(unittest.TestCase):
"Basic tests for the docgen package."
def testInterfaces(self):
pass
def test_suite():
flags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
return unittest.TestSuite((
unittest.makeSuite(Test),
DocFileSuite('README.txt', optionflags=flags),
))
if __name__ == '__main__':
unittest.main(defaultTest='test_suite')

70
docgen/word_macros.pt Normal file
View file

@ -0,0 +1,70 @@
<html>
<metal:example define-macro="example">
<div class="Section1">
<div align="center">
<h1>Title</h1>
</div>
<br /><br/>
<div style="font-size: 8pt">
<table cellpadding=5 cellspacing=0 width="100%" height=120 align="center">
<tr height=48 valign="bottom">
<td style="border-left: solid black; border-left-width : thin;
border-right: solid black; border-right-width : thin;
border-top: solid black; border-top-width : thin;
border-bottom: solid black; border-bottom-width : thin;
width=25%">
<i>Start date:</i>
<p><i><b> startDate</i></b></p></td>
</tr>
</table>
</div>
<h2 style="page-break-before: always">Index</h2>
<div>
<p><b>1. Introduction</b></p>
</div>
<p>For ...
<a style="mso-footnote-id:ftn1"
href="#_ftn1" name="_ftnref1" title=""><span
class="MsoFootnoteReference"><span
style="mso-special-character:footnote"></span></span></a>.</p>
<br clear=all
style="page-break-before:always;mso-break-type:section-break" />
</div><!-- section -->
<div class="Section2">
<h2>Annex 1: ...</h2>
<br clear=all style="page-break-before:always" />
</div><!-- section -->
<div style="mso-element:footnote-list">
<div style="mso-element:footnote" id="ftn1">
<p class="MsoFootnoteText"><a style="mso-footnote-id:ftn1"
href="#_ftnref1" name="_ftn1" title=""><span
class="MsoFootnoteReference"><span
style="mso-special-character:footnote"></span></span></a>
Footnote Text...</p></div>
<span><br clear=all style="mso-break-type:section-break" /></span>
<div class="Section3">
<div>
<div style="mso-element:header" id="h1">
<div class="MsoHeader"><h4 align="right">$refNumber</h4></div>
</div>
<div style="mso-element:footer" id="f1">
<div class="MsoFooter">Page
<span style="mso-element:field-begin"></span>PAGE<span
style="mso-element:field-end"></span>
</div>
</div>
</div>
</div> <!-- Section3 -->
</metal:example>
</html>

23
docgen/word_page.pt Normal file
View file

@ -0,0 +1,23 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:tal="http://xml.zope.org/namespaces/tal"
xmlns:metal="http://xml.zope.org/namespaces/metal"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<title xxtal:content="view/title">Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<xml xxtal:replace="structure view/mswordXml" />
<style type="text/css"
xxtal:content="view/css">
</style>
</head>
<body class="doc">
<div xxtal:replace="structure view/text">Body Text</div>
</body>
</html>