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