# * George Moschovitis # $Id: run.rb 270 2005-03-07 17:52:16Z gmosx $ require 'cgi' require 'redcloth' require 'yaml/store' require 'nitro'; include N require 'nitro/adapters/webrick' # A simple wiki. A straight conversion of the redhanded.hobix.com # example. Original code by why the lucky stiff. # This is just a cool example, not the preffered way to write # Nitro applications. WIKI = YAML::Store.new('wiki.yml') class Wiki < Component def index @p = 'Home' show end def show o.h1(@p) o.a('Home', :href => '/') unless @p == 'Home' WIKI.transaction do if WIKI[@p] o << RedCloth.new(WIKI[@p]).to_html.gsub(/([A-Z]\w+){2}/) { "#$&" } o.a 'Edit', :href => "/edit/#@p" else o.p("No page #{p}. ").a('Create?', :href => "/edit/#@p") end end end action :show, :route => /show\/(.*)/, 'p' => String def edit if c = request['c'] WIKI.transaction { WIKI[@p] = c } redirect "/show/#@p" end WIKI.transaction do o.h1 "Editing #@p" o.a('Show', :href => "/show/#@p").br.br o.form(:method => 'post') { o.input :type => 'hidden', :name => 'p', :value => @p o.textarea WIKI[@p].to_s, :name => 'c', :style => 'width: 90%; height: 200px' o.br.br.submit } end end action :edit, :route => /edit\/(.*)/, 'p' => String end Webrick.start(:dispatcher => Dispatcher.new(Wiki))