Monday, September 13, 2010

Snippet Highlighting with Ramaze and Sequel

A few days ago I saw this post on creating a pastie "clone" using Sinatra and Datamapper. I thought that it might be cool to try something similar in Ramaze and Sequel. It actually ended up being pretty easy (and similar to the other post (especially since I "stole" most of his CSS, etc.)), so I thought I'd share it here. If you've read much of this blog, then you should be pretty familiar with most of this, so I won't go through all of the code here but just hit the highlights. Here's the controller, controller/main.rb:

# controllers/main.rb
# The MainController has methods for index/main (to create a new snippet) and show (to show
# an existing snippet).

# Require syntaxi for syntax highlighting.
require 'syntaxi'
Syntaxi::line_number_method = 'floating'
Syntaxi::wrap_enabled = false
Syntaxi::wrap_at_column = 120

class MainController < Controller

# The main/index page that shows a text area where we can put in a
# title and a snippet. We'll grab the title and snippet text, create a new
# snippet, and then go to the snippet display page to see it.
def index
@title = "Snippet!"

# We've got a post, so grab the title and the snippet and then create
# a new Snippet with them and the current time. We'll then go ahead and
# redirect to the show method with the id that we get from the snippet.
title = request[:title]
snippet_text = request[:snippet]
snippet = Snippet.create(:title => title, :body => snippet_text, :created_at =>
redirect rs(:show,

# The show page that shows an existing snippet. It takes the id of an existing
# snippet and if it exists shows it nicely highlighted. If it doesn't exist, we'll
# just go back to the main page after setting the flash.
def show(id)
@title = "Show Snippet!"

# Find the snippet if it exists
@snippet = Snippet[id]

if @snippet != nil
# Get some text we can use to substitute for the "[/code]" text so that it doesn't
# mess up Syntaxi.
replacer ='[code-%d]')

# Do the syntax highlighting of the text with Syntaxi after we've done our
# substitution.
@snippet_highlight ="[code lang='ruby']#{@snippet.body.gsub('[/code]', replacer)}[/code]").process

# Substitute the '[/code]' back in for our replacement text.
@snippet_highlight = "#{@snippet_highlight.gsub(replacer, '[/code]')}"
# The snippet doesn't exist, so set a message and just redirect
# to the main/index page.
flash[:message] = "Snippet #{id} not found."
redirect rs :index

We start out requiring syntaxi which can be used with a sudo gem install syntaxi. The next three lines set a few variables which can be read about at the link. The index method will all the user to put in a snippet in a text area and then we'll create the Snippet (see model/models.rb and dbMigration/001_RamazeSnippet.rb for the information in this table/model). After we've created the new snippet, we'll redirect to the show method with the id of the new snippet.

The show method takes the id of the snippet and then displays the syntax highlighted version of it. It grabs the snippet from the database and if the snippet is not nil, it creates a "replacer" for [/code] which signals syntaxi to quit highlighting. We then substitute this replacer for the a [/code] and process the snippet. When this is complete, we resubstitute the [/code] for replacer and save it so the view can get to it. The view/show.xhtml is pretty simple and just displays the title, the highlighted snippet, and the creation date.

Like I said, not too much other than the syntax highlighting that we haven't seen before here. You can check all of the code on GitHub and run the code on Heroku.

Let me know if you have questions.

No comments:

Post a Comment