summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/htmltidy.pm
blob: fc0d3b1d68067593851f3da8203ccdfcd87e344e (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 3.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. if ($sigpipe || ! defined $ret) {
  41. print STDERR gettext("warning: tidy failed")."\n";
  42. return "";
  43. }
  44. return $ret;
  45. }
  46. 1