summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/htmltidy.pm
blob: 906c677dc1302391eebd9266d74aea20fe5b1d9c (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 $tries=10;
  19. my $pid;
  20. while (1) {
  21. eval {
  22. $pid=open2(*IN, *OUT, 'tidy -quiet -asxhtml -utf8 --show-body-only yes --show-warnings no --tidy-mark no');
  23. };
  24. last unless $@;
  25. $tries--;
  26. if ($tries < 1) {
  27. debug("failed to run tidy: $@");
  28. return $params{content};
  29. }
  30. }
  31. # open2 doesn't respect "use open ':utf8'"
  32. binmode (IN, ':utf8');
  33. binmode (OUT, ':utf8');
  34. print OUT $params{content};
  35. close OUT;
  36. local $/ = undef;
  37. my $ret=<IN>;
  38. close IN;
  39. waitpid $pid, 0;
  40. return $ret;
  41. } # }}}
  42. 1