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 => "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);
42 return(plugin => {safe => 1,
45 link => "http://git.donarmstrong.com/?p=ikiwiki_plugins.git;a=blob;f=sweavealike.pm;hb=HEAD",
46 description => "sweavealike plugin",
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");
58 # we currently don't bother to support anything but outputing the
59 # entire segment of code and its R output
61 if (not exists $param{code} or not defined $param{code}) {
62 error("There wasn't any R code supplied");
66 $code_result = $pagestate{$param{page}}{$id}{R}->run($param{code});
71 my $output = "sweave output\n\n";
72 if (exists $param{verbatim}) {
73 $output = $param{code};
76 $output .= $code_result;
81 # stop any started R processes here
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();
91 delete $pagestate{$param{page}}{$id}{R};
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;
104 delete $pagestate{$page}{$id}{R};