* Patch from James Westby to add an actions option to inline; this
[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;
8 use IPC::Open2;
9
10 sub import { #{{{
11         IkiWiki::hook(type => "filter", id => "otl", call => \&filter);
12         IkiWiki::hook(type => "htmlize", id => "otl", call => \&htmlize);
13
14 } # }}}
15
16 sub filter (@) { #{{{
17         my %params=@_;
18         
19         # Munge up check boxes to look a little bit better. This is a hack.
20         my $checked=IkiWiki::htmllink($params{page}, $params{page},
21                 "smileys/star_on.png", 0);
22         my $unchecked=IkiWiki::htmllink($params{page}, $params{page},
23                 "smileys/star_off.png", 0);
24         $params{content}=~s/^(\s*)\[X\]\s/${1}$checked /mg;
25         $params{content}=~s/^(\s*)\[_\]\s/${1}$unchecked /mg;
26         
27         return $params{content};
28 } # }}}
29
30 sub htmlize (@) { #{{{
31         my %params=@_;
32
33         my $tries=10;
34         while (1) {
35                 eval {
36                         open2(*IN, *OUT, 'otl2html -S /dev/null -T /dev/stdin');
37                 };
38                 last unless $@;
39                 $tries--;
40                 if ($tries < 1) {
41                         IkiWiki::debug("failed to run otl2html: $@");
42                         return $params{content};
43                 }
44         }
45         # open2 doesn't respect "use open ':utf8'"
46         binmode (IN, ':utf8'); 
47         binmode (OUT, ':utf8'); 
48         
49         print OUT $params{content};
50         close OUT;
51
52         local $/ = undef;
53         my $ret=<IN>;
54         $ret=~s/.*<body>//s;
55         $ret=~s/<body>.*//s;
56         $ret=~s/<div class="Footer">.*//s;
57         return $ret;
58 } # }}}
59
60 1