rendler has asked for the wisdom of the Perl Monks concerning the following question:

I currently have this bit of code in my autohandler file for indeng all the HTML that is outputted with the HTML tidy utility, First off to capture all of the page and send it off to work:
<%filter> s/^(.*?)$/tidy($1)/se; $filter
And the actual sub:
sub tidy { my ($output) = @_; use IPC::Open2; open2 *READER, *WRITER, "/usr/bin/tidy -i -w 0 -q -raw 2>/dev/nu +ll"; print WRITER $output; close WRITER; undef $/; $output = <READER>; close READER; return $output; }
This code seems to only be working in a standalone script on the system but doesn't when trying to use it with Mason. So I had to come up with an alternate method for the moment with:
sub tidy { my ($output) = @_; # Try to generate and unique filename using session_id and appen +ding # some more randomly generated stuff using md5_hex(). The same w +ay that # the session_id is generated in Apache::Session::Generate::MD5. my $tmp_file = "/tmp/$session{'_session_id'}-" . Digest::MD5::md +5_hex(Digest::MD5::md5_hex(time().{}. rand(). $$)); return "output file not correct ($tmp_file)." unless $tmp_file = +~ /^\/tmp\/[\w-]+$/; open TMP, ">$tmp_file" or die $!; chmod 0600, $tmp_file; print TMP $output; close TMP; $output = `/usr/bin/tidy -i -w 0 -q -raw $tmp_file 2>/dev/null`; unlink $tmp_file; return $output; }
Of couse this just a cheap hack of how I wanted it to work in the first place. So any help in getting the first sub working would be greatly appreciated. Thanks.

Replies are listed 'Best First'.
Re: Tidy with HTML::Mason
by Fletch (Bishop) on May 10, 2002 at 07:08 UTC

    If you're running under mod_perl, you might want to look into enabling stacked handlers and then chain a handler which runs tidy over the output of the real Mason handler. I believe the mod_perl Developer's Cookbook has an example of doing something similar (using HTML::Clean).

Re: Tidy with HTML::Mason
by IlyaM (Parson) on May 10, 2002 at 11:12 UTC