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