fixups tidy change
[ikiwiki] / 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 3.00;
13 use IPC::Open2;
14
15 sub import {
16         hook(type => "getsetup", id => "tidy", call => \&getsetup);
17         hook(type => "sanitize", id => "tidy", call => \&sanitize);
18 }
19
20 sub getsetup () {
21         return
22                 plugin => {
23                         safe => 1,
24                         rebuild => undef,
25                 },
26                 htmltidy => {
27                         type => "string",
28                         description => "tidy command line",
29                         safe => 0, # path
30                         rebuild => undef,
31                 },
32 }
33
34 sub checkconfig () {
35         if (! defined $config{htmltidy}) {
36                 $config{htmltidy}="tidy -quiet -asxhtml -utf8 --show-body-only yes --show-warnings no --tidy-mark no --markup yes";
37         }
38 }
39
40 sub sanitize (@) {
41         my %params=@_;
42
43         my $pid;
44         my $sigpipe=0;
45         $SIG{PIPE}=sub { $sigpipe=1 };
46         $pid=open2(*IN, *OUT, "$config{htmltidy} 2>/dev/null");
47
48         # open2 doesn't respect "use open ':utf8'"
49         binmode (IN, ':utf8');
50         binmode (OUT, ':utf8');
51         
52         print OUT $params{content};
53         close OUT;
54
55         local $/ = undef;
56         my $ret=<IN>;
57         close IN;
58         waitpid $pid, 0;
59
60         $SIG{PIPE}="DEFAULT";
61         if ($sigpipe || ! defined $ret) {
62                 return gettext("htmltidy failed to parse this html");
63         }
64
65         return $ret;
66 }
67
68 1