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