summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/otl.pm
blob: 3801a6ec2417d225f3cf541a505ff79adedec8ef (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. section => "format",
  18. },
  19. }
  20. sub htmlize (@) {
  21. my %params=@_;
  22. # Munge up check boxes to look a little bit better.
  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. # Can't use open2 since otl2html doesn't play nice with buffering.
  30. # Instead, fork off a child process that will run otl2html and feed
  31. # it the content. Then read otl2html's response.
  32. my $tries=10;
  33. my $pid;
  34. do {
  35. $pid = open(KID_TO_READ, "-|");
  36. unless (defined $pid) {
  37. $tries--;
  38. if ($tries < 1) {
  39. debug("failed to fork: $@");
  40. return $params{content};
  41. }
  42. }
  43. } until defined $pid;
  44. if (! $pid) {
  45. $tries=10;
  46. $pid=undef;
  47. do {
  48. $pid = open(KID_TO_WRITE, "|-");
  49. unless (defined $pid) {
  50. $tries--;
  51. if ($tries < 1) {
  52. debug("failed to fork: $@");
  53. print $params{content};
  54. exit;
  55. }
  56. }
  57. } until defined $pid;
  58. if (! $pid) {
  59. if (! exec 'otl2html', '-S', '/dev/null', '-T', '/dev/stdin') {
  60. debug("failed to run otl2html: $@");
  61. print $params{content};
  62. exit;
  63. }
  64. }
  65. print KID_TO_WRITE $params{content};
  66. close KID_TO_WRITE;
  67. waitpid $pid, 0;
  68. exit;
  69. }
  70. local $/ = undef;
  71. my $ret=<KID_TO_READ>;
  72. close KID_TO_READ;
  73. waitpid $pid, 0;
  74. $ret=~s/.*<body>//s;
  75. $ret=~s/<body>.*//s;
  76. $ret=~s/<div class="Footer">.*//s;
  77. return $ret;
  78. }
  79. 1