hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Catapult Wiki Exoskeleton #

by why in bits

Here’s a fun library that’s gone under the radar. Catapult lets you serve objects over HTTP using over-the-counter WEBrick. Your object simply needs two methods:

 run( path_info_string ) -> response string
 content_type() -> HTTP content type string

For example, here’s a Wiki which uses Catapult and stores its pages in a single Textile+YAML document. It’s all gluey! If you’re hankering for a custom Wiki, it’s a good start.

 require 'cgi'
 require 'redcloth'
 require 'yaml/store'
 class Wiki

   def initialize
     @hp = "HomePage" 
     @c = CGI.new('html4')
     @wiki = YAML::Store.new('wiki.yml')
   end

   def content_type; "text/html"; end

   def run( path_info )
     @v = path_info.gsub!( /\?(.+)\Z/m, '' ) ? CGI::parse( $1 ) : {}
     action, path = path_info.split( '/', 2 )
     @wiki.transaction do
       title, body = method( action ).call( path.to_s )
       @c.html { @c.head { @c.title { title } } + @c.body { body } }
     end
   end

   # Wiki actions: 'show' and 'edit'
   def show( p )
     p = @hp if p.empty?
     body = @c.h1 { p }
     body += @c.a(@hp) { @hp } unless p == @hp
     body +=
       if @wiki[p]
         RedCloth.new( @wiki[p] ).to_html.
           gsub(/([A-Z]\w+){2}/){ @c.a($&){$&} } +
           @c.a("../edit/#{p}") { "Edit" }
       else
         @c.p { "No page #{ p }. " +
           @c.a("../edit/#{p}") { "Create?" } }
       end
     [p, body]
   end

   def edit( p )
     @wiki[p] = @v['c'].to_s if @v['c']
     title = "Editing " + p
     body = @c.h1 { title } + 
       @c.a("../show/#{ p }" ) { "Show" } +
       @c.form("post") { 
         @c.textarea('c') { @wiki[p] } + @c.submit 
       }
     [title, body]
   end
 end

To run the Wiki, put it in a directory with catapult.rb. Then, run: ruby -rwiki catapult.rb 2005.

Access the wiki at http://host:2005/wiki/show.

Turns out the Rubyforge site is the new home. That means it’s better. Truly, truly, use it duly.

said on

Very cool.

said on

Hey Why, thanks for the kind words. Catapult might be my favorite Ruby app because it does so little. I use it all the time, running simple XML services on my home network. It sort of “fell out” from other code when I find myself writing the same thing over and over.

BTW , there is a RubyForge project for Catault, with the latest code.

http://catapult.rubyforge.org/

said on

This is awesome.

said on

This is cool, Btw i implemented this very same wiki in the latest Nitro release: Check out: examples/why_wiki

Will be improved further in the next release.

Comments are closed for this entry.