Riki Wiki

riki: ruby wiki (a wiki written in ruby) or ruby icky (a ruby rehash of IckyWiki)

when i discovered the ShortestWikiContest, i thought "since RubyLanguage is so cool, i bet it can be used to write a complete wiki in under two hours and less than fifty lines". riki is the result. it may not the kind of code you'd want running a RealWiki, but it does implement AutomaticLinkGeneration, ContentEditableByAll, BackLinks, and most TextFormattingRules.

there is lots of room for improvement here (perhaps most importantly: complete TextFormattingRules as well as RecentChanges) which i may get to in time. or perhaps you, dear reader, will fill them in for me down below.


$ cat riki.rb | wc
  1. 146 1850
$ cat riki.rb
#!/usr/bin/env ruby
require 'cgi' # riki.rb ShortestWikiContest submission by BrandtKurowski
if ENV['PATH_INFO'] !~ /^\/(\w+)\/(\w+)$/
puts "Location: riki.cgi/view/HomePage\n\n" and exit
end
operation, page, text = $1, $2, CGI.new['t'].shift
data = File.exist?('../riki/data') ?
File.open('../riki/data') do |file| Marshal.load(file) end : Hash.new()
if text
data[page] = text
File.open('../riki/data','w') do |file| Marshal.dump(data, file) end
end
puts "Content-type: text/html\n\n"<br/> case operation<br/> when 'edit'<br/> puts %Q{#{page}

#{page}



}
when 'search'
list = data.reject {|name, text| text !~ page}.keys.collect { |name|
%Q{
  • #{name}} }
    puts %Q{Search for #{page}

    Search Results



    Searching for #{page}
      #{list}

    }
    else
    [ ['&','&'], ['<','<'], ['>','>'], [/(\r?\n){2}/, '

    \\&' ],
    [/(.*?)/, '\\1' ],
    [/(.*?)/, '\\1' ], [/^----/, '


    '],
    [/http:\/\/\S*/, '\\&' ],
    ].each do |wiki, html| data[page].gsub!(wiki, html) end
    nil while data[page].gsub!(/^(\t*)\t([^\t]*?)$/, "
      \n\\1
    • \\2
    ")
    nil while data[page].gsub!(/<\/ul>\s*