Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

a quick and dirty Wiki

by neniro (Priest)
on Oct 29, 2005 at 23:24 UTC ( #503926=CUFP: print w/replies, xml ) Need Help??

This is a quick and dirty hack of mine (in 52 lines) with the primary purpose to show what can be done using hashes and the secondary purpose to show the usage of some popular modules for doing cgi-scripts.

The caveats: It has no sanitizing, it isn't taint-safe, Storable isn't good for bigger amounts of data (I could have used one of those nice ORM-modules on cpan, but I would have lost the simplicity of hashes I'd liked to show) and it has nearly no features.

I really would like to hear your comments and advices. So here is the code:

#!/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 "<br />\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/$wikiw +ords/; $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#<a href="$1">$1</a>#g; $template->param( topic => $topic, entry => textile($entry) ); return $q->header(), $template->output(); } print $actions->{$action} ? $actions->{$action}() : show();

and the template:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns="" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/ +> <title>wiki</title> <style type="text/css" media="screen"> /* <![CDATA[ */ .default { font-family: Verdana, Helvetica, sans-serif, Arial; font-size: 11px; color: #245199; background-color: #aaa; } #navigation { float: right; width: 150px; background-color: #EEE; padding: 4px; padding-bottom: 20px; border-style: solid; border-width: 0px; border-left-width: 2px; } a { color: #245199; text-decoration: underline } a:hover { color: #000; text-decoration: none } a:active { color: #245199; text-decoration: none } /* ]]> */ </style> </head> <body class="default"> <div id="navigation"> <h2>navigation</h2> <a href="<TMPL_VAR NAME="topic">;action=edit">edit +</a><br /> <a href="">new</a><br /> <!-- <a href="<TMPL_VAR NAME="topic">;action=delet +e">delete</a><br /> --> <a href="">overview</a><br /> </div> <div id="content"> <h1 id="topic"><TMPL_VAR NAME="topic"></h1> <TMPL_IF NAME="edit"> <form action="" method="POST"> Topic:&nbsp;<input name="topic" size="30" value="<TMPL_VAR + NAME="topic">" /> <p> Entry:&nbsp;<textarea name="entry" cols="80" rows="24"><TM +PL_VAR NAME="entry"></textarea></p> <p> <input type="submit" value="update" name="action"/> </p> </form> <TMPL_ELSE> <div id="entry"> <TMPL_VAR NAME="entry"> </div> </TMPL_IF> </div> </body> </html>

Replies are listed 'Best First'.
Re: a quick and dirty Wiki
by eyepopslikeamosquito (Chancellor) on Oct 30, 2005 at 20:06 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://503926]
Approved by skx
Front-paged by Arunbear
[Corion]: :-D
Corion discovers a new shiny toy to try out over the (longish) weekend. Since I've done some more with websockets, maybe I'll try writing a webserver that implements hot-reloading of HTML(+CSS, +Javascript) in the browser. Edit the local file and ...
[Corion]: ... the browser(s) get a ping to a) refresh the page or b) reload "just" the changed parts, keeping the scroll position etc.
[Corion]: But I also have to look at how I can make WWW::Mechanize:: RemoteBrowser a reality, and how to make it safe from malicious content ;)
[Corion]: Part of wanting hot-reloading is that I think I've stumbled on a very simple set of CSS that I maybe want to use for a blog, but I want to try that out on mobile too, and I also want to add/modify it slightly so it has a header too...
[Corion]: ... and having hot reloading would make it easy to view the changes in multiple browser windows of different sizes, and on Android, simultaneously

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2018-04-26 10:49 GMT
Find Nodes?
    Voting Booth?