X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=sweavealike.pm;h=d330a58706919225541363b9ec039b5acca261f0;hb=5c5c14bbd4505e7211039287e091522f7bc5c2d8;hp=2056e51fadc15b245466089d16f63c2e77162691;hpb=1ffc765a589a98c325c2337fe59680658659cabc;p=ikiwiki_plugins.git diff --git a/sweavealike.pm b/sweavealike.pm index 2056e51..d330a58 100644 --- a/sweavealike.pm +++ b/sweavealike.pm @@ -30,45 +30,87 @@ use Statistics::R; use IkiWiki '3.00'; +use Encode qw(decode); +use Digest::MD5 qw(md5_hex); + my $id = "sweavealike"; sub import { + hook(type => "getsetup", id => $id, call => \&getsetup); hook(type => "preprocess", id => $id, call => \&preprocess); - hook(type => "preprocess", id => $id, call => \&preprocess_scan, scan => 1); hook(type => "htmlize", id => $id, call => \&htmlize); hook(type => "savestate", id => $id, call => \&savestate); } -sub preprocess_scan { - my %param = @_; - # start the R process here for this page - if (not defined $pagestate{$param{page}}{$id}{R}) { - $pagestate{$param{page}}{$id}{R} = Statistics::R->new(shared => 1) or error("Unable to create an R process"); - } +sub getsetup { + return(plugin => {safe => 1, + rebuild => 1, + section => "misc", + link => "http://git.donarmstrong.com/?p=ikiwiki_plugins.git;a=blob;f=sweavealike.pm;hb=HEAD", + description => "sweavealike plugin", + }, + ); +} + +sub code_md5 { + my ($code) = @_; + return(md5_hex(decode('utf8',$code))); } sub preprocess { my %param = @_; + if (not defined $pagestate{$param{page}}{$id}{R}) { + $pagestate{$param{page}}{$id}{R} = Statistics::R->new(shared => 1) + or error("Unable to create an R process"); + } # we currently don't bother to support anything but outputing the # entire segment of code and its R output - if (not exists $param{code} or not defined $param{code}) { + if (not exists $param{code} + or not defined $param{code} + or not length $param{code}) { error("There wasn't any R code supplied"); } + + $param{width} = '400' unless exists $param{width} and defined $param{width}; + $param{height} = '400' unless exists $param{height} and defined $param{height}; + for (qw(width height)) { + if ($param{$_} !~ /^\d+$/) { + error("invalid $_; must be an integer: $param{$_}"); + } + } + if (exists $param{picture}) { + my $md5 = code_md5($param{code}."width=$param{width}height=$param{height}"); + my $page_esc = $params->{page}; + $page_esc =~ s/"/\\"/g; + $pagestate{$param{page}}{$id}{R}->run(qq{png(filename="$page_esc",width=$param{width},height=$param{height});}); + } my $code_result; eval { $code_result = $pagestate{$param{page}}{$id}{R}->run($param{code}); }; if ($@) { - error($@); + error("code '$param{code}' produced error '$@'"); } - my $output = "sweave output\n\n" - if (exists $param{verbatim}) { - $output = $param{code}; + if (exists $param{picture}) { + $pagestate{$param{page}}{$id}{R}->run(qq{dev.off();}); + } + if (exists $param{nooutput}) { + return(''); + } + my $output = ''; + if (exists $param{echo}) { + $output .= $param{code}; $output =~ s/^/> /mg; + $output .= "\n"; + } + if (exists $param{results}) { + $output .= $code_result; + } + if (exists $param{echo} or + exists $param{results}) { + $output =~ s/^/ /mg; } - $output .= $code_result; - $output =~ s/^/ /mg; return($output); }