2 # Ikiwiki Sweave-alike plugin
3 # under the terms of the GPL version 2, or any later version at your
5 # Copyright 2012 by Don Armstrong <don@donarmstrong.com>.
8 package IkiWiki::Plugin::sweavealike;
12 sweavealike -- A Sweave-alike plugin which allows for embedding R code in IkiWiki
33 my $id = "sweavealike";
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);
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");
52 # we currently don't bother to support anything but outputing the
53 # entire segment of code and its R output
55 if (not exists $param{code} or not defined $param{code}) {
56 error("There wasn't any R code supplied");
60 $code_result = $pagestate{$param{page}}{$id}{R}->run($param{code});
66 if ($param{verbatim}) {
67 $output = $param{code};
70 $output .= $code_result;
75 # stop any started R processes here
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();
85 delete $pagestate{$param{page}}{$id}{R};
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;
98 delete $pagestate{$page}{$id}{R};