FB wrote:

On Fri, Sep 08, 2006 at 12:54:14PM +0200, Martijn Pieters wrote:
On 9/8/06, FB <[EMAIL PROTECTED]> wrote:
my employer want to have all external links marked with a small icon telling
anonymous users from the internet that everything behind given links is
beyond our responsibility.
Why not use a piece of javascript to do this? See the linkpopper
product on plone.org for a way to process all links in a page and
process them. That product makes external links open in a new window,
but the code should be easy to alter.

Thank you for the hint.

But there are several reasons for not using JS:
   * One of the constraints of that site is javascript being optional.
     Problem ist: marked links are mandatory - they have to be marked
     even with javascript turned off.
   * I'd like to have a tag-postprocessing namespace for some other
     reasons, too - e.g. for a printing-view that automatically creates
     a list of links at the end of the page.
   * I'd like to know, how to make a new pagetemplate namespace :-).

As a follow-on to Philipp's comment about WSGI middleware:

It could be fairly easy (to be sure, not *really* easy) to do a search-and-replace for link tags and do appropriate alterations in WSGI middleware.

If I were doing this, I might re-parse the document in middleware using lxml, then do an xpath query to find the locations in the document that need changing. Then, it would be a matter of using the elementtree API to make the changes. lxml.etree.tounicode() would put the page back together for output. Since outgoing pages generated with ZPT are generally parseable, this might work OK. N.B., You may have to use the DTD at some point with this method. Lxml will attempt to xml-minimize (e.g., <br/>) all tags without text content, so you will have to assure that this does not happen where it shouldn't.

If you are not too married to generating pages with ZPT, and if you like the elementtree API, there is also a possibility of a lxml/elementtree (python-only) method for generating pages, which can have a method for post-processing the page, in a manner like the above, except that the document stays in a parsed state until it is output by Zope. I have a working base class for this. I call it XTemplate. It's not-quite-ready-for-prime-time (no docs or tests, yet, and you have to do skinning within this framework), but if there is interest, I should be able to release a preview in the near future.

-Jim Washington
Zope3-users mailing list

Reply via email to