summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/htmltidy.pm
blob: 185d01dd68c54a3db899ee299a7a5552ba62a519 (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. htmltidy => {
  24. type => "string",
  25. description => "tidy command line",
  26. safe => 0, # path
  27. rebuild => undef,
  28. },
  29. }
  30. sub checkconfig () {
  31. if (! defined $config{htmltidy}) {
  32. $config{htmltidy}="tidy -quiet -asxhtml -utf8 --show-body-only yes --show-warnings no --tidy-mark no --markup yes";
  33. }
  34. }
  35. sub sanitize (@) {
  36. my %params=@_;
  37. my $pid;
  38. my $sigpipe=0;
  39. $SIG{PIPE}=sub { $sigpipe=1 };
  40. $pid=open2(*IN, *OUT, "$config{htmltidy} 2>/dev/null");
  41. # open2 doesn't respect "use open ':utf8'"
  42. binmode (IN, ':utf8');
  43. binmode (OUT, ':utf8');
  44. print OUT $params{content};
  45. close OUT;
  46. local $/ = undef;
  47. my $ret=<IN>;
  48. close IN;
  49. waitpid $pid, 0;
  50. $SIG{PIPE}="DEFAULT";
  51. if ($sigpipe || ! defined $ret) {
  52. return gettext("htmltidy failed to parse this html");
  53. }
  54. return $ret;
  55. }
  56. 1