Hi, 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, gyro
gf wrote at 2006-4-6 12:06 -0600:
Hi, 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.
The current Zope version does not support general post processing. Depending on your precise use cases, you have various options: * change "ZPublisher.HTTPResponse.HTTPResponse" to perform postprocessing it its "setBody". * change "ZPublisher.Publish.publish_module" to add post processing * use a PythonScript and its "traverse_subpath" feature. When traversal reaches a PythonScript, then traversal stops and the remaining traversal steps are made available in the scripts "traverse_subpath". The script can try to locate the object identified by the "traverse_subpath" and call it. It then can perform post processing on the result. This usually works for templates and parameterless scripts. Passing parameters to scripts is difficult. You probably would like to make "ZPublisher.mapply.mapply" available in untrusted code for this. The approach will also fail when the user can only be authenticated below the script.
Thank you for any comments, suggestions, or simple examples you may be able to provide.
Best Regards, gyro
-- Dieter
On 4/7/06, Dieter Maurer <dieter@handshake.de> wrote:
gf wrote at 2006-4-6 12:06 -0600:
Hi, 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.
The current Zope version does not support general post processing.
Depending on your precise use cases, you have various options:
* change "ZPublisher.HTTPResponse.HTTPResponse" to perform postprocessing it its "setBody".
* change "ZPublisher.Publish.publish_module" to add post processing
* use a PythonScript and its "traverse_subpath" feature.
When traversal reaches a PythonScript, then traversal stops and the remaining traversal steps are made available in the scripts "traverse_subpath".
The script can try to locate the object identified by the "traverse_subpath" and call it. It then can perform post processing on the result.
This usually works for templates and parameterless scripts. Passing parameters to scripts is difficult. You probably would like to make "ZPublisher.mapply.mapply" available in untrusted code for this.
The approach will also fail when the user can only be authenticated below the script. -- Dieter
Hi Dieter, Thank you for the ideas! I will check into them and see if I can make anything work for my application. Best Regards, gyro
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) return 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" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Testing</title> </head> <body> <table summary="dummy table"><tr><td> <!-- dummyhead --> """ dummytail = """ <!-- dummytail --> </td></tr></table> </body> </html> . """ 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 try: # 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) except: return (parsefailed + buffer, input) Cliff gf wrote:
Hi, 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, gyro _______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
participants (3)
-
Cliff Ford -
Dieter Maurer -
gf