summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/htmltidy.pm
blob: 1108aeb894e658a35eff011975c1c0b50d14a439 (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. my $pid;
  39. my $sigpipe=0;
  40. $SIG{PIPE}=sub { $sigpipe=1 };
  41. $pid=open2(*IN, *OUT, "$config{htmltidy} 2>/dev/null");
  42. # open2 doesn't respect "use open ':utf8'"
  43. binmode (IN, ':utf8');
  44. binmode (OUT, ':utf8');
  45. print OUT $params{content};
  46. close OUT;
  47. local $/ = undef;
  48. my $ret=<IN>;
  49. close IN;
  50. waitpid $pid, 0;
  51. $SIG{PIPE}="DEFAULT";
  52. if ($sigpipe || ! defined $ret) {
  53. return gettext("htmltidy failed to parse this html");
  54. }
  55. return $ret;
  56. }
  57. 1