]> git.donarmstrong.com Git - ikiwiki_plugins.git/blob - sweavealike.pm
fix wrong page in hashref
[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 => "preprocess", id => $id, call => \&preprocess);
36     hook(type => "preprocess", id => $id, call => \&preprocess_scan, scan => 1);
37     hook(type => "htmlize", id => $id, call => \&htmlize);
38     hook(type => "savestate", id => $id, call => \&savestate);
39 }
40
41 sub preprocess_scan {
42     my %param = @_;
43     # start the R process here for this page
44     if (not defined $pagestate{$param{page}}{$id}{R}) {
45         $pagestate{$param{page}}{$id}{R} = Statistics::R->new(shared => 1) or error("Unable to create an R process");
46     }
47 }
48
49 sub preprocess {
50     my %param = @_;
51
52     # we currently don't bother to support anything but outputing the
53     # entire segment of code and its R output
54
55     if (not exists $param{code} or not defined $param{code}) {
56         error("There wasn't any R code supplied");
57     }
58     my $code_result;
59     eval {
60         $code_result = $pagestate{$param{page}}{$id}{R}->run($param{code});
61     };
62     if ($@) {
63         error($@);
64     }
65     my $output;
66     if ($param{verbatim}) {
67         $output = $param{code};
68         $output =~ s/^/> /mg;
69     }
70     $output .= $code_result;
71     $output =~ s/^/    /mg;
72     return($output);
73 }
74
75 # stop any started R processes here
76 sub htmlize {
77     my %param = @_;
78     if (exists $pagestate{$param{page}} and
79         exists $pagestate{$param{page}}{$id} and
80         exists $pagestate{$param{page}}{$id}{R}) {
81         if (defined $pagestate{$param{page}}{$id}{R}
82             and $pagestate{$param{page}}{$id}{R}->is_started()) {
83             $pagestate{$param{page}}{$id}{R}->stop();
84         }
85         delete $pagestate{$param{page}}{$id}{R};
86     }
87 }
88
89 sub savestate {
90     # make sure we never try to save an R process
91     for my $page (keys %pagestate) {
92         next unless exists $pagestate{$page}{$id};
93         next unless exists $pagestate{$page}{$id}{R};
94         if (defined $pagestate{$page}{$id}{R}
95             and $pagestate{$page}{$id}{R}->is_started()) {
96             $pagestate{$page}{$id}{R}->stop;
97         }
98         delete $pagestate{$page}{$id}{R};
99     }
100 }
101
102
103
104
105
106 1;
107
108
109 __END__
110
111
112
113
114
115
116
117