2006-07-29 22:50:20 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# HTML Tidy plugin
|
|
|
|
# requires 'tidy' binary, found in Debian or http://tidy.sf.net/
|
|
|
|
# mostly a proof-of-concept on how to use external filters.
|
|
|
|
# It is particularly useful when the html plugin is used.
|
|
|
|
#
|
|
|
|
# by Faidon Liambotis
|
2006-08-04 09:04:16 +02:00
|
|
|
package IkiWiki::Plugin::htmltidy;
|
2006-07-29 22:50:20 +02:00
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2006-07-29 22:50:20 +02:00
|
|
|
use IPC::Open2;
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2008-08-03 22:40:12 +02:00
|
|
|
hook(type => "getsetup", id => "tidy", call => \&getsetup);
|
2006-09-10 00:50:27 +02:00
|
|
|
hook(type => "sanitize", id => "tidy", call => \&sanitize);
|
2010-10-31 02:02:38 +01:00
|
|
|
hook(type => "checkconfig", id => "tidy", call => \&checkconfig);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-07-29 22:50:20 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup () {
|
2008-08-03 22:40:12 +02:00
|
|
|
return
|
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => undef,
|
|
|
|
},
|
2010-09-30 13:53:37 +02:00
|
|
|
htmltidy => {
|
|
|
|
type => "string",
|
|
|
|
description => "tidy command line",
|
|
|
|
safe => 0, # path
|
2010-10-02 18:02:34 +02:00
|
|
|
rebuild => undef,
|
2010-09-30 13:53:37 +02:00
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-08-03 22:40:12 +02:00
|
|
|
|
2010-10-02 18:02:34 +02:00
|
|
|
sub checkconfig () {
|
|
|
|
if (! defined $config{htmltidy}) {
|
|
|
|
$config{htmltidy}="tidy -quiet -asxhtml -utf8 --show-body-only yes --show-warnings no --tidy-mark no --markup yes";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub sanitize (@) {
|
2006-08-28 20:17:59 +02:00
|
|
|
my %params=@_;
|
|
|
|
|
2006-09-03 22:11:39 +02:00
|
|
|
my $pid;
|
2006-09-25 23:38:25 +02:00
|
|
|
my $sigpipe=0;
|
|
|
|
$SIG{PIPE}=sub { $sigpipe=1 };
|
2010-09-30 13:53:37 +02:00
|
|
|
$pid=open2(*IN, *OUT, "$config{htmltidy} 2>/dev/null");
|
2008-09-27 20:27:16 +02:00
|
|
|
|
2006-07-29 22:50:20 +02:00
|
|
|
# open2 doesn't respect "use open ':utf8'"
|
2006-09-25 23:38:25 +02:00
|
|
|
binmode (IN, ':utf8');
|
2008-09-27 20:27:16 +02:00
|
|
|
binmode (OUT, ':utf8');
|
2006-08-04 09:04:16 +02:00
|
|
|
|
2006-08-28 20:17:59 +02:00
|
|
|
print OUT $params{content};
|
2006-07-29 22:50:20 +02:00
|
|
|
close OUT;
|
|
|
|
|
|
|
|
local $/ = undef;
|
2006-09-03 22:11:39 +02:00
|
|
|
my $ret=<IN>;
|
|
|
|
close IN;
|
|
|
|
waitpid $pid, 0;
|
|
|
|
|
2006-09-25 23:38:25 +02:00
|
|
|
$SIG{PIPE}="DEFAULT";
|
2009-08-26 19:24:51 +02:00
|
|
|
if ($sigpipe || ! defined $ret) {
|
2009-08-27 22:27:57 +02:00
|
|
|
return gettext("htmltidy failed to parse this html");
|
2009-08-26 19:24:51 +02:00
|
|
|
}
|
2006-09-25 23:38:25 +02:00
|
|
|
|
2006-09-03 22:11:39 +02:00
|
|
|
return $ret;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-07-29 22:50:20 +02:00
|
|
|
|
|
|
|
1
|