]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/htmltidy.pm
estseek patch (slightly altered) and other replies
[ikiwiki.git] / IkiWiki / Plugin / htmltidy.pm
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.
6 #
7 # by Faidon Liambotis
8 package IkiWiki::Plugin::htmltidy;
9
10 use warnings;
11 use strict;
12 use IkiWiki;
13 use IPC::Open2;
14
15 sub import { #{{{
16         hook(type => "sanitize", id => "tidy", call => \&sanitize);
17 } # }}}
18
19 sub sanitize (@) { #{{{
20         my %params=@_;
21
22         my $tries=10;
23         my $pid;
24         while (1) {
25                 eval {
26                         $pid=open2(*IN, *OUT, 'tidy -quiet -asxhtml -utf8 --show-body-only yes --show-warnings no --tidy-mark no');
27                 };
28                 last unless $@;
29                 $tries--;
30                 if ($tries < 1) {
31                         debug("failed to run tidy: $@");
32                         return $params{content};
33                 }
34         }
35         # open2 doesn't respect "use open ':utf8'"
36         binmode (IN, ':utf8'); 
37         binmode (OUT, ':utf8'); 
38         
39         print OUT $params{content};
40         close OUT;
41
42         local $/ = undef;
43         my $ret=<IN>;
44         close IN;
45         waitpid $pid, 0;
46
47         return $ret;
48 } # }}}
49
50 1