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