The Frontier Group - Blog

Syntax highlighting code in blog posts

October 6th, 2008, by mlambie

I recently asked the programming community what were the best options for highlighting code snippets in blog posts. A lot of good answers came back and I settled on using SyntaxHighlighter.

There’s a handy plugin for Wordpress users, though be sure to use the HTML editor and not the Visual/WYSIWYG editor otherwise the name attribute will be stripped from the <pre> tag.

Here’s an example of some Ruby code to show you SyntaxHighlighter in action. This code is, taken from the upcoming AuroraCMS v2.0 – stay tuned for more news on this product in the coming weeks.

def force_login
  if params[:controller] != 'session'
    redirect_to login_path unless logged_in
  end
end

def force_logout(message = 'You have been logged out.')
  self.current_user.forget_me if logged_in?
  cookies.delete :auth_token
  reset_session
  flash[:notice] = message
  redirect_to login_path
end

Previously I’ve used vim’s :TOhtml command to export syntax-highlighted code snippets, but I think this is a much nicer alternative. It also means that we’ll have consistent highlighting across all of our posts regardless of individual poster’s preferences or .vimrc.

We are a web development company and this is our blog. We specialize in building web applications with the Ruby on Rails framework. You can read more about our Ruby on Rails development or contact us.

Leave a Reply


Follow Us

Stay in the Loop

  • Enter your email address to subscribe to our mailing list. You'll get updates about our products, specials and bonus offers, and general behind the scenes news from our team.

Twitter

Newsletters

Alexa Rank

Testimonial

The boys at The Frontier Group are amazing! For such a relaxed and personable organisation, they have phenomenal technical ability and a rampant professionalism. They have customisable solutions for all of my IT needs and they always deliver, on time and beyond expectation.

They fix problems other service providers can't and they helped me get a critical section of my web site up and running 10 minutes after I emailed the request!

Alex Hyndman, Nexus Car Share.

Featured Project

Case Study - Caudo Group - www.caudo.com.au

Website

www.caudo.com.au

Caudo Machinery

Caudo Group engaged our services to redesign their outdated website. We sent our photographer on-site to capture the essence of their business and turned it into a stunning web design.