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