Back to index

plone3  3.1.7
compress.py
Go to the documentation of this file.
00001 """
00002 In order to use Peter Bengtsson's slimmer library available from
00003 http://www.issuetrackerproduct.com/Download#slimmer
00004 you need to register the XHTMLSlimmer utility like this:
00005 
00006   <utility component="plone.memoize.compress.xhtmlslimmer"
00007            provides="plone.memoize.interfaces.IXHTMLCompressor" />
00008 
00009 You can register other XHTML-whitespace removal libraries as well.
00010 
00011 Per default there is no compressor available and we get the same string back:
00012 
00013    >>> html_string = u"<html><body><SPAN>Hello.</SPAN></body><html>"
00014    >>> xhtml_compress(html_string) is html_string
00015    True
00016 
00017 Make a stupid lowercasing compressor. This is not safe as it would lowercase
00018 all text outside of tags as well:
00019 
00020    >>> class LowerCaser(object):
00021    ...     implements(IXHTMLCompressor)
00022    ...
00023    ...     def compress(self, string):
00024    ...         return string.lower()
00025 
00026    >>> lower = LowerCaser()
00027 
00028 Register our new compressor:
00029 
00030    >>> from zope.component import getSiteManager
00031    >>> sm = getSiteManager()
00032    >>> sm.registerUtility(lower)
00033 
00034    >>> xhtml_compress(html_string)
00035    u'<html><body><span>hello.</span></body><html>'
00036 """
00037 
00038 from zope.component import queryUtility
00039 from zope.interface import implements
00040 
00041 from plone.memoize.interfaces import IXHTMLCompressor
00042 
00043 SLIMMER = True
00044 try:
00045     from slimmer import xhtml_slimmer
00046 except ImportError:
00047     SLIMMER = False
00048 
00049 
00050 def xhtml_compress(string):
00051     util = queryUtility(IXHTMLCompressor)
00052     if util is not None:
00053         return util.compress(string)
00054     return string
00055 
00056 
00057 class XHTMLSlimmer(object):
00058 
00059     implements(IXHTMLCompressor)
00060 
00061     def compress(self, string):
00062         if SLIMMER:
00063             return xhtml_slimmer(string)
00064         return string
00065 
00066 xhtmlslimmer = XHTMLSlimmer()