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