summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/otl.pm
blob: be339c88ebee44b77371422822ae4816a102cd5e (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. while (1) {
  27. eval {
  28. open2(*IN, *OUT, 'otl2html -S /dev/null -T /dev/stdin');
  29. };
  30. last unless $@;
  31. $tries--;
  32. if ($tries < 1) {
  33. IkiWiki::debug("failed to run otl2html: $@");
  34. return $params{content};
  35. }
  36. }
  37. # open2 doesn't respect "use open ':utf8'"
  38. binmode (IN, ':utf8');
  39. binmode (OUT, ':utf8');
  40. print OUT $params{content};
  41. close OUT;
  42. local $/ = undef;
  43. my $ret=<IN>;
  44. $ret=~s/.*<body>//s;
  45. $ret=~s/<body>.*//s;
  46. $ret=~s/<div class="Footer">.*//s;
  47. return $ret;
  48. } # }}}
  49. 1