From ba8fd5520052c61c5eed8384453f374f81088bcc Mon Sep 17 00:00:00 2001 From: Don Armstrong Date: Tue, 20 Mar 2012 17:38:53 -0700 Subject: [PATCH] add sweavealike --- agimg.pm | 50 +++++++++++++++++++++ sweavealike.pm | 117 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 agimg.pm create mode 100644 sweavealike.pm diff --git a/agimg.pm b/agimg.pm new file mode 100644 index 0000000..ea026a2 --- /dev/null +++ b/agimg.pm @@ -0,0 +1,50 @@ +#!/usr/bin/perl +# Ikiwiki Apache::Gallery plugin +# Don Armstrong 2011/11/26 + +package IkiWiki::Plugin::agimg; + +use warnings; +use strict; + +use IkiWiki 3.00; + +use URI::Escape qw(uri_escape_utf8); +use HTML::Entities qw(encode_entities); + +sub import { + hook(type => "getsetup", id => "agimg", call => \&getsetup); + hook(type => "checkconfig", id => "agimg", call => \&checkconfig); + hook(type => "preprocess", id => "agimg", call => \&preprocess); +} + +sub getsetup () { + return (plugin => {safe => 1, + rebuild => undef, + section => "widget", + }, + agimgprefix => {type => "string", + example => "/gallery/", + description => "Base URI of Apache Gallery site", + advanced => 0, + safe => 0, + rebuild => 1, + }, + ); +} + +sub checkconfig() { + if (! defined $config{agimgprefix}) { + $config{agimgprefix} = "" + } +} + + +sub preprocess(@) { + my ($url) = $_[0] =~ /^(.+)$/; # anything is ok + my $complete_url = $config{agimgprefix}.$url; #encode_entities($config{agimgprefix} . $url); + return ""; +} + + +1; diff --git a/sweavealike.pm b/sweavealike.pm new file mode 100644 index 0000000..c8bcbad --- /dev/null +++ b/sweavealike.pm @@ -0,0 +1,117 @@ +#!/usr/bin/perl +# Ikiwiki Sweave-alike plugin +# under the terms of the GPL version 2, or any later version at your +# option. +# Copyright 2012 by Don Armstrong . + + +package IkiWiki::Plugin::sweavealike; + +=head1 NAME + +sweavealike -- A Sweave-alike plugin which allows for embedding R code in IkiWiki + +=head1 SYNOPSIS + + +=head1 DESCRIPTION + + +=head1 BUGS + +None known. + +=cut + +use warnings; +use strict; + +use Statistics::R; + +use IkiWIki '3.00'; + +my $id = "sweavealike"; +sub import { + 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 preprocess { + my %param = @_; + + # 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}) { + error("There wasn't any R code supplied"); + } + my $code_result; + eval { + $code_result = $pagestate{$param{page}}{$id}{R}->run($param{code}); + }; + if ($@) { + error($@); + } + my $output; + if ($param{verbatim}) { + $output = $param{code}; + $output =~ s/^/> /mg; + } + $output .= $code_result; + $output =~ s/^/ /mg; + return($output); +} + +# stop any started R processes here +sub htmlize { + my %param = @_; + if (exists $pagestate{$param{page}} and + exists $pagestate{$param{page}}{$id} and + exists $pagestate{$param{page}}{$id}{R}) { + if (defined $pagestate{$param{page}}{$id}{R} + and $pagestate{$param{page}}{$id}{R}->is_started()) { + $pagestate{$param{page}}{$id}{R}->stop(); + } + delete $pagestate{$param{page}}{$id}{R}; + } +} + +sub savestate { + # make sure we never try to save an R process + for my $page (keys %pagestate) { + next unless exists $pagestate{$page}{$id}; + next unless exists $pagestate{$page}{$id}{R}; + if (defined $pagestate{$page}{$id}{R} + and $pagestate{$param{page}}{$id}{R}->is_started()) { + $pagestate{$page}{$id}{R}->stop; + } + delete $pagestate{$page}{$id}{R}; + } +} + + + + + +1; + + +__END__ + + + + + + + + -- 2.39.2