Commit 3ae9251f authored by Arnaud Fontaine's avatar Arnaud Fontaine

PortalTransforms: Prepare migration to Components.

Remove unused source code directories and files:
  + unsafe_transforms/
  + tests/
  + profiles/
  + libtransforms/piltransform.py
  + libtransforms/zope27rest.py
  + setuphandlers.py

TransformEngine.py only contains TransformTool class (and TransformTool.py is
just importing it) so rename that file to TransformTool. Also, cache module
only defines Cache class and this is only used in TransformTool so move it to
TransformTool module.
parent bd2f51c9
...@@ -19,7 +19,6 @@ from Products.PageTemplates.PageTemplateFile import PageTemplateFile ...@@ -19,7 +19,6 @@ from Products.PageTemplates.PageTemplateFile import PageTemplateFile
from Products.PortalTransforms.data import datastream from Products.PortalTransforms.data import datastream
from Products.PortalTransforms.chain import TransformsChain from Products.PortalTransforms.chain import TransformsChain
from Products.PortalTransforms.chain import chain from Products.PortalTransforms.chain import chain
from Products.PortalTransforms.cache import Cache
from Products.PortalTransforms.interfaces import IDataStream from Products.PortalTransforms.interfaces import IDataStream
from Products.PortalTransforms.interfaces import ITransform from Products.PortalTransforms.interfaces import ITransform
from Products.PortalTransforms.interfaces import IEngine from Products.PortalTransforms.interfaces import IEngine
...@@ -36,6 +35,76 @@ from Products.PortalTransforms.utils import parseContentType ...@@ -36,6 +35,76 @@ from Products.PortalTransforms.utils import parseContentType
from ZODB.POSException import ConflictError from ZODB.POSException import ConflictError
from zLOG import WARNING from zLOG import WARNING
from time import time
from Acquisition import aq_base
_marker = object()
class Cache:
def __init__(self, obj, context=None, _id='_v_transform_cache'):
self.obj = obj
if context is None:
self.context = obj
else:
self.context = context
self._id =_id
def _genCacheKey(self, identifier, *args):
key = identifier
for arg in args:
key = '%s_%s' % (key, arg)
key = key.replace('/', '_')
key = key.replace('+', '_')
key = key.replace('-', '_')
key = key.replace(' ', '_')
if hasattr(aq_base(self.context), 'absolute_url'):
return key, self.context.absolute_url()
return key
def setCache(self, key, value):
"""cache a value indexed by key"""
if not value.isCacheable():
return
obj = self.obj
key = self._genCacheKey(key)
entry = getattr(aq_base(obj), self._id, None)
if entry is None:
entry = {}
setattr(obj, self._id, entry)
entry[key] = (time(), value)
return key
def getCache(self, key):
"""try to get a cached value for key
return None if not present
else return a tuple (time spent in cache, value)
"""
obj = self.obj
key = self._genCacheKey(key)
dict = getattr(obj, self._id, None)
if dict is None :
return None
try:
orig_time, value = dict.get(key, None)
return time() - orig_time, value
except TypeError:
return None
def purgeCache(self, key=None):
"""Remove cache
"""
obj = self.obj
id = self._id
if getattr(obj, id, _marker) is _marker:
return
if key is None:
delattr(obj, id)
else:
cache = getattr(obj, id)
key = self._genCacheKey(key)
if cache.has_key(key):
del cache[key]
class TransformTool(UniqueObject, ActionProviderBase, Folder): class TransformTool(UniqueObject, ActionProviderBase, Folder):
id = 'portal_transforms' id = 'portal_transforms'
......
from Products.PortalTransforms.TransformEngine import TransformTool
from Products.PortalTransforms.TransformEngine import TransformTool from Products.PortalTransforms.Tool.TransformTool import TransformTool
PKG_NAME = 'PortalTransforms' PKG_NAME = 'PortalTransforms'
......
"""Cache
"""
from time import time
from Acquisition import aq_base
_marker = object()
class Cache:
def __init__(self, obj, context=None, _id='_v_transform_cache'):
self.obj = obj
if context is None:
self.context = obj
else:
self.context = context
self._id =_id
def _genCacheKey(self, identifier, *args):
key = identifier
for arg in args:
key = '%s_%s' % (key, arg)
key = key.replace('/', '_')
key = key.replace('+', '_')
key = key.replace('-', '_')
key = key.replace(' ', '_')
if hasattr(aq_base(self.context), 'absolute_url'):
return key, self.context.absolute_url()
return key
def setCache(self, key, value):
"""cache a value indexed by key"""
if not value.isCacheable():
return
obj = self.obj
key = self._genCacheKey(key)
entry = getattr(aq_base(obj), self._id, None)
if entry is None:
entry = {}
setattr(obj, self._id, entry)
entry[key] = (time(), value)
return key
def getCache(self, key):
"""try to get a cached value for key
return None if not present
else return a tuple (time spent in cache, value)
"""
obj = self.obj
key = self._genCacheKey(key)
dict = getattr(obj, self._id, None)
if dict is None :
return None
try:
orig_time, value = dict.get(key, None)
return time() - orig_time, value
except TypeError:
return None
def purgeCache(self, key=None):
"""Remove cache
"""
obj = self.obj
id = self._id
if getattr(obj, id, _marker) is _marker:
return
if key is None:
delattr(obj, id)
else:
cache = getattr(obj, id)
key = self._genCacheKey(key)
if cache.has_key(key):
del cache[key]
from Products.PortalTransforms.interfaces import ITransform
from zope.interface import implements
from StringIO import StringIO
import PIL.Image
class PILTransforms:
implements(ITransform)
__name__ = "piltransforms"
def __init__(self, name=None):
if name is not None:
self.__name__ = name
def name(self):
return self.__name__
def convert(self, orig, data, **kwargs):
imgio = StringIO()
orig = StringIO(orig)
newwidth = kwargs.get('width',None)
newheight = kwargs.get('height',None)
pil_img = PIL.Image.open(orig)
if(self.format in ['jpeg','ppm']):
pil_img.draft("RGB", pil_img.size)
pil_img = pil_img.convert("RGB")
if(newwidth or newheight):
pil_img.thumbnail((newwidth,newheight),PIL.Image.ANTIALIAS)
pil_img.save(imgio,self.format)
data.setData(imgio.getvalue())
return data
def register():
return PILTransforms()
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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
#
##############################################################################
"""Wrapper to integrate reStructuredText into Zope
This implementation requires docutils 0.3.4+ from http://docutils.sf.net/
Based on the new implementation of Zope 2.7.1 altered for PortalTransforms
"""
try:
import docutils
except ImportError:
raise ImportError, 'Please install docutils 0.3.3+ from http://docutils.sourceforge.net/#download.'
version = docutils.__version__.split('.')
if version < ['0', '3', '3']:
raise ImportError, """Old version of docutils found:
Got: %(version)s, required: 0.3.3+
Please remove docutils from %(path)s and replace it with a new version. You
can download docutils at http://docutils.sourceforge.net/#download.
""" % {'version' : docutils.__version__, 'path' : docutils.__path__[0] }
import sys, os, locale
##from App.config import getConfiguration
from docutils.core import publish_parts
# get encoding
##default_enc = sys.getdefaultencoding()
##default_output_encoding = getConfiguration().rest_output_encoding or default_enc
##default_input_encoding = getConfiguration().rest_input_encoding or default_enc
default_enc = 'utf-8'
default_output_encoding = default_enc
default_input_encoding = default_enc
# starting level for <H> elements (default behaviour inside Zope is <H3>)
default_level = 3
##initial_header_level = getConfiguration().rest_header_level or default_level
initial_header_level = default_level
# default language
##default_lang = getConfiguration().locale or locale.getdefaultlocale()[0]
default_lang = locale.getdefaultlocale()[0]
if default_lang and '_' in default_lang:
default_lang = default_lang[:default_lang.index('_')]
class Warnings:
def __init__(self):
self.messages = []
def write(self, message):
self.messages.append(message)
def render(src,
writer='html4css1',
report_level=1,
stylesheet='default.css',
input_encoding=default_input_encoding,
output_encoding=default_output_encoding,
language_code=default_lang,
initial_header_level = initial_header_level,
settings = {}):
"""get the rendered parts of the document the and warning object
"""
# Docutils settings:
settings = settings.copy()
settings['input_encoding'] = input_encoding
settings['output_encoding'] = output_encoding
settings['stylesheet'] = stylesheet
settings['language_code'] = language_code
# starting level for <H> elements:
settings['initial_header_level'] = initial_header_level + 1
# set the reporting level to something sane:
settings['report_level'] = report_level
# don't break if we get errors:
settings['halt_level'] = 6
# remember warnings:
settings['warning_stream'] = warning_stream = Warnings()
parts = publish_parts(source=src, writer_name=writer,
settings_overrides=settings,
config_section='zope application')
return parts, warning_stream
def HTML(src,
writer='html4css1',
report_level=1,
stylesheet='default.css',
input_encoding=default_input_encoding,
output_encoding=default_output_encoding,
language_code=default_lang,
initial_header_level = initial_header_level,
warnings = None,
settings = {}):
""" render HTML from a reStructuredText string
- 'src' -- string containing a valid reST document
- 'writer' -- docutils writer
- 'report_level' - verbosity of reST parser
- 'stylesheet' - Stylesheet to be used
- 'input_encoding' - encoding of the reST input string
- 'output_encoding' - encoding of the rendered HTML output
- 'report_level' - verbosity of reST parser
- 'language_code' - docutils language
- 'initial_header_level' - level of the first header tag
- 'warnings' - will be overwritten with a string containing the warnings
- 'settings' - dict of settings to pass in to Docutils, with priority
"""
parts, warning_stream = render(src,
writer = writer,
report_level = report_level,
stylesheet = stylesheet,
input_encoding = input_encoding,
output_encoding = output_encoding,
language_code=language_code,
initial_header_level = initial_header_level,
settings = settings)
header = '<h%(level)s class="title">%(title)s</h%(level)s>\n' % {
'level': initial_header_level,
'title': parts['title'],
}
body = '%(docinfo)s%(body)s' % {
'docinfo': parts['docinfo'],
'body': parts['body'],
}
if parts['title']:
output = header + body
else:
output = body
warnings = ''.join(warning_stream.messages)
return output.encode(output_encoding)
__all__ = ("HTML", 'render')
<?xml version="1.0"?>
<componentregistry>
<adapters/>
<utilities>
<utility
interface="Products.PortalTransforms.interfaces.IPortalTransformsTool"
object="portal_transforms"/>
</utilities>
</componentregistry>
<?xml version="1.0"?>
<import-steps>
<import-step id="portal-transforms-various" version="20070309-01"
handler="Products.PortalTransforms.setuphandlers.setupPortalTransforms"
title="PortalTransforms setup">
<dependency step="componentregistry"/>
PortalTransforms installation step.
</import-step>
</import-steps>
<?xml version="1.0"?>
<metadata>
<version>1.6</version>
</metadata>
<?xml version="1.0"?>
<tool-setup>
<required tool_id="portal_transforms"
class="Products.PortalTransforms.TransformEngine.TransformTool"/>
</tool-setup>
"""
PortalTransforms setup handlers.
"""
from StringIO import StringIO
from Products.CMFCore.utils import getToolByName
def correctMapping(out, portal):
pt = getToolByName(portal, 'portal_transforms')
pt_ids = pt.objectIds()
for m_in, m_out_dict in pt._mtmap.items():
for m_out, transforms in m_out_dict.items():
for transform in transforms:
if transform.id not in pt_ids:
#error, mapped transform is no object in portal_transforms. correct it!
print >>out, "have to unmap transform (%s) cause its not in portal_transforms ..." % transform.id
try:
pt._unmapTransform(transform)
except:
raise
else:
print >>out, "...ok"
def updateSafeHtml(out, portal):
print >>out, 'Update safe_html...'
safe_html_id = 'safe_html'
safe_html_module = "Products.PortalTransforms.transforms.safe_html"
pt = getToolByName(portal, 'portal_transforms')
for id in pt.objectIds():
transform = getattr(pt, id)
if transform.id == safe_html_id and transform.module == safe_html_module:
try:
disable_transform = transform.get_parameter_value('disable_transform')
except KeyError:
print >>out, ' replace safe_html (%s, %s) ...' % (transform.name(), transform.module)
try:
pt.unregisterTransform(id)
pt.manage_addTransform(id, safe_html_module)
except:
raise
else:
print >>out, ' ...done'
print >>out, '...done'
def installPortalTransforms(portal):
out = StringIO()
updateSafeHtml(out, portal)
correctMapping(out, portal)
def setupPortalTransforms(context):
"""
Setup PortalTransforms step.
"""
# Only run step if a flag file is present (e.g. not an extension profile)
if context.readDataFile('portal-transforms-various.txt') is None:
return
out = []
site = context.getSite()
installPortalTransforms(site)
h1. Textile test text
_This_ is quite *boring*, but it needs to be "done":http://plone.org, right?
h2. Cheeses
# Gouda
# Roquefort
# Emmentaler
h2. Episodes
* Bicycle Repairman
* Spanish Inquisition
* Fishslapping Dance
## Testing Markdown
`code` and _italic_ and *bold* and even a [link](http://plone.org).
Fööbär
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN" "http://my.netscape.com/publish/formats/rss-0.91.dtd">
<rss version="0.91"><channel><title>Logilab.org news</title><language>en</language><item><title>xmltools 1.3.7</title><descr>bugfix in namespace handling</descr></item><item><title>Python-logic</title><descr>Set up of the Python-Logic special interest group</descr></item><item><title>PyReverse 0.2.3</title><descr>New features and bug fixes</descr></item><item><title>xmltools 1.3.6</title><descr>Uses the new APIs in pyxml-0.7 and 4Suite-0.12.0</descr></item><item><title>hmm-0.2</title><descr>New learning algorithms available</descr></item><item><title>Version 1.2a1 is out</title><descr>Overall refactoring of the engine. Backward incompatible changes
in the syntax of recipes and in modules, in order to ease product development.</descr></item><item><title>XMLdiff v0.5.3 (bug fixes)</title><descr>Version 0.5.3 fixes packaging bugs.</descr></item><item><title>hmm-0.1</title><descr>hmm is a module for Hidden Markov Model manipulation.</descr></item><item><title>PyReverse 0.1 (new product)</title><descr>
Beta release for this set of tools for reverse engineering python code
</descr></item><item><title>PyPaSax 0.3 (bug fixes)</title><descr>A few changes in the DTD, improved PyXML compatibility</descr></item><item><title>XMLdiff v0.5.2 (bug fixes)</title><descr>Version 0.5.2 fixes several bugs.</descr></item><item><title>Version 1.1 is out</title><descr>bugfixes over beta 3.</descr></item><item><title>Version 1.1b3 is out</title><descr>Great speed improvement for Horn. All-in-one windows installer.</descr></item><item><title>xmltools-1.3.5</title><descr>Version 1.3.5 code cleanup.</descr></item><item><title>xmltools-1.3.4</title><descr>Version 1.3.4 fixes a sever encoding bug that could cause crashes on windows machines.</descr></item><item><title>Version 1.1b1 is out</title><descr>Version 1.1b1 drops support for Python 1.5.2 in favor of Python 2.1, and features a new version of Horn, with localization support</descr></item><item><title>XMLdiff v0.5 (algorithm change, bug fixes)</title><descr>Version 0.5. The new algorithm makes it now usable either on big
documents and really faster in any cases. Fixes Unicode problem.</descr></item><item><title>XMLtools v1.3.1 (bugfixes)</title><descr>Version 1.3.1. This release fixes some minor glitches that had slipped in 1.3.</descr></item><item><title>XMLdiff v0.2 (performance improvement)</title><descr>Version 0.2. Huge performance improvement, and output cleanup.</descr></item><item><title>XMLdiff v0.1.1 (beta release)</title><descr>Version 0.1.1. Fully functionnal. Beta release.</descr></item><item><title>XPathVis v1.0beta (beta release)</title><descr>Version 1.0beta. Works nicely.</descr></item><item><title>XMLtools v1.3 (new features)</title><descr>Version 1.3. This release is compatible with Python 2.x and Unicode. It is not guaranteed to work with Python 1.5.2.</descr></item><item><title>Narval on developerWorks</title><descr>An Introduction to Narval was published on developerWorks.</descr><link>http://www-106.ibm.com/developerworks/library/l-ai/</link></item><item><title>Version 1.0.1 is out</title><descr>Version 1.0.1 is a bugfix release.</descr></item><item><title>Narval reviewed on AI.About.com</title><descr>AI.About.com published a review of Narval.</descr><link>http://ai.about.com/compute/ai/library/weekly/aa060801a.htm</link></item><item><title>Narval at BotShow 2001</title><descr>Narval was presented at the first BotShow event. The slides will soon be available online.</descr><link>http://www.ptolemee.com/botshow/text/text_fr/edito/edito_set.html</link></item><item><title>Version 1.0 is out</title><descr>Version 1.0. Celebration time, come on!</descr></item><item><title>Network-boot-HOWTO v0.2.1</title><descr>Version 0.2.1 is out.</descr></item><item><title>GuessLang v0.1.0 (beta release)</title><descr>Version 0.1.0 is out.</descr></item><item><title>Network-boot-HOWTO v0.1.1</title><descr>Version 0.1.1 is out.</descr></item><item><title>PyPaSax v0.1</title><descr>Version 0.1 is out.</descr></item><item><title>RC2 is out</title><descr>Release Candidate 2 is out. French documentation will be updated within a few days. We also released several applications (or maybe extension sets?) that are in alpha/beta stage. Give them a try!</descr></item><item><title>VCalSax v0.1 (beta)</title><descr>Version 0.1 is out. Still beta, but fully functional.</descr></item><item><title>Talk at LinuxExpo in English</title><descr>A translation of the talk we gave at Linux Expo 2001 is available on-line.</descr><link>http://www.logilab.com/press/linux-expo2001/</link></item><item><title>RC1 is out</title><descr>Release Candidate 1 is out. Documentation will be updated within a few days. Please help us test this one so that we can release 1.0 quicker.</descr></item><item><title>XMLtools v1.2 (stable release)</title><descr>Version 1.2 is released. Bugfixes, mainly..</descr></item><item><title>Application section on web site</title><descr>We just added a new applications section on Logilab.org web site.</descr><link>http://www.logilab.org/narval/app.html</link></item><item><title>WMgMon v0.4.0</title><descr>version 0.4.0 is out. Bugfixes and new monitor functions. </descr></item><item><title>XmlTools v1.1</title><descr>version 1.1 is out. New features in XmlTree.</descr></item><item><title>Beta5 is out</title><descr>Beta 5 is out. Lots of bugfixes in Narval and Horn, client server communication between the kernel and the graphical interface using SOAP. Windows specific bugfixes.</descr></item><item><title>XmlTools v1.0</title><descr>Initial release.</descr></item><item><title>PyGantt v0.6.0</title><descr>Version 0.6.0 released. New features added.</descr></item><item><title>Beta4 is out</title><descr>Beta 4 is out. Improved Windows compatibility. New features and bugfixes in both Narval and Horn.</descr></item><item><title>Article on Narval in Linux Gazette</title><descr>We published an article in the #59 issue of the Linux Gazette. It describes Narval and its use to set up Gazo, the assistant-coordinator for the translation of the Linux Gazette.</descr><link>http://www.linuxgazette.com/issue59/chauvat.html</link></item><item><title>Beta3 is out</title><descr>Beta 3 is out. No more memory leaks (almost). Time conditions work correctly. A step can be an XSL transform. Changes in the Narval DTD.</descr></item><item><title>Logilab invited at Linux Expo</title><descr>We got invited to give a talk at Linux Expo in Paris, France. The talk will be geared toward business uses of Narval. The title will be Using XML and Intelligent Personnal Assistants to enhance groupware and workflow enterprise applications. Come and meet with us!</descr><link>http://www.linuxexpoparis.com/EN/conferences</link></item><item><title>Beta2 is out</title><descr>Beta 2 is out. Installation is much easier. Tutorial. GUI improvements. Bugfixes.</descr></item><item><title>Beta1 is out</title><descr>Beta 1 is out. Many bug fixes.</descr></item><item><title>Beta0 is out</title><descr>Beta 0 is out. Logilab.org is one-line.</descr><link>http://www.logilab.org</link></item></channel></rss>
This is a test of the *reST* transform
o one
o two
o three
Heading 1
=========
Some text.
Heading 2
---------
Some text, bla ble bli blo blu. Yes, i know this is Stupid_.
.. _Stupid: http://www.example.com
=====
Title
=====
--------
Subtitle
--------
This is a test document to make sure subtitle gets the right heading.
Now the real heading
====================
The brown fox jumped over the lazy dog.
With a subheading
------------------
Some text, bla ble bli blo blu. Yes, i know this is Stupid_.
.. _Stupid: http://www.example.com
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:transform xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
<xsl:strip-space elements='*'/>
<xsl:output method='xml'/>
<!-- Narval prototype ====================================================== -->
<al:prototype xmlns:al="http://www.logilab.org/namespaces/Narval/1.2">
<al:description lang="fr">Transforme du RSS en du HTML.</al:description>
<al:description lang="en">Turns RSS into HTML.</al:description>
<al:input id="input"><al:match>rss</al:match></al:input>
<al:output id="output" list="yes"><al:match>html-body</al:match></al:output>
</al:prototype>
<!-- root ================================================================== -->
<xsl:template match='rss/rss/channel'>
<html-body>
<h2>
<xsl:value-of select='title'/>
</h2>
<p>
<xsl:element name='a'>
<xsl:attribute name='href'><xsl:value-of select='link'/></xsl:attribute>
<xsl:value-of select='title'/>
</xsl:element>
<em><xsl:value-of select='description'/></em>
</p>
<table>
<xsl:apply-templates select='item'/>
</table>
</html-body>
</xsl:template>
<xsl:template match='item'>
<tr>
<td>
<xsl:element name='a'>
<xsl:attribute name='href'><xsl:value-of select='link'/></xsl:attribute>
<xsl:value-of select='title'/>
</xsl:element>
<xsl:apply-templates mode='multi' select='description'/>
</td>
</tr>
</xsl:template>
</xsl:transform>
<?xml version="1.0" encoding="utf-8" ?>
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.2.8: http://docutils.sourceforge.net/" />
<title>Copying Docutils</title>
<meta name="author" content="David Goodger" />
<meta name="date" content="2002-10-03" />
<link rel="stylesheet" href="tools/stylesheets/default.css" type="text/css" />
</head>
<body>
<div class="document" id="copying-docutils">
<h1 class="title">Copying Docutils</h1>
<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr><th class="docinfo-name">Author:</th>
<td>David Goodger</td></tr>
<tr><th class="docinfo-name">Contact:</th>
<td><a class="first last reference" href="mailto:goodger&#64;users.sourceforge.net">goodger&#64;users.sourceforge.net</a></td></tr>
<tr><th class="docinfo-name">Date:</th>
<td>2002-10-03</td></tr>
<tr class="field"><th class="docinfo-name">Web site:</th><td class="field-body"><a class="reference" href="http://docutils.sourceforge.net/">http://docutils.sourceforge.net/</a></td>
</tr>
</tbody>
</table>
<p>Most of the files included in this project are in the public domain,
and therefore have no license requirement and no restrictions on
copying or usage. The exceptions are:</p>
<ul class="simple">
<li>docutils/optik.py, copyright Gregory P. Ward, released under a
BSD-style license (which can be found in the module's source code).</li>
<li>docutils/roman.py, copyright by Mark Pilgrim, released under the
<a class="reference" href="http://www.python.org/2.1.1/license.html">Python 2.1.1 license</a>.</li>
<li>test/difflib.py, copyright by the Python Software Foundation,
released under the <a class="reference" href="http://www.python.org/2.2/license.html">Python 2.2 license</a>. This file is included for
compatibility with Python versions less than 2.2; if you have Python
2.2 or higher, difflib.py is not needed and may be removed. (It's
only used to report test failures anyhow; it isn't installed
anywhere. The included file is a pre-generator version of the
difflib.py module included in Python 2.2.)</li>
</ul>
<p>(Disclaimer: I am not a lawyer.) Both the BSD license and the Python
license are <a class="reference" href="http://opensource.org/licenses/">OSI-approved</a> and <a class="reference" href="http://www.gnu.org/philosophy/license-list.html">GPL-compatible</a>. Although complicated
by multiple owners and lots of legalese, the Python license basically
lets you copy, use, modify, and redistribute files as long as you keep
the copyright attribution intact, note any changes you make, and don't
use the owner's name in vain. The BSD license is similar.</p>
</div>
<hr class="footer"/>
<div class="footer">
Generated on: 2003-04-19 15:32 UTC.
Generated by <a class="reference" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.
</div>
</body>
</html>
""" nice docstring """
class A : pass
# comment
def inc(i):
return i+1
def greater(a, b):
"""foo <html />"""
return a > b
<?xml version="1.0" encoding="utf-8" ?>
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test page for save html rendering</title>
<meta name="date" content="2005-07-22" />
</head>
<body>
<h1>Test page</h1>
<table>
<tr>
<th>Test1</th>
<td>test2</td>
</tr>
</table>
<p>This is a text used as a blind text.</p>
<div><![CDATA[
Some CDATA text.
]]>
</div>
<ul>
<li>A sample list item1</li>
<li>A sample list item2</li>
</ul>
<p>This is again a blind text with a<br>line break.</p>
<div>
Can we <q>quote</q> or write something we <del>didn't</del> mean to write? Or how is <ins>this</ins> instead?
</div>
<hr>
<div>
<a href="http://www.plone.org"><img src="http://www.plone.org/logo.jpg"/></a> is just great.
</div>
</body>
</html>
This diff is collapsed.
P6
24 23
255
̙̙
\ No newline at end of file
<h2> Testing Markdown </h2>
<p> <code>code</code> and <em>italic</em> and <em>bold</em> and even a <a href="http://plone.org">link</a>.
</p>
<p>Fööbär</p>
<?xml version="1.0"?>
Logilab.org newsen<tr><td><a href="">xmltools 1.3.7</a></td></tr><tr><td><a href="">Python-logic</a></td></tr><tr><td><a href="">PyReverse 0.2.3</a></td></tr><tr><td><a href="">xmltools 1.3.6</a></td></tr><tr><td><a href="">hmm-0.2</a></td></tr><tr><td><a href="">Version 1.2a1 is out</a></td></tr><tr><td><a href="">XMLdiff v0.5.3 (bug fixes)</a></td></tr><tr><td><a href="">hmm-0.1</a></td></tr><tr><td><a href="">PyReverse 0.1 (new product)</a></td></tr><tr><td><a href="">PyPaSax 0.3 (bug fixes)</a></td></tr><tr><td><a href="">XMLdiff v0.5.2 (bug fixes)</a></td></tr><tr><td><a href="">Version 1.1 is out</a></td></tr><tr><td><a href="">Version 1.1b3 is out</a></td></tr><tr><td><a href="">xmltools-1.3.5</a></td></tr><tr><td><a href="">xmltools-1.3.4</a></td></tr><tr><td><a href="">Version 1.1b1 is out</a></td></tr><tr><td><a href="">XMLdiff v0.5 (algorithm change, bug fixes)</a></td></tr><tr><td><a href="">XMLtools v1.3.1 (bugfixes)</a></td></tr><tr><td><a href="">XMLdiff v0.2 (performance improvement)</a></td></tr><tr><td><a href="">XMLdiff v0.1.1 (beta release)</a></td></tr><tr><td><a href="">XPathVis v1.0beta (beta release)</a></td></tr><tr><td><a href="">XMLtools v1.3 (new features)</a></td></tr><tr><td><a href="http://www-106.ibm.com/developerworks/library/l-ai/">Narval on developerWorks</a></td></tr><tr><td><a href="">Version 1.0.1 is out</a></td></tr><tr><td><a href="http://ai.about.com/compute/ai/library/weekly/aa060801a.htm">Narval reviewed on AI.About.com</a></td></tr><tr><td><a href="http://www.ptolemee.com/botshow/text/text_fr/edito/edito_set.html">Narval at BotShow 2001</a></td></tr><tr><td><a href="">Version 1.0 is out</a></td></tr><tr><td><a href="">Network-boot-HOWTO v0.2.1</a></td></tr><tr><td><a href="">GuessLang v0.1.0 (beta release)</a></td></tr><tr><td><a href="">Network-boot-HOWTO v0.1.1</a></td></tr><tr><td><a href="">PyPaSax v0.1</a></td></tr><tr><td><a href="">RC2 is out</a></td></tr><tr><td><a href="">VCalSax v0.1 (beta)</a></td></tr><tr><td><a href="http://www.logilab.com/press/linux-expo2001/">Talk at LinuxExpo in English</a></td></tr><tr><td><a href="">RC1 is out</a></td></tr><tr><td><a href="">XMLtools v1.2 (stable release)</a></td></tr><tr><td><a href="http://www.logilab.org/narval/app.html">Application section on web site</a></td></tr><tr><td><a href="">WMgMon v0.4.0</a></td></tr><tr><td><a href="">XmlTools v1.1</a></td></tr><tr><td><a href="">Beta5 is out</a></td></tr><tr><td><a href="">XmlTools v1.0</a></td></tr><tr><td><a href="">PyGantt v0.6.0</a></td></tr><tr><td><a href="">Beta4 is out</a></td></tr><tr><td><a href="http://www.linuxgazette.com/issue59/chauvat.html">Article on Narval in Linux Gazette</a></td></tr><tr><td><a href="">Beta3 is out</a></td></tr><tr><td><a href="http://www.linuxexpoparis.com/EN/conferences">Logilab invited at Linux Expo</a></td></tr><tr><td><a href="">Beta2 is out</a></td></tr><tr><td><a href="">Beta1 is out</a></td></tr><tr><td><a href="http://www.logilab.org">Beta0 is out</a></td></tr>
<p>This is a test of the *reST* transform<br /> o one<br /> o two<br /> o three</p>
\ No newline at end of file
<dl class="docutils">
<dt>This is a test of the <em>reST</em> transform</dt>
<dd>o one
o two
o three</dd>
</dl>
This is a test of the *reST* transform
o one
o two
o three
<h2 class="title">Heading 1</h2>
<p>Some text.</p>
<div class="section" id="heading-2">
<h3>Heading 2</h3>
<p>Some text, bla ble bli blo blu. Yes, i know this is<a class="reference external" href="http://www.example.com">Stupid</a>.</p>
</div>
<h2 class="title">Title</h2>
<h3 class="subtitle">Subtitle</h3>
<p>This is a test document to make sure subtitle gets the right heading.</p>
<div class="section" id="now-the-real-heading">
<h3>Now the real heading</h3>
<p>The brown fox jumped over the lazy dog.</p>
<div class="section" id="with-a-subheading">
<h4>With a subheading</h4>
<p>Some text, bla ble bli blo blu. Yes, i know this is<a class="reference external" href="http://www.example.com">Stupid</a>.</p>
</div>
</div>
Copying Docutils
Copying Docutils
Author:
David Goodger
Contact:
goodger@users.sourceforge.net
Date:
2002-10-03
Web site: http://docutils.sourceforge.net/
Most of the files included in this project are in the public domain,
and therefore have no license requirement and no restrictions on
copying or usage. The exceptions are:
docutils/optik.py, copyright Gregory P. Ward, released under a
BSD-style license (which can be found in the module's source code).
docutils/roman.py, copyright by Mark Pilgrim, released under the
Python 2.1.1 license .
test/difflib.py, copyright by the Python Software Foundation,
released under the Python 2.2 license . This file is included for
compatibility with Python versions less than 2.2; if you have Python
2.2 or higher, difflib.py is not needed and may be removed. (It's
only used to report test failures anyhow; it isn't installed
anywhere. The included file is a pre-generator version of the
difflib.py module included in Python 2.2.)
(Disclaimer: I am not a lawyer.) Both the BSD license and the Python
license are OSI-approved and GPL-compatible . Although complicated
by multiple owners and lots of legalese, the Python license basically
lets you copy, use, modify, and redistribute files as long as you keep
the copyright attribution intact, note any changes you make, and don't
use the owner's name in vain. The BSD license is similar.
Generated on: 2003-04-19 15:32 UTC.
Generated by Docutils from reStructuredText source.
Copying Docutils
Author: David Goodger
Contact: [1]goodger@users.sourceforge.net
Date: 2002-10-03
Web site: [2]http://docutils.sourceforge.net/
Most of the files included in this project are in the public domain,
and therefore have no license requirement and no restrictions on
copying or usage. The exceptions are:
* docutils/optik.py, copyright Gregory P. Ward, released under a
BSD-style license (which can be found in the module's source
code).
* docutils/roman.py, copyright by Mark Pilgrim, released under the
[3]Python 2.1.1 license.
* test/difflib.py, copyright by the Python Software Foundation,
released under the [4]Python 2.2 license. This file is included
for compatibility with Python versions less than 2.2; if you have
Python 2.2 or higher, difflib.py is not needed and may be removed.
(It's only used to report test failures anyhow; it isn't installed
anywhere. The included file is a pre-generator version of the
difflib.py module included in Python 2.2.)
(Disclaimer: I am not a lawyer.) Both the BSD license and the Python
license are [5]OSI-approved and [6]GPL-compatible. Although
complicated by multiple owners and lots of legalese, the Python
license basically lets you copy, use, modify, and redistribute files
as long as you keep the copyright attribution intact, note any changes
you make, and don't use the owner's name in vain. The BSD license is
similar.
_________________________________________________________________
Generated on: 2003-04-19 15:32 UTC. Generated by [7]Docutils from
[8]reStructuredText source.
References
1. mailto:goodger@users.sourceforge.net
2. http://docutils.sourceforge.net/
3. http://www.python.org/2.1.1/license.html
4. http://www.python.org/2.2/license.html
5. http://opensource.org/licenses/
6. http://www.gnu.org/philosophy/license-list.html
7. http://docutils.sourceforge.net/
8. http://docutils.sourceforge.net/rst.html
<pre class="python">
<span style="color: #004080;">&quot;&quot;&quot; nice docstring &quot;&quot;&quot;</span>
<span style="color: #C00000;">class</span> <span style="color: #000000;">A</span> <span style="color: #0000C0;">:</span> <span style="color: #C00000;">pass</span>
<span style="color: #008000;"># comment
</span>
<span style="color: #C00000;">def</span> <span style="color: #000000;">inc</span><span style="color: #0000C0;">(</span><span style="color: #000000;">i</span><span style="color: #0000C0;">)</span><span style="color: #0000C0;">:</span>
<span style="color: #C00000;">return</span> <span style="color: #000000;">i</span><span style="color: #0000C0;">+</span><span style="color: #0080C0;">1</span>
<span style="color: #C00000;">def</span> <span style="color: #000000;">greater</span><span style="color: #0000C0;">(</span><span style="color: #000000;">a</span><span style="color: #0000C0;">,</span> <span style="color: #000000;">b</span><span style="color: #0000C0;">)</span><span style="color: #0000C0;">:</span>
<span style="color: #004080;">&quot;&quot;&quot;foo &lt;html /&gt;&quot;&quot;&quot;</span>
<span style="color: #C00000;">return</span> <span style="color: #000000;">a</span> <span style="color: #0000C0;">&gt;</span> <span style="color: #000000;">b</span>
</pre>
<h1>Test page</h1>
<table>
<tr>
<th>Test1</th>
<td>test2</td>
</tr>
</table>
<p>This is a text used as a blind text.</p>
<div><![CDATA[
Some CDATA text.
]]>
</div>
<ul>
<li>A sample list item1</li>
<li>A sample list item2</li>
</ul>
<p>This is again a blind text with a<br />line break.</p>
<div>
Can we <q>quote</q> or write something we <del>didn't</del> mean to write? Or how is <ins>this</ins> instead?
</div>
<hr />
<div>
<a href="http://www.plone.org"><img src="http://www.plone.org/logo.jpg" /></a> is just great.
</div>
\ No newline at end of file
<br />
<p><div name="Default" align="left" style=" padding: 0.00mm 0.00mm 0.00mm 0.00mm; ">
<p style="text-indent: 0.00mm; text-align: left; line-height: 4.166667mm; color: Black; background-color: White; ">
how odd: blank named file in directory
</p></div>
<h1>Textile test text</h1>
<p><em>This</em> is quite <strong>boring</strong>, but it needs to be <a href="http://plone.org">done</a>, right?</p>
<h2>Cheeses</h2>
<ol>
<li>Gouda</li>
<li>Roquefort</li>
<li>Emmentaler</li>
</ol>
<h2>Episodes</h2>
<ul>
<li>Bicycle Repairman</li>
<li>Spanish Inquisition</li>
<li>Fishslapping Dance</li>
</ul>
import os
from Testing import ZopeTestCase
from Products.PortalTransforms.tests.utils import input_file_path, normalize_html,\
matching_inputs
from Products.PortalTransforms.transforms.image_to_gif import image_to_gif
from Products.PortalTransforms.transforms.image_to_png import image_to_png
from Products.PortalTransforms.transforms.image_to_jpeg import image_to_jpeg
from erp5.component.module.TransformImageToBmp import image_to_bmp
from Products.PortalTransforms.transforms.image_to_tiff import image_to_tiff
from Products.PortalTransforms.transforms.image_to_ppm import image_to_ppm
from erp5.component.module.TransformImageToPcx import image_to_pcx
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
# we have to set locale because lynx output is locale sensitive !
os.environ['LC_ALL'] = 'C'
class ImageMagickTransformsTest(ERP5TypeTestCase, ZopeTestCase.Functional):
def afterSetUp(self):
super(ImageMagickTransformsTest, self).afterSetUp()
self.pt = self.portal.portal_transforms
def test_image_to_bmp(self):
self.pt.registerTransform(image_to_bmp())
imgFile = open(input_file_path('logo.jpg'), 'rb')
data = imgFile.read()
self.assertEqual(self.portal.mimetypes_registry.classify(data),'image/jpeg')
data = self.pt.convertTo(target_mimetype='image/x-ms-bmp',orig=data)
self.assertEqual(data.getMetadata()['mimetype'], 'image/x-ms-bmp')
def test_image_to_gif(self):
self.pt.registerTransform(image_to_gif())
imgFile = open(input_file_path('logo.png'), 'rb')
data = imgFile.read()
self.assertEqual(self.portal.mimetypes_registry.classify(data),'image/png')
data = self.pt.convertTo(target_mimetype='image/gif',orig=data)
self.assertEqual(data.getMetadata()['mimetype'], 'image/gif')
def test_image_to_jpeg(self):
self.pt.registerTransform(image_to_jpeg())
imgFile = open(input_file_path('logo.gif'), 'rb')
data = imgFile.read()
self.assertEqual(self.portal.mimetypes_registry.classify(data),'image/gif')
data = self.pt.convertTo(target_mimetype='image/jpeg',orig=data)
self.assertEqual(data.getMetadata()['mimetype'], 'image/jpeg')
def test_image_to_png(self):
self.pt.registerTransform(image_to_png())
imgFile = open(input_file_path('logo.jpg'), 'rb')
data = imgFile.read()
self.assertEqual(self.portal.mimetypes_registry.classify(data),'image/jpeg')
data = self.pt.convertTo(target_mimetype='image/png',orig=data)
self.assertEqual(data.getMetadata()['mimetype'], 'image/png')
def test_image_to_pcx(self):
self.pt.registerTransform(image_to_pcx())
imgFile = open(input_file_path('logo.gif'), 'rb')
data = imgFile.read()
self.assertEqual(self.portal.mimetypes_registry.classify(data),'image/gif')
data = self.pt.convertTo(target_mimetype='image/pcx',orig=data)
self.assertEqual(data.getMetadata()['mimetype'], 'image/pcx')
def test_image_to_ppm(self):
self.pt.registerTransform(image_to_ppm())
imgFile = open(input_file_path('logo.png'), 'rb')
data = imgFile.read()
self.assertEqual(self.portal.mimetypes_registry.classify(data),'image/png')
data = self.pt.convertTo(target_mimetype='image/x-portable-pixmap',orig=data)
self.assertEqual(data.getMetadata()['mimetype'], 'image/x-portable-pixmap')
def test_image_to_tiff(self):
self.pt.registerTransform(image_to_tiff())
imgFile = open(input_file_path('logo.jpg'), 'rb')
data = imgFile.read()
self.assertEqual(self.portal.mimetypes_registry.classify(data),'image/jpeg')
data = self.pt.convertTo(target_mimetype='image/tiff',orig=data)
self.assertEqual(data.getMetadata()['mimetype'], 'image/tiff')
# FIXME missing tests for image_to_html, st
def test_suite():
from unittest import TestSuite, makeSuite
suite = TestSuite()
suite.addTest(makeSuite(ImageMagickTransformsTest))
return suite
import unittest
from zope.testing import doctestunit
modules = (
'Products.PortalTransforms.transforms.safe_html',
'Products.PortalTransforms.transforms.rest',
)
def test_suite():
return unittest.TestSuite(
[doctestunit.DocTestSuite(module=module) for module in modules]
)
This diff is collapsed.
from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
from utils import input_file_path
FILE_PATH = input_file_path("demo1.pdf")
class TestGraph(ATSiteTestCase):
def afterSetUp(self):
ATSiteTestCase.afterSetUp(self)
self.engine = self.portal.portal_transforms
def testGraph(self):
data = open(FILE_PATH, 'r').read()
requirements = self.engine._policies.get('text/plain', [])
if requirements:
out = self.engine.convertTo('text/plain', data, filename=FILE_PATH)
self.assertTrue(out.getData())
def testFindPath(self):
originalMap = self.engine._mtmap
"""
The dummy map used for this test corresponds to a graph
depicted in ASCII art below :
+---+
| |
| v
+-->1<-->2-->4-->6<--7
^ ^ |
| | |
v | |
3<---+ |
^ |
| |
v |
5<-------+
"""
# we need a DummyTransform class
class DT:
def __init__(self, name):
self._name = name
def name(self):
return self._name
dummyMap1 = {
'1': { '1': [DT('transform1-1')],
'2': [DT('transform1-2')],
'3': [DT('transform1-3')]},
'2': { '1': [DT('transform2-1')],
'3': [DT('transform2-3')],
'4': [DT('transform2-4')]},
'3': { '1': [DT('transform3-1')],
'2': [DT('transform3-2')],
'5': [DT('transform3-5')]},
'4': { '5': [DT('transform4-5')],
'6': [DT('transform4-6')]},
'5': { '3': [DT('transform5-3')]},
'7': { '6': [DT('transform7-6')]}
}
expectedPathes = {
'1-1': [],
'1-2': ['transform1-2'],
'1-3': ['transform1-3'],
'1-4': ['transform1-2', 'transform2-4'],
'1-5': ['transform1-3', 'transform3-5'],
'1-6': ['transform1-2', 'transform2-4', 'transform4-6'],
'1-7': None,
'2-1': ['transform2-1'],
'2-2': [],
'2-4': ['transform2-4'],
'4-2': ['transform4-5', 'transform5-3', 'transform3-2'],
'5-3': ['transform5-3']
}
self.engine._mtmap = dummyMap1
for orig in ['1','2','3','4','5','6','7']:
for target in ['1','2','3','4','5','6','7']:
# build the name of the path
pathName = orig + '-' + target
# do we have any expectation for this path ?
if pathName in expectedPathes.keys():
# we do. Here is the expected shortest path
expectedPath = expectedPathes[pathName]
# what's the shortest path according to the engine ?
gotPath = self.engine._findPath(orig,target)
# just keep the name of the transforms, please
if gotPath is not None:
gotPath = [transform.name() for transform in gotPath]
# this must be the same as in our expectation
self.assertEqual(expectedPath, gotPath)
self.engine._mtmap = originalMap
def testFindPathWithEmptyTransform(self):
""" _findPath should not throw "index out of range" when dealing with
empty transforms list
"""
dummyMap = {'1': {'2': []}}
self.engine._mtmap = dummyMap
self.engine._findPath('1','2')
def testIdentity(self):
orig = 'Some text'
converted = self.engine.convertTo(
'text/plain', 'Some text', mimetype='text/plain')
self.assertEqual(orig, str(converted))
def test_suite():
from unittest import TestSuite, makeSuite
suite = TestSuite()
suite.addTest(makeSuite(TestGraph))
return suite
# -*- coding: utf-8 -*-
from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
class TransformTestCase(ATSiteTestCase):
def afterSetUp(self):
ATSiteTestCase.afterSetUp(self)
self.transforms = self.portal.portal_transforms
class TestIntelligentTextToHtml(TransformTestCase):
def performTransform(self, orig, targetMimetype = 'text/html', mimetype='text/x-web-intelligent'):
return self.transforms.convertTo(targetMimetype, orig, context=self.portal, mimetype=mimetype).getData()
def testHyperlinks(self):
orig = "A test http://test.com"
new = self.performTransform(orig)
self.assertEqual(new, 'A test <a href="http://test.com" rel="nofollow">http://test.com</a>')
def testMailto(self):
orig = "A test test@test.com of mailto"
new = self.performTransform(orig)
self.assertEqual(new, 'A test <a href="&#0109;ailto&#0058;test&#0064;test.com">test&#0064;test.com</a> of mailto')
def testTextAndLinks(self):
orig = """A test
URL: http://test.com End
Mail: test@test.com End
URL: http://foo.com End"""
new = self.performTransform(orig)
self.assertEqual(new, 'A test<br />' \
'URL: <a href="http://test.com" rel="nofollow">http://test.com</a> End<br />' \
'Mail: <a href="&#0109;ailto&#0058;test&#0064;test.com">test&#0064;test.com</a> End<br />' \
'URL: <a href="http://foo.com" rel="nofollow">http://foo.com</a> End')
def testTextAndLinksAtEndOfLine(self):
orig = """A test
URL: http://test.com
Mail: test@test.com
URL: http://foo.com"""
new = self.performTransform(orig)
self.assertEqual(new, 'A test<br />' \
'URL: <a href="http://test.com" rel="nofollow">http://test.com</a><br />' \
'Mail: <a href="&#0109;ailto&#0058;test&#0064;test.com">test&#0064;test.com</a><br />' \
'URL: <a href="http://foo.com" rel="nofollow">http://foo.com</a>')
def testIndents(self):
orig = """A test
URL: http://test.com
Mail: test@test.com
URL: http://foo.com"""
new = self.performTransform(orig)
self.assertEqual(new, 'A test<br />' \
'&nbsp;&nbsp;URL: <a href="http://test.com" rel="nofollow">http://test.com</a><br />' \
'&nbsp;&nbsp;&nbsp;&nbsp;Mail: <a href="&#0109;ailto&#0058;test&#0064;test.com">test&#0064;test.com</a><br />' \
'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;URL: <a href="http://foo.com" rel="nofollow">http://foo.com</a>')
def testEntities(self):
orig = "Some & funny < characters"
new = self.performTransform(orig)
self.assertEqual(new, "Some &amp; funny &lt; characters")
def testAccentuatedCharacters(self):
orig = "The French use é à ô ù à and ç"
new = self.performTransform(orig)
self.assertEqual(new, "The French use &eacute; &agrave; &ocirc; &ugrave; &agrave; and &ccedil;")
class TestHtmlToIntelligentText(TransformTestCase):
def performTransform(self, orig, targetMimetype = 'text/x-web-intelligent', mimetype='text/html'):
return self.transforms.convertTo(targetMimetype, orig, context=self.portal, mimetype=mimetype).getData()
def testStripTags(self):
orig = "Some <b>bold</b> text."
new = self.performTransform(orig)
self.assertEqual(new, "Some bold text.")
def testBreaks(self):
orig = "Some<br/>broken<BR/>text<br />"
new = self.performTransform(orig)
self.assertEqual(new, "Some\nbroken\ntext\n")
def testStartBlocks(self):
orig = "A block<dt>there</dt>"
new = self.performTransform(orig)
self.assertEqual(new, "A block\n\nthere")
def testEndBlocks(self):
orig = "<p>Paragraph</p>Other stuff"
new = self.performTransform(orig)
self.assertEqual(new, "Paragraph\n\nOther stuff")
def testIndentBlocks(self):
orig = "A<blockquote>Indented blockquote</blockquote>"
new = self.performTransform(orig)
self.assertEqual(new, "A\n\n Indented blockquote")
def testListBlocks(self):
orig = "A list<ul><li>Foo</li><li>Bar</li></ul>"
new = self.performTransform(orig)
self.assertEqual(new, "A list\n\n - Foo\n\n - Bar\n\n")
def testNbsp(self):
orig = "Some space &nbsp;&nbsp;here"
new = self.performTransform(orig)
self.assertEqual(new, "Some space here")
def testAngles(self):
orig = "Watch &lt;this&gt; and &lsaquo;that&rsaquo;"
new = self.performTransform(orig)
self.assertEqual(new, "Watch <this> and &#8249;that&#8250;")
def testBullets(self):
orig = "A &bull; bullet"
new = self.performTransform(orig)
self.assertEqual(new, "A &#8226; bullet")
def testAmpersands(self):
orig = "An &amp; ampersand"
new = self.performTransform(orig)
self.assertEqual(new, "An & ampersand")
def testEntities(self):
orig = "A &mdash; dash"
new = self.performTransform(orig)
self.assertEqual(new, "A &#8212; dash")
def testPre(self):
orig = "A <pre> pre\n section</pre>"
new = self.performTransform(orig)
self.assertEqual(new, "A \n\n pre\n section\n\n")
def testWhitespace(self):
orig = "A \n\t spaceful, <b> tag-filled</b>, <b> <i> snippet\n</b></i>"
new = self.performTransform(orig)
self.assertEqual(new, "A spaceful, tag-filled, snippet ")
def test_suite():
from unittest import TestSuite, makeSuite
suite = TestSuite()
suite.addTest(makeSuite(TestIntelligentTextToHtml))
suite.addTest(makeSuite(TestHtmlToIntelligentText))
return suite
This diff is collapsed.
"""
"""
import os, sys
if __name__ == '__main__':
execfile(os.path.join(sys.path[0], 'framework.py'))
from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
class TestXSSFilter(ATSiteTestCase):
def afterSetUp(self):
ATSiteTestCase.afterSetUp(self)
self.engine = self.portal.portal_transforms
def doTest(self, data_in, data_out):
html = self.engine.convertTo('text/x-html-safe', data_in, mimetype="text/html")
self.assertEqual (data_out,html.getData())
def test_1(self):
data_in = """<html><body><img src="javascript:Alert('XSS');" /></body></html>"""
data_out = """<img />"""
self.doTest(data_in, data_out)
def test_2(self):
data_in = """<img src="javascript:Alert('XSS');" />"""
data_out = """<img />"""
self.doTest(data_in, data_out)
def test_3(self):
data_in = """<html><body><IMG SRC=&#106;&#97;&#118;&#97;&#115;&#99;&#114;&#105;&#112;&#116;&#58;&#97;&#108;&#101;&#114;&#116;&#40;&#39;&#88;&#83;&#83;&#39;&#41;></body></html>"""
data_out = """<img />"""
self.doTest(data_in, data_out)
def test_4(self):
data_in = """<IMG SRC=&#106;&#97;&#118;&#97;&#115;&#99;&#114;&#105;&#112;&#116;&#58;&#97;&#108;&#101;&#114;&#116;&#40;&#39;&#88;&#83;&#83;&#39;&#41;>"""
data_out = """<img />"""
self.doTest(data_in, data_out)
def test_5(self):
data_in = """<img src="jav
asc
ript:Alert('XSS');" />"""
data_out = """<img />"""
self.doTest(data_in, data_out)
def test_6(self):
data_in = """<img src="jav asc ript:Alert('XSS');"/>"""
data_out = """<img />"""
self.doTest(data_in, data_out)
def test_7(self):
data_in = """<a href=&#106;&#97;&#118;&#97;&#115;&#99;&#114;&#105;&#112;&#116;&#58;&#97;&#108;&#101;&#114;&#116;&#40;&#39;&#88;&#83;&#83;&#39;&#41;>test med a-tag</a>"""
data_out = """<a>test med a-tag</a>"""
self.doTest(data_in, data_out)
def test_8(self):
data_in = """<div style="bacground:url(jav asc ript:Alert('XSS')">test</div>"""
data_out = """<div>test</div>"""
self.doTest(data_in, data_out)
def test_9(self):
data_in = """<div style="bacground:url(jav
asc
ript:
Alert('XSS')">test</div>"""
data_out = """<div>test</div>"""
self.doTest(data_in, data_out)
def test_10(self):
data_in = """<div style="bacground:url(&#106;&#97;&#118;&#97;&#115;&#99;&#114;&#105;&#112;&#116;&#58;&#97;&#108;&#101;&#114;&#116;&#40;&#39;&#88;&#83;&#83;&#39;&#41;">test</div>"""
data_out = """<div>test</div>"""
self.doTest(data_in, data_out)
def test_11(self):
data_in = """<div style="bacground:url(v b sc ript:msgbox('XSS')">test</div>"""
data_out = """<div>test</div>"""
self.doTest(data_in, data_out)
def test_12(self):
data_in = """<img src="vbscript:msgbox('XSS')"/>"""
data_out = """<img />"""
self.doTest(data_in, data_out)
def test_13(self):
data_in = """<img src="vb
sc
ript:msgbox('XSS')"/>"""
data_out = """<img />"""
self.doTest(data_in, data_out)
def test_14(self):
data_in = """<a href="vbscript:Alert('XSS')">test</a>"""
data_out = """<a>test</a>"""
self.doTest(data_in, data_out)
def test_15(self):
data_in = """<div STYLE="width: expression(window.location='http://www.dr.dk';);">div</div>"""
data_out = """<div>div</div>"""
self.doTest(data_in, data_out)
def test_16(self):
data_in = """<div STYLE="width: ex pre ss io n(window.location='http://www.dr.dk';);">div</div>"""
data_out = """<div>div</div>"""
self.doTest(data_in, data_out)
def test_17(self):
data_in = """<div STYLE="width: ex
pre
ss
io
n(window.location='http://www.dr.dk';);">div</div>"""
data_out = """<div>div</div>"""
self.doTest(data_in, data_out)
def test_18(self):
data_in = """<div style="width: 14px;">div</div>"""
data_out = data_in
self.doTest(data_in, data_out)
def test_19(self):
data_in = """<a href="http://www.headnet.dk">headnet</a>"""
data_out = data_in
self.doTest(data_in, data_out)
def test_20(self):
data_in = """<img src="http://www.headnet.dk/log.jpg" />"""
data_out = data_in
self.doTest(data_in, data_out)
def test_21(self):
data_in = """<mustapha name="mustap" tlf="11 11 11 11" address="unknown">bla bla bla</mustapha>"""
data_out = """bla bla bla"""
self.doTest(data_in, data_out)
def test_22(self):
data_in = '<<frame></frame>script>alert("XSS");<<frame></frame>/script>'
data_out = '&lt;script&gt;alert("XSS");&lt;/script&gt;'
self.doTest(data_in, data_out)
def test_suite():
from unittest import TestSuite, makeSuite
suite = TestSuite()
suite.addTest(makeSuite(TestXSSFilter))
return suite
if __name__ == '__main__':
framework()
import re
import glob
from unittest import TestSuite
from sys import modules
from os.path import join, abspath, dirname, basename
def normalize_html(s):
s = re.sub(r"&nbsp;", " ", s)
s = re.sub(r"\s+", " ", s)
s = re.sub(r"(?s)\s+<", "<", s)
s = re.sub(r"(?s)>\s+", ">", s)
s = re.sub(r"\r", "", s)
return s
def build_test_suite(package_name,module_names,required=1):
"""
Utlitity for building a test suite from a package name
and a list of modules.
If required is false, then ImportErrors will simply result
in that module's tests not being added to the returned
suite.
"""
suite = TestSuite()
try:
for name in module_names:
the_name = package_name+'.'+name
__import__(the_name,globals(),locals())
suite.addTest(modules[the_name].test_suite())
except ImportError:
if required:
raise
return suite
PREFIX = abspath(dirname(__file__))
def input_file_path(file):
return join(PREFIX, 'input', file)
def output_file_path(file):
return join(PREFIX, 'output', file)
def matching_inputs(pattern):
return [basename(path) for path in glob.glob(join(PREFIX, "input", pattern))]
def load(dotted_name, globals=None):
""" load a python module from it's name """
mod = __import__(dotted_name, globals)
components = dotted_name.split('.')
for comp in components[1:]:
mod = getattr(mod, comp)
return mod
from rigging import transformer
import os
from stat import ST_MTIME
## BIG BAD FUNCTIONAL TEST OF OOo Word Conversion
## The interfaces work, but are not quite what we need
## I might have to back fill a chain from source/dest graphing
file = "/tmp/word.doc"
class curry:
def __init__(self, func, *fixed_args):
self.func = func
self.fixed_args = fixed_args
def __call__(self, *variable_args):
return apply(self.func, self.fixed_args +
variable_args)
data = open("/tmp/word.doc", "r").read()
data = transformer.convert("WordToHtml", data, filename="word.doc")
print data.getData()
"""try to build some usefull transformations with the command and xml
transforms and the available binaries
"""
from Products.PortalTransforms.libtransforms.utils import bin_search, MissingBinary
COMMAND_CONFIGS = (
('lynx_dump', '.html',
{'binary_path' : 'lynx',
'command_line' : '-dump %(input)s',
'inputs' : ('text/html',),
'output' : 'text/plain',
}),
('tidy_html', '.html',
{'binary_path' : 'tidy',
'command_line' : '%(input)s',
'inputs' : ('text/html',),
'output' : 'text/html',
}),
('rtf_to_html', None,
{'binary_path' : 'unrtf',
'command_line' : '%(input)s',
'inputs' : ('application/rtf',),
'output' : 'text/html',
}),
('ppt_to_html', None,
{'binary_path' : 'ppthtml',
'command_line' : '%(input)s',
'inputs' : ('application/vnd.ms-powerpoint',),
'output' : 'text/html',
}),
('excel_to_html', None,
{'binary_path' : 'xlhtml',
'command_line' : '-nh -a %(input)s',
'inputs' : ('application/vnd.ms-excel',),
'output' : 'text/html',
}),
('ps_to_text', None,
{'binary_path' : 'ps2ascii',
'command_line' : '%(input)s',
'inputs' : ('application/postscript',),
'output' : 'text/plain',
}),
)
TRANSFORMS = {}
from command import ExternalCommandTransform
for tr_name, extension, config in COMMAND_CONFIGS:
try:
bin = bin_search(config['binary_path'])
except MissingBinary:
print 'no such binary', config['binary_path']
else:
tr = ExternalCommandTransform(tr_name, extension)
tr.config['binary_path'] = bin
tr.__name__ = tr_name
tr.config = config
TRANSFORMS[tr_name] = tr
XMLPROCS_CONF = {
'xsltproc' : '--catalogs --xinclude -o %(output)s %(transform)s %(input)s',
'4xslt' : ' -o %(output)s %(input)s %(transform)s'
}
bin = None
for proc in XMLPROCS_CONF.keys():
try:
bin = bin_search(proc)
break
except MissingBinary:
print 'no such binary', proc
if bin is not None:
print 'Using %s as xslt processor' % bin
from xml import XsltTransform
for output in ('html', 'plain'):
name = "xml_to_" + output
command_line = XMLPROCS_CONF[proc]
tr = XsltTransform(name=name, inputs=('text/xml',), output='text/'+output,
binary_path=bin, command_line=command_line)
TRANSFORMS[name] = tr
def initialize(engine):
for transform in TRANSFORMS.values():
engine.registerTransform(transform)
"""
A custom transform using external command
"""
__revision__ = '$Id: command.py 4439 2005-06-15 16:32:36Z panjunyong $'
import os.path
from os import popen3
from Products.PortalTransforms.interfaces import ITransform
from zope.interface import implements
from Products.PortalTransforms.libtransforms.utils import bin_search, sansext
from Products.PortalTransforms.libtransforms.commandtransform import commandtransform
from Products.PortalTransforms.utils import log
class ExternalCommandTransform(commandtransform):
""" Custom external command
transform content by launching an external command
the command should take the content in an input file (designed by '%s' in
the command line parameters) and return output on stdout.
Input and output mime types must be set correctly !
"""
implements(ITransform)
__name__ = "command_transform"
def __init__(self, name=None, input_extension=None, **kwargs):
self.config = {
'binary_path' : '',
'command_line' : '',
'inputs' : ('text/plain',),
'output' : 'text/plain',
}
self.config_metadata = {
'binary_path' : ('string', 'Binary path',
'Path of the executable on the server.'),
'command_line' : ('string', 'Command line',
'''Additional command line option.
There should be at least the input file (designed by "%(input)s").
The transformation\'s result must be printed on stdout.
'''),
'inputs' : ('list', 'Inputs', 'Input(s) MIME type. Change with care.'),
'output' : ('string', 'Output', 'Output MIME type. Change with care.'),
}
self.config.update(kwargs)
commandtransform.__init__(self, name=name, binary=self.config['binary_path'], **kwargs)
# use the full binary path
self.config.update({'binary_path':self.binary})
self.input_extension = input_extension
def __getattr__(self, attr):
if attr == 'inputs':
return self.config['inputs']
if attr == 'output':
return self.config['output']
raise AttributeError(attr)
def convert(self, data, cache, **kwargs):
filename = kwargs.get('filename') or 'unknown'
if self.input_extension is not None:
kwargs['filename'] = 'unknown' + self.input_extension
else:
kwargs['filename'] = 'unknown' + os.path.splitext(filename)[-1]
tmpdir, fullname = self.initialize_tmpdir(data, **kwargs)
data = self.invokeCommand(fullname)
cache.setData(data)
path, images = self.subObjects(tmpdir)
objects = {}
if images:
self.fixImages(path, images, objects)
cache.setSubObjects(objects)
self.cleanDir(tmpdir)
return cache
def invokeCommand(self, input_name):
command = '%(binary_path)s %(command_line)s' % self.config
input, output, error = popen3(command % input_name)
input.close()
# first read stderr, else we may hang on stout
# but, still hang my windows, so commented it :-(
# error_data = error.read()
error_data = 'error while running "%s"' % (command % input_name)
error.close()
data = output.read()
output.close()
if error_data and not data:
data = error_data
else:
log('Error while running "%s":\n %s' % (command % input_name,
error_data))
return data
def register():
return ExternalCommandTransform()
"""
A custom transform using external command
"""
__revision__ = '$Id: xml.py 4787 2005-08-19 21:43:41Z dreamcatcher $'
from os.path import join, dirname, exists
import re
from os import popen3, popen4, system
from cStringIO import StringIO
from Products.PortalTransforms.interfaces import ITransform
from zope.interface import implements
from Products.PortalTransforms.libtransforms.utils import bin_search, sansext
from Products.PortalTransforms.libtransforms.commandtransform import commandtransform
from Products.PortalTransforms.utils import log
class XsltTransform(commandtransform):
""" Custom external command
transform xml content by launching an external XSLT processor
Input and output mime types must be set correctly !
You can associate different document type to different transformations.
"""
implements(ITransform)
__name__ = "xml_to_html"
def __init__(self, name=None, **kwargs):
self.config = {
# sample configuration
'binary_path' : bin_search('xsltproc'),
'command_line' : '%(transform)s %(input)s',
'inputs' : ('text/xml',),
'output' : 'text/html',
'output_encoding' : 'UTF-8',
'dtds' : {
'-//OASIS//DTD DocBook V4.1//EN' : '/usr/share/sgml/docbook/xsl-stylesheets-1.29/html/docbook.xsl'
},
'default_transform': ''
}
self.config_metadata = {
'binary_path' : ('string', 'Binary path',
'Path of the executable on the server.'),
'command_line' : ('string', 'Command line',
'''Additional command line option.
There should be at least the input file (designed by "%(input)s") and the xsl
file (designed by "%(transform)s").The transformation\'s result must be printed on stdout.
'''),
'inputs' : ('list', 'Inputs', 'Input(s) MIME type. Change with care.'),
'output' : ('string', 'Output', 'Output MIME type. Change with care.'),
'output_encoding': ('string', 'Output encoding', 'Output encoding.'),
'dtds' : ('dict', 'DTDs',
'Association of public ids or dtds to XSL transformations.',
('Public id', 'XSLT path')),
'default_transform' : ('string', 'Default xslt',
'Default xslt, used when no specific transformation is found.'),
}
self.config.update(kwargs)
if name:
self.__name__ = name
def __getattr__(self, attr):
if attr == 'inputs':
return self.config['inputs']
if attr == 'output':
return self.config['output']
if attr == 'output_encoding':
return self.config['output_encoding']
raise AttributeError(attr)
def convert(self, data, cache, **kwargs):
base_name = sansext(kwargs.get("filename") or 'unknown.xml')
dtds = self.config['dtds']
tmpdir, fullname = self.initialize_tmpdir(data, filename=base_name)
try:
try:
doctype = get_doctype(data)
except DTException:
try:
doctype = get_dtd(data)
except DTException:
log('Unable to get doctype nor dtd in %s' % data)
doctype = None
if doctype and dtds.has_key(doctype):
data = self.invokeCommand(fullname, dtds[doctype])
elif self.config['default_transform']:
data = self.invokeCommand(fullname, self.config['default_transform'])
cache.setData(data)
path, images = self.subObjects(tmpdir)
objects = {}
if images:
self.fixImages(path, images, objects)
cache.setSubObjects(objects)
return cache
finally:
self.cleanDir(tmpdir)
def invokeCommand(self, input_name, xsl):
dest_dir = dirname(input_name)
output_file = join(dirname(input_name), 'tr_output')
command = '%(binary_path)s %(command_line)s' % self.config
data = {'input': input_name, 'output': output_file, 'transform': xsl}
system(command % data)
if exists(output_file):
data = open(output_file).read()
else:
data = 'error occurs during transform. See error log'
return data
def register():
return XsltTransform()
DT_RGX = re.compile('<!DOCTYPE \w* PUBLIC \"([^"]*)\" \"([^"]*)\"')
DT_RGX2 = re.compile('<!DOCTYPE \w* SYSTEM \"([^"]*)\"')
class DTException(Exception): pass
def get_doctype(data):
""" return the public id for the doctype given some raw xml data
"""
if not hasattr(data, 'readlines'):
data = StringIO(data)
for line in data.readlines():
line = line.strip()
if not line:
continue
if line.startswith('<?xml') or line.startswith('<!-- '):
continue
m = DT_RGX.match(line)
if m is not None:
return m.group(1)
else:
raise DTException('Unable to match doctype in "%s"' % line)
def get_dtd(data):
""" return the public id for the doctype given some raw xml data
"""
if not hasattr(data, 'readlines'):
data = StringIO(data)
for line in data.readlines():
line = line.strip()
if not line:
continue
if line.startswith('<?xml') or line.startswith('<!-- '):
continue
m = DT_RGX.match(line)
if m is not None:
return m.group(2)
m = DT_RGX2.match(line)
if m is not None:
return m.group(1)
else:
raise DTException('Unable to match doctype in "%s"' % line)
if __name__ == '__main__':
print get_doctype('''<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE article PUBLIC "-//LOGILAB/DTD DocBook V4.1.2-Based Extension V0.1//EN" "dcbk-logilab.dtd" []>
<book id="devtools_user_manual" lang="fr">
''')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment