summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/otl.pm
blob: 94a2b80f6ef16048b6c5ff87e168255dd746145e (plain)
  1. #!/usr/bin/perl
  2. # outline markup
  3. package IkiWiki::Plugin::otl;
  4. use warnings;
  5. use strict;
  6. use IkiWiki;
  7. sub import { #{{{
  8. hook(type => "filter", id => "otl", call => \&filter);
  9. hook(type => "htmlize", id => "otl", call => \&htmlize);
  10. } # }}}
  11. sub filter (@) { #{{{
  12. my %params=@_;
  13. # Munge up check boxes to look a little bit better. This is a hack.
  14. my $checked=htmllink($params{page}, $params{page},
  15. "smileys/star_on.png", linktext => "[X]");
  16. my $unchecked=htmllink($params{page}, $params{page},
  17. "smileys/star_off.png", linktext => "[_]");
  18. $params{content}=~s/^(\s*)\[X\]\s/${1}$checked /mg;
  19. $params{content}=~s/^(\s*)\[_\]\s/${1}$unchecked /mg;
  20. return $params{content};
  21. } # }}}
  22. sub htmlize (@) { #{{{
  23. my %params=@_;
  24. # Can't use open2 since otl2html doesn't play nice with buffering.
  25. # Instead, fork off a child process that will run otl2html and feed
  26. # it the content. Then read otl2html's response.
  27. my $tries=10;
  28. my $pid;
  29. do {
  30. $pid = open(KID_TO_READ, "-|");
  31. unless (defined $pid) {
  32. $tries--;
  33. if ($tries < 1) {
  34. debug("failed to fork: $@");
  35. return $params{content};
  36. }
  37. }
  38. } until defined $pid;
  39. if (! $pid) {
  40. $tries=10;
  41. $pid=undef;
  42. do {
  43. $pid = open(KID_TO_WRITE, "|-");
  44. unless (defined $pid) {
  45. $tries--;
  46. if ($tries < 1) {
  47. debug("failed to fork: $@");
  48. print $params{content};
  49. exit;
  50. }
  51. }
  52. } until defined $pid;
  53. if (! $pid) {
  54. if (! exec 'otl2html', '-S', '/dev/null', '-T', '/dev/stdin') {
  55. debug("failed to run otl2html: $@");
  56. print $params{content};
  57. exit;
  58. }
  59. }
  60. print KID_TO_WRITE $params{content};
  61. close KID_TO_WRITE;
  62. waitpid $pid, 0;
  63. exit;
  64. }
  65. local $/ = undef;
  66. my $ret=<KID_TO_READ>;
  67. close KID_TO_READ;
  68. waitpid $pid, 0;
  69. $ret=~s/.*<body>//s;
  70. $ret=~s/<body>.*//s;
  71. $ret=~s/<div class="Footer">.*//s;
  72. return $ret;
  73. } # }}}
  74. 1