]> git.donarmstrong.com Git - ikiwiki_plugins.git/blob - sweavealike.pm
ec6660c09e77e4d26dbb32b3129b8db073aacbc7
[ikiwiki_plugins.git] / sweavealike.pm
1 #!/usr/bin/perl
2 # Ikiwiki Sweave-alike plugin
3 # under the terms of the GPL version 2, or any later version at your
4 # option.
5 # Copyright 2012 by Don Armstrong <don@donarmstrong.com>.
6
7
8 package IkiWiki::Plugin::sweavealike;
9
10 =head1 NAME
11
12 sweavealike -- A Sweave-alike plugin which allows for embedding R code in IkiWiki
13
14 =head1 SYNOPSIS
15
16
17 =head1 DESCRIPTION
18
19
20 =head1 BUGS
21
22 None known.
23
24 =cut
25
26 use warnings;
27 use strict;
28
29 use Statistics::R;
30
31 use IkiWiki '3.00';
32
33 my $id = "sweavealike";
34 sub import {
35     hook(type => "getsetup", id => $id, call => \&getsetup);
36     hook(type => "preprocess", id => $id, call => \&preprocess);
37     hook(type => "htmlize", id => $id, call => \&htmlize);
38     hook(type => "savestate", id => $id, call => \&savestate);
39 }
40
41 sub getsetup {
42     return(plugin => {safe => 1,
43                       rebuild => 1,
44                       section => "misc",
45                       link => "http://git.donarmstrong.com/?p=ikiwiki_plugins.git;a=blob;f=sweavealike.pm;hb=HEAD",
46                       description => "sweavealike plugin",
47                      },
48           );
49 }
50
51 sub preprocess {
52     my %param = @_;
53
54     if (not defined $pagestate{$param{page}}{$id}{R}) {
55         $pagestate{$param{page}}{$id}{R} = Statistics::R->new(shared => 1)
56             or error("Unable to create an R process");
57     }
58     # we currently don't bother to support anything but outputing the
59     # entire segment of code and its R output
60
61     if (not exists $param{code} or not defined $param{code}) {
62         error("There wasn't any R code supplied");
63     }
64     my $code_result;
65     eval {
66         $code_result = $pagestate{$param{page}}{$id}{R}->run($param{code});
67     };
68     if ($@) {
69         error($@);
70     }
71     my $output = "sweave output\n\n";
72     if (exists $param{verbatim}) {
73         $output = $param{code};
74         $output =~ s/^/> /mg;
75     }
76     $output .= $code_result;
77     $output =~ s/^/    /mg;
78     return($output);
79 }
80
81 # stop any started R processes here
82 sub htmlize {
83     my %param = @_;
84     if (exists $pagestate{$param{page}} and
85         exists $pagestate{$param{page}}{$id} and
86         exists $pagestate{$param{page}}{$id}{R}) {
87         if (defined $pagestate{$param{page}}{$id}{R}
88             and $pagestate{$param{page}}{$id}{R}->is_started()) {
89             $pagestate{$param{page}}{$id}{R}->stop();
90         }
91         delete $pagestate{$param{page}}{$id}{R};
92     }
93 }
94
95 sub savestate {
96     # make sure we never try to save an R process
97     for my $page (keys %pagestate) {
98         next unless exists $pagestate{$page}{$id};
99         next unless exists $pagestate{$page}{$id}{R};
100         if (defined $pagestate{$page}{$id}{R}
101             and $pagestate{$page}{$id}{R}->is_started()) {
102             $pagestate{$page}{$id}{R}->stop;
103         }
104         delete $pagestate{$page}{$id}{R};
105     }
106 }
107
108
109
110
111
112 1;
113
114
115 __END__
116
117
118
119
120
121
122
123