Syntax highlighting code in blog posts

Posted in AuroraCMS, Code

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.

No Comments

No comments have been left yet, be the first.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Twitter

Great web stats at @petrescue , the driving force behind the rebuild of their systems by @frontiergroup . http://t.co/MTvfoxnU

@frontiergroup about 2 weeks ago #

Search Posts

Featured Posts

Categories

Archives

View more archives