dinosaurs eat everybody

for all your Dave Schwantes related needs

Sinatra + Jekyll + Prose

Welcome to the new Dinosaurs Eat Everybody blog! The old site/blog was an interesting mess of custom PHP and some custom build hooks into a Word Press blog and though it worked pretty well but I was tired of maintaining stuff in PHP so when my old hosting was about to expire I decided to move to a VPS and build something new. The result is what you see before you today.

This new site is built mainly on Sinatra with Jekyll mixed in for some blogging functionality. I didn’t really need any sort of content management system for most of the site so there is no database behind it and I’m using flat files (mostly just handwritten JSON) for anything that seems dynamic.

The interesting part was adding a Jekyll blog to an existing Sinatra site. Jekyll is super easy to set up on its own but I didn’t want it to run the whole site. I wanted to be able to serve dynamic pages if I wanted. With some help from this blog post it was actually pretty easy to get things up and running. That post is a bit out of date but still very helpful.

To add Jekyll to Sinatra I first just created a new Jekyll site in a seperate directory and copied it over into a jekyll_blog/ folder in the root of my Sinatra project. I then removed the .gitignore, the default.html layout and the whole css/ directory (after moving the Pygments syntax.css into my main stylesheet directory) from the new Jekyll directory. I also adjusted the _post.html layout to not have a layout of its own by just removing all the YAML Front-matter from the file. This is because I want Sinatra to handle the layouts for everything.

The real meat of this is a custom function in the /blog route:

get '/blog/?*' do
	jekyll_blog(request.path)
end

def jekyll_blog(path)
  @page_title = "blog"

  file_path = File.join(File.dirname(__FILE__), 'jekyll_blog/_site',  path.gsub('/blog',''))
  file_path = File.join(file_path, 'index.html') unless file_path =~ /\.[a-z]+$/i 

  if File.exist?(file_path)
    file = File.open(file_path, "rb")
    contents = file.read
    file.close

    erb contents
  end
end

This just calls the jekyll_blog(path) function whenever you go to a page that starts with /blog/ and that just pulls the html content of either the post or the Jekyll index.html and puts into the <%= yield %> in my Sinatra erb layout.

This is a pretty simplistic solution and I want to add better 404 checking and a way to pull the post title out for use in the page title, but it gets the job done by pulling out the content of the post html file generated by Jekyll and putting it into the content container of Sinatra. Now all my layouts are handled by the same file and all my CSS comes from the same source.

While I like the idea of being able to write my blog posts in Markdown and from whatever editor I want I also wanted to have some sort of web based editor for my posts so I’m trying Prose. Prose is basically a web based editor for Github files (which is where all the content for this site resides anyways. It lets me go in and create new posts through a pretty nice looking Markdown editor with preview. It also helps with syntax and media uploading (though I haven’t tried that yet). I’ve included a _prose.rb config file that sends me right to the _posts/ directory of this project when I’m working on it:

prose:
  rooturl: 'jekyll_blog/_posts'
  media: 'public/images/blog_media'

I think that the transiton from a really polished, very user friendly system like Word Press to something more technical like Jekyll will be interesting. I feel like I still need to work some kinks out of the system and I’m still not totally won over by Prose but it has been really fun to set this up. I’d also like to add comments through Disqus soon. Hopefully now I’ll make this work worthwhile and actually write a few blog posts every now and then.