This is my implementation of an external method that calls HTML Tidy. I hope it is of some use.

This is a fragment of a form processing script that calls an External Method, passing the body content of a html page:

if request.submit == 'Tidy':
    (messages, buffer) = context.calltidy(body)
    request.set('messages', messages)
    request.set('body', buffer)

The body and any HTML Tidy messages are passed back to the form for display. The external method module name is caltidy and the function name is tidy.

This is the external method that calls HTML Tidy. It prepends a suitable head and appends a suitable tail to the body (shich happens to be part of a table) to make a valid html document, then chops them off the result for return to the caller:

# call htmltidy
import os
import string

# path to executable
#exe = '/usr/local/zopeinst/Extensions/htmltidy/tidy'
exe = '/disk/home/www/zopeinst/Extensions/htmltidy/tidy'

# path to HTML Tidy configuration file
#config = '/usr/local/zopeinst/Extensions/htmltidy/config.txt'
config = '/disk/home/www/zopeinst/Extensions/htmltidy/config.txt'
errors = ''

dummyhead = """
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "";>
<html lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<table summary="dummy table"><tr><td>
<!-- dummyhead -->
dummytail = """
<!-- dummytail -->
parsefailed = """
Tidy has not changed the source. This will happen if the input
contains any tags not recognised by the html specification. Do
not use Tidy on pages containing dtml! Correct only the errors
listed below and then run Tidy again. Tidy will convert font tags
to style classes, which will be ignored unless you create a local
style sheet. Tidy will also correct many other faults that lead
to the warnings below. The first line of your text is line 11.


def tidy(input):
    combo = dummyhead + input + dummytail
    command = "%s -config %s 2>&1 << .\n%s" % (exe, config, combo)
    buffer = ''
    # get results from tidy into buffer
    for line in os.popen(command).readlines():
        buffer += line
    # if anything goes wrong with the tidy call the line count is zero
        # chop the dummy head
        (head, buffer) = string.split(buffer, '<!-- dummyhead -->')
        # keep the warnings part of the head
        (head, tail) = string.split(head, '<!DOCTYPE')
        # and chop the dummy tail
        (buffer, tail) = string.split(buffer, '<!-- dummytail -->')
        return (head, buffer)
        return (parsefailed + buffer, input)


gf wrote:
I am a Zope newbie. I apologize if the terminology I use below is not
correct, but I hope that you get the gist of what I am about to ask.

I would like to be able to 'filter' results before returning them to
the user.  In other words, if a user requests a given object, it is
passed through a filter before being passed on to him or her. As a
simple example, I may want to tidy the html before the user sees it,
or eliminate or add certain tags or words.

I have put together a simple 'html tidy' external method that takes
raw html and produces a tidied version, but I don't know how I can
apply it.

Thank you for any comments, suggestions, or simple examples you may be
able to provide.

Best  Regards,
Zope maillist  -
**   No cross posts or HTML encoding!  **
(Related lists - )

Zope maillist  -
**   No cross posts or HTML encoding!  **
(Related lists - )

Reply via email to