hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Thursday

2006.09.28

Open Up Your Wiki and We'll Finish it For You #

by why in inspect

I’m hoping the Freaky Freaky Sandbox will avail me of having to finish anything ever again. Instead, I can just stop. Whatever it is, leave it unfinished. Gently place a sandboxed wiki on it. And then all of you at home can each add five lines of code and that’s it!

So, let’s get started on this sandboxed scriptable wiki. Step one, get some embedded Ruby in there. Here’s how.

 %w(sandbox rubygems redcloth 
    camping acts_as_versioned).each { |lib| require lib }

 Camping.goes :Tepee

 Tepee::Box = Sandbox.safe
 Tepee::Box.load "erbl.rb" 

I’m using Tepee as the base. Sandbox gets loaded first because if you load RubyGems first, it goes bonkers. So, we’re going to start up a little barebones sandbox and load the ERbLight library, which is written in pure Ruby. Just to keep it simple.

Now to swap out the markup processor for a sandbox eval.

  def markup str
    @boxx = nil
    begin
      str = Tepee::Box.eval("ERbLight.new(#{str.dump}).result")
    rescue Sandbox::Exception => @boxx
    end
    RedCloth.new(str, [ :hard_breaks ]).to_html
  end

And that’s it. If the sandbox throws an exception, it’ll go in the @boxx ivar. It might be better to check for an exception when the user saves the page instead.

So, this sandbox does nothing. I mean you can do math and string stuff and hashes and whatever, but it’s not that great. But come back in a few days and I’ll show you some more tricks for the Tippy Tippy Tepee.