summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/htmltidy.pm
blob: 079da7b493d29d84b6ffdba6d4d06f0498ec88ec (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 %params=@_;
  18. my $tries=10;
  19. while (1) {
  20. eval {
  21. open2(*IN, *OUT, 'tidy -quiet -asxhtml -utf8 --show-body-only yes --show-warnings no --tidy-mark no');
  22. };
  23. last unless $@;
  24. $tries--;
  25. if ($tries < 1) {
  26. IkiWiki::debug("failed to run tidy: $@");
  27. return $params{content};
  28. }
  29. }
  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. return <IN>;
  37. } # }}}
  38. 1