tidy up new page_types code
[ikiwiki.git] / IkiWiki / Plugin / htmltidy.pm
blob6f3379ef457bddb7387c8485427ec88d51f5b0c5
1 #!/usr/bin/perl
2 # HTML Tidy plugin
3 # requires 'tidy' binary, found in Debian or http://tidy.sf.net/
4 # mostly a proof-of-concept on how to use external filters.
5 # It is particularly useful when the html plugin is used.
7 # by Faidon Liambotis
8 package IkiWiki::Plugin::htmltidy;
10 use warnings;
11 use strict;
12 use IkiWiki 3.00;
13 use IPC::Open2;
15 sub import {
16 hook(type => "getsetup", id => "tidy", call => \&getsetup);
17 hook(type => "sanitize", id => "tidy", call => \&sanitize);
20 sub getsetup () {
21 return
22 plugin => {
23 safe => 1,
24 rebuild => undef,
28 sub sanitize (@) {
29 my %params=@_;
31 my $pid;
32 my $sigpipe=0;
33 $SIG{PIPE}=sub { $sigpipe=1 };
34 $pid=open2(*IN, *OUT, 'tidy -quiet -asxhtml -utf8 --show-body-only yes --show-warnings no --tidy-mark no --markup yes 2>/dev/null');
36 # open2 doesn't respect "use open ':utf8'"
37 binmode (IN, ':utf8');
38 binmode (OUT, ':utf8');
40 print OUT $params{content};
41 close OUT;
43 local $/ = undef;
44 my $ret=<IN>;
45 close IN;
46 waitpid $pid, 0;
48 $SIG{PIPE}="DEFAULT";
49 return "" if $sigpipe || ! defined $ret;
51 return $ret;