Home · Blog · Talks · Projects

Ruby scripting in the Webby "environment"

As I mentioned in my previous post, I’m in the process of converting my blog from being a Rails app using Typo to being a static site generated by Webby.

I’ve found myself wanting to be able to write Ruby scripts with access to the Webby “environment” e.g. the @pages instance variable.

I couldn’t find any documentation about this, so with a bit of delving into the source code and a bit of experimentation, I came up with the following code. I thought I’d share it here in case it was of use to anyone else. And if anyone knows a better way of doing it, please let me know.

Note that I think this script has to run at the top level of the project.

require 'webby'

# load Sitefile, setup load path
main = Webby::Apps::Main.new
main.init([])
main.app.init 'webby'
main.app.load_rakefile

# load helpers
include BlogHelper
include UrlHelper

# load pages, layouts, etc.
builder = Webby::Builder.new
builder.load_files
@pages = Webby::Resources.pages
Contact · History · Colophon · Links