fix syntax error with corrupt data
[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 use open qw{:utf8 :std};
9
10 sub import { #{{{
11         hook(type => "filter", id => "otl", call => \&filter);
12         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=htmllink($params{page}, $params{page},
21                 "smileys/star_on.png", linktext => "[X]");
22         my $unchecked=htmllink($params{page}, $params{page},
23                 "smileys/star_off.png", linktext => "[_]");
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         # Can't use open2 since otl2html doesn't play nice with buffering.
34         # Instead, fork off a child process that will run otl2html and feed
35         # it the content. Then read otl2html's response.
36
37         my $tries=10;
38         my $pid;
39         do {
40                 $pid = open(KID_TO_READ, "-|");
41                 unless (defined $pid) {
42                         $tries--;
43                         if ($tries < 1) {
44                                 debug("failed to fork: $@");
45                                 return $params{content};
46                         }
47                 }
48         } until defined $pid;
49
50         if (! $pid) {
51                 $tries=10;
52                 $pid=undef;
53
54                 do {
55                         $pid = open(KID_TO_WRITE, "|-");
56                         unless (defined $pid) {
57                                 $tries--;
58                                 if ($tries < 1) {
59                                         debug("failed to fork: $@");
60                                         print $params{content};
61                                         exit;
62                                 }
63                         }
64                 } until defined $pid;
65
66                 if (! $pid) {
67                         if (! exec 'otl2html', '-S', '/dev/null', '-T', '/dev/stdin') {
68                                 debug("failed to run otl2html: $@");
69                                 print $params{content};
70                                 exit;
71                         }
72                 }
73
74                 print KID_TO_WRITE $params{content};
75                 close KID_TO_WRITE;
76                 waitpid $pid, 0;
77                 exit;
78         }
79         
80         local $/ = undef;
81         my $ret=<KID_TO_READ>;
82         close KID_TO_READ;
83         waitpid $pid, 0;
84
85         $ret=~s/.*<body>//s;
86         $ret=~s/<body>.*//s;
87         $ret=~s/<div class="Footer">.*//s;
88         return $ret;
89 } # }}}
90
91 1