#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw/warningsToBrowser fatalsToBrowser/;
use HTML::Template;
use FindBin qw/$Bin/;
use Storable;
use Text::Textile qw(textile);
my $q = new CGI;
my $form = $q->Vars();
my $action = $form->{'action'};
my $wiki = -e "$Bin/wiki.db" ? retrieve("$Bin/wiki.db") : {} ;
my $wikiwords = qr/\b([A-Z]\w+[A-Z]\w+)/;
my $topic = $form->{topic} || 'FrontPage';
my $entry = $form->{entry} || $wiki->{$topic};
my $template = HTML::Template->new(filename => "$Bin/wiki.html");
my $actions = {
'index' => sub {
$entry = join "
\n", sort keys %$wiki;
$topic = 'OverView';
show();
},
'new' => sub {
$template->param( edit => 1, topic => '', entry => '' );
return $q->header(), $template->output();
},
'edit' => sub {
$template->param( edit => 1, topic => $topic, entry => $entry );
return $q->header(), $template->output();
},
'update' => sub {
die "No topic no entry!\n" unless $topic;
die "Topic $topic isn't a WikiWord!\n" unless $topic =~ m/$wikiwords/;
$wiki->{$topic} = $entry;
store $wiki, "$Bin/wiki.db" or die "Can't store wiki.dat: $!\n";
show();
},
};
sub show {
$actions->{'edit'}() unless $entry;
$entry =~ s#$wikiwords#$1#g;
$template->param( topic => $topic, entry => textile($entry) );
return $q->header(), $template->output();
}
print $actions->{$action} ? $actions->{$action}() : show();
####
wiki