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