Skip to content

Commit

Permalink
Fixed testing error by moving the part of README.rst to plone/outputf…
Browse files Browse the repository at this point in the history
…ilters/README.txt.
  • Loading branch information
mauritsvanrees committed Apr 10, 2012
1 parent f50169c commit 26dbae7
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 66 deletions.
4 changes: 3 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Changelog
1.3 (unreleased)
----------------

- Nothing changed yet.
- Fixed testing error by moving the part of README.rst to
plone/outputfilters/README.txt.
[maurits]


1.2 - 2012-04-09
Expand Down
64 changes: 0 additions & 64 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,67 +107,3 @@ isfullsize
True if ``image`` is ``fullimage``.
width
The width of ``image``.


Adding a custom filter
======================

As an example, the following filter replaces all doubled hyphens ("--") with em
dashes ("—"). (Don't use the example verbatim, because it doesn't parse HTML to
apply itself only to text nodes, so will mangle HTML comments.)

A filter is a callable which accepts a UTF-8-encoded HTML string as input, and
returns a modified UTF-8-encoded HTML string. A return value of ``None`` may be
used to indicate that the input should not be modified.

.. include:: plone/outputfilters/filters/example.py
:literal:

The ``order`` attribute may be used to affect the order in which filters are
applied (higher values run later). The is_enabled method should return a boolean
indicating whether the filter should be applied.

Filters are registered in ZCML as a named multi-adapter of the context and
request to IFilter.

>>> from Products.Five.zcml import load_string
>>> load_string("""
... <configure
... xmlns="http://namespaces.zope.org/zope">
...
... <adapter
... name="em_dash_adder"
... provides="plone.outputfilters.interfaces.IFilter"
... for="* *"
... factory="plone.outputfilters.filters.example.EmDashAdder"
... />
...
... </configure>
... """)

Now when text is transformed from text/html to text/x-html-safe, the filter will
be applied.

>>> str(self.portal.portal_transforms.convertTo('text/x-html-safe',
... 'test--test', mimetype='text/html', context=self.portal))
'test\xe2\x80\x94test'


How it works
============

``plone.outputfilters`` hooks into the PortalTransforms machinery by installing:

1. a new mimetype ("text/x-plone-outputfilters-html")
2. a transform from text/html to text/x-plone-outputfilters-html
3. a null transform from text/x-plone-outputfilters-html back to text/html
4. a "transform policy" for the text/x-html-safe mimetype, which says that text
being transformed to text/x-html-safe must first be transformed to
text/x-plone-outputfilters-html

The filter adapters are looked up and applied during the execution of the
transform from step #2.

This should be considered an implementation detail and may change at some point
in the future.

62 changes: 62 additions & 0 deletions plone/outputfilters/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
Adding a custom filter
======================

As an example, the following filter replaces all doubled hyphens ("--") with em
dashes ("—"). (Don't use the example verbatim, because it doesn't parse HTML to
apply itself only to text nodes, so will mangle HTML comments.)

A filter is a callable which accepts a UTF-8-encoded HTML string as input, and
returns a modified UTF-8-encoded HTML string. A return value of ``None`` may be
used to indicate that the input should not be modified.

.. include:: plone/outputfilters/filters/example.py
:literal:

The ``order`` attribute may be used to affect the order in which filters are
applied (higher values run later). The is_enabled method should return a boolean
indicating whether the filter should be applied.

Filters are registered in ZCML as a named multi-adapter of the context and
request to IFilter.

>>> from Products.Five.zcml import load_string
>>> load_string("""
... <configure
... xmlns="http://namespaces.zope.org/zope">
...
... <adapter
... name="em_dash_adder"
... provides="plone.outputfilters.interfaces.IFilter"
... for="* *"
... factory="plone.outputfilters.filters.example.EmDashAdder"
... />
...
... </configure>
... """)

Now when text is transformed from text/html to text/x-html-safe, the filter will
be applied.

>>> str(self.portal.portal_transforms.convertTo('text/x-html-safe',
... 'test--test', mimetype='text/html', context=self.portal))
'test\xe2\x80\x94test'


How it works
============

``plone.outputfilters`` hooks into the PortalTransforms machinery by installing:

1. a new mimetype ("text/x-plone-outputfilters-html")
2. a transform from text/html to text/x-plone-outputfilters-html
3. a null transform from text/x-plone-outputfilters-html back to text/html
4. a "transform policy" for the text/x-html-safe mimetype, which says that text
being transformed to text/x-html-safe must first be transformed to
text/x-plone-outputfilters-html

The filter adapters are looked up and applied during the execution of the
transform from step #2.

This should be considered an implementation detail and may change at some point
in the future.

2 changes: 1 addition & 1 deletion plone/outputfilters/tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_suite():
return unittest.TestSuite([

ztc.ZopeDocFileSuite(
'../../README.rst', package='plone.outputfilters',
'README.txt', package='plone.outputfilters',
test_class=OutputFiltersDocTestCase),

])
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
version=version,
description="Transformations applied to HTML in Plone text fields as they are rendered",
long_description=open("README.rst").read() + "\n" +
open(os.path.join("plone", "outputfilters",
"README.txt")).read() + "\n" +
open("CHANGES.txt").read(),
# Get more strings from
# http://pypi.python.org/pypi?%3Aaction=list_classifiers
Expand Down

0 comments on commit 26dbae7

Please sign in to comment.