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