27 June 2009

WordPressWordPress is a seriously popular blogging platform, primarily because it simply rocks. In fact, this here blog uses WordPress.

Since I generally write posts about programming issues, I needed a good plugin for automagically highlighting code segments. I ended up choosing SyntaxHighlighter by Viper007Bond.

While it generally kicks butt, there is one little bug that is particularly annoying for those of us who post snippets of Ruby code: including a variable in a string like 'this is a #{color} string' causes SyntaxHighlighter to make everything after the “#” into a comment, which it wraps down to the following line.

That simply won’t do…

You can see what I mean below:

Default parsing of a snippet of Ruby code
Default parsing of a snippet of Ruby code

The code above can be found in my recent post Calling a 32-bit System Command from a Script in x64 Windows. As you can see, the parser assumes that #{putername} should be a comment since it contains a “#“. Obviously, that’s flat out wrong.

I did a little search and I found out this problem has been hanging around since at least April 7, 2008. So, I figured if I wanted it fixed, I’d have to take matters into my own hands.

It’s a rather simple bug to fix if you understand regular expressions. Here’s what to do:

  1. Find the file: /wp-content/plugins/syntaxhighlighter/syntaxhighlighter/scripts/shBrushRuby.js
  2. Edit the file and find line 43, which should look like this:
    { regex: SyntaxHighlighter.regexLib.singleLinePerlComments, css: 'comments' },
  3. Change that line to look like this:
    { regex: new RegExp('#[^\{].*$', 'gm'), css: 'comments' },
  4. Save your changes and upload to the server if you are using FTP

That’s it – you’re all set.

The way Ruby parsing is set by default is to use the “Perl Comment” standard. That means that any time SyntaxHighlighter finds a “#” in your code, it will assume that everything after the “#” until the end of the string is a comment. The change you just made uses a different regular expression for Ruby code comments:


The new regex matches a “#” followed by any character that is NOT an opening squiggly brace, i.e. “{“. The next characters to match can be zero or more of any character, i.e. “.*“. The match is continued until the end of the string (“$“), or the end of the current line in other words.

All we’ve done is made it treat comments the same as before, except that if it finds “#{“, it will no longer parse the text as a comment. After applying the new regex as outlined above, we now get this:

Modified Ruby parser now works properly!
Modified Ruby parser now works properly!

That’s all it takes to get SyntaxHighlighter working properly for variables inside a string in Ruby!

SyntaxHighlighter: Prevent #{var} in a Ruby String from Being Parsed as a Comment
Tagged on:         

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.