summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/htmltidy.pm
blob: 0609e72c328e5ea4fc06b945203f51f73e0e6338 (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;
  12. use IPC::Open2;
  13. sub import { #{{{
  14. hook(type => "sanitize", id => "tidy", call => \&sanitize);
  15. } # }}}
  16. sub sanitize (@) { #{{{
  17. my %params=@_;
  18. my $pid;
  19. my $sigpipe=0;
  20. $SIG{PIPE}=sub { $sigpipe=1 };
  21. $pid=open2(*IN, *OUT, 'tidy -quiet -asxhtml -utf8 --show-body-only yes --show-warnings no --tidy-mark no');
  22. # open2 doesn't respect "use open ':utf8'"
  23. binmode (IN, ':utf8');
  24. binmode (OUT, ':utf8');
  25. print OUT $params{content};
  26. close OUT;
  27. local $/ = undef;
  28. my $ret=<IN>;
  29. close IN;
  30. waitpid $pid, 0;
  31. return $params{content} if $sigpipe;
  32. $SIG{PIPE}="DEFAULT";
  33. return $ret;
  34. } # }}}
  35. 1