summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/htmltidy.pm
blob: eb8f9d3d37efb4037d53ee7a23bcae168a5b8826 (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. IkiWiki::hook(type => "sanitize", id => "tidy", call => \&sanitize);
  15. } # }}}
  16. sub sanitize ($) { #{{{
  17. my $tries=10;
  18. while (1) {
  19. eval {
  20. open2(*IN, *OUT, 'tidy -quiet -asxhtml -utf8 --show-body-only yes --show-warnings no --tidy-mark no');
  21. };
  22. last unless $@;
  23. $tries--;
  24. if ($tries < 1) {
  25. IkiWiki::debug("failed to run tidy: $@");
  26. return shift;
  27. }
  28. }
  29. # open2 doesn't respect "use open ':utf8'"
  30. binmode (IN, ':utf8');
  31. binmode (OUT, ':utf8');
  32. print OUT shift;
  33. close OUT;
  34. local $/ = undef;
  35. return <IN>;
  36. } # }}}
  37. 1