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}
62 or not defined $param{code}
63 or not length $param{code}) {
64 error("There wasn't any R code supplied");
68 $code_result = $pagestate{$param{page}}{$id}{R}->run($param{code});
71 error("code '$param{code}' produced error '$@'");
73 my $output = "sweave output\n\n";
74 if (exists $param{verbatim}) {
75 $output = $param{code};
78 $output .= $code_result;
83 # stop any started R processes here
86 if (exists $pagestate{$param{page}} and
87 exists $pagestate{$param{page}}{$id} and
88 exists $pagestate{$param{page}}{$id}{R}) {
89 if (defined $pagestate{$param{page}}{$id}{R}
90 and $pagestate{$param{page}}{$id}{R}->is_started()) {
91 $pagestate{$param{page}}{$id}{R}->stop();
93 delete $pagestate{$param{page}}{$id}{R};
98 # make sure we never try to save an R process
99 for my $page (keys %pagestate) {
100 next unless exists $pagestate{$page}{$id};
101 next unless exists $pagestate{$page}{$id}{R};
102 if (defined $pagestate{$page}{$id}{R}
103 and $pagestate{$page}{$id}{R}->is_started()) {
104 $pagestate{$page}{$id}{R}->stop;
106 delete $pagestate{$page}{$id}{R};