summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/htmltidy.pm
blob: 9ba5e9592b0c84482e01175c5881f901315a4397 (plain)
  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. use warnings;
  10. use strict;
  11. use IkiWiki 2.00;
  12. use IPC::Open2;
  13. sub import { #{{{
  14. hook(type => "getsetup", id => "tidy", call => \&getsetup);
  15. hook(type => "sanitize", id => "tidy", call => \&sanitize);
  16. } # }}}
  17. sub getsetup () { #{{{
  18. return
  19. plugin => {
  20. safe => 1,
  21. rebuild => undef,
  22. },
  23. } #}}}
  24. sub sanitize (@) { #{{{
  25. my %params=@_;
  26. my $pid;
  27. my $sigpipe=0;
  28. $SIG{PIPE}=sub { $sigpipe=1 };
  29. $pid=open2(*IN, *OUT, 'tidy -quiet -asxhtml -utf8 --show-body-only yes --show-warnings no --tidy-mark no --markup yes 2>/dev/null');
  30. # open2 doesn't respect "use open ':utf8'"
  31. binmode (IN, ':utf8');
  32. binmode (OUT, ':utf8');
  33. print OUT $params{content};
  34. close OUT;
  35. local $/ = undef;
  36. my $ret=<IN>;
  37. close IN;
  38. waitpid $pid, 0;
  39. $SIG{PIPE}="DEFAULT";
  40. return "" if $sigpipe || ! defined $ret;
  41. return $ret;
  42. } # }}}
  43. 1