[Zope] Surpress white space?

Toby Dickenson tdickenson@geminidataloggers.com
Wed, 9 Jul 2003 15:46:49 +0100


On Wednesday 09 July 2003 15:37, Sergey Volobuev wrote:

> But it is still ugly :((

Soon after I started using Zope, I created a <dtml-unitws> tag that replaces 
blocks of whitespace with a single character. The code looks a little dated, 
but is still functional. Examle usage below, followed by code. I hope this 
helps.


<dtml-unitws>
  <dtml-in xxx>
    <dtml-if comecondition>
      <dtml-var somevariable>
    </dtml-if>
  </dtml-in>
</dtml-unitws>


from string import split, strip, join, replace
import DocumentTemplate.DT_Util
from DocumentTemplate.DT_String import String

class UnitWhitespaceTag:
    """
    Removes redundant whitespace (not very conservatively)
    """

    name='zcunitws'
    blockContinuations=()

    def __init__(self, blocks):
        tname, args, section = blocks[0]
        args = DocumentTemplate.DT_Util.parse_params(args)
        self.blocks = section.blocks

    def render(self,md):
        a = []
        for line in split(DocumentTemplate.DT_Util.render_blocks(self.blocks, 
md),'\n'):
            line = strip(line)
            if line:
                b = []
                for word in split(line,' '):
                    b.append(strip(word))
                a.append(join(b,' '))
        return join(a,'\n')

    __call__=render

String.commands['unitws']=UnitWhitespaceTag


-- 
Toby Dickenson
http://www.geminidataloggers.com/people/tdickenson