summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/otl.pm
blob: 0cd93a3ecc773248e82e9180f7ae8a6c319198fa (plain)
  1. #!/usr/bin/perl
  2. # outline markup
  3. package IkiWiki::Plugin::otl;
  4. use warnings;
  5. use strict;
  6. use IkiWiki;
  7. use IPC::Open2;
  8. sub import { #{{{
  9. IkiWiki::hook(type => "filter", id => "otl", call => \&filter);
  10. IkiWiki::hook(type => "htmlize", id => "otl", call => \&htmlize);
  11. } # }}}
  12. sub filter (@) { #{{{
  13. my %params=@_;
  14. # Munge up check boxes to look a little bit better. This is a hack.
  15. my $checked=IkiWiki::htmllink($params{page}, $params{page},
  16. "smileys/star_on.png", 0);
  17. my $unchecked=IkiWiki::htmllink($params{page}, $params{page},
  18. "smileys/star_off.png", 0);
  19. $params{content}=~s/^(\s*)\[X\]\s/${1}$checked /mg;
  20. $params{content}=~s/^(\s*)\[_\]\s/${1}$unchecked /mg;
  21. return $params{content};
  22. } # }}}
  23. sub htmlize (@) { #{{{
  24. my %params=@_;
  25. my $tries=10;
  26. my $pid;
  27. while (1) {
  28. eval {
  29. $pid=open2(*IN, *OUT, 'otl2html -S /dev/null -T /dev/stdin');
  30. };
  31. last unless $@;
  32. $tries--;
  33. if ($tries < 1) {
  34. IkiWiki::debug("failed to run otl2html: $@");
  35. return $params{content};
  36. }
  37. }
  38. # open2 doesn't respect "use open ':utf8'"
  39. binmode (IN, ':utf8');
  40. binmode (OUT, ':utf8');
  41. print OUT $params{content};
  42. close OUT;
  43. local $/ = undef;
  44. my $ret=<IN>;
  45. close IN;
  46. waitpid $pid, 0;
  47. $ret=~s/.*<body>//s;
  48. $ret=~s/<body>.*//s;
  49. $ret=~s/<div class="Footer">.*//s;
  50. return $ret;
  51. } # }}}
  52. 1