In this blog post I will demonstrate a quick and easy way I have found to syntax highlight your Ruby code in pretty colors for your blog posts or other HTML-based display purposes. Please note, this blog post is designed for use with color monitors only, obviously.
The focus of this post is Ruby, but this method also applies for syntax highlighting C, C++, Python, CSS, Clojure, diff, Groovy, HAML, ERB, Java, JavaScript, PHP, SQL, XML and YAML.
What’s Wrong With Good Ol’ Black and White?
Here is some arbitrary code in app/controllers/home_controller.rb
class HomeController < ApplicationController
FEATURE_ICONS_DIRECTORY = File.join(::Rails.root, "public/img/feature-icons")
def index
# Find all the images in the "feature-icons" directory, strip off
# the directory and extension, to just leave a list of icon names
@feature_names =
Dir.entries(File.join(FEATURE_ICONS_DIRECTORY)).
reject { |f| ! f.end_with? ".png" }.
map { |f| f.split(".")[0] }.
sort
end
end
Yawn. So black and white. So hard to read. Would it not be great if we could display this in color?
Here is how you can convert this code to color…
Install the CodeRay gem
First we need to install the CodeRay gem.
gem install coderay
Colorize That Code
The CodeRay gem is very feature rich, but here is a simple out-the-box example that will colorize our code for syntax highlighting…
ruby -e '
require "coderay";
filename = "app/controllers/home_controller.rb";
print CodeRay.scan_file(filename).div
'
This example will output HTML with inline CSS. This is a good quick and dirty way to get to colorized code, but your front-end CSS person, who likes clean HTML, will probably hate you. Replacing the “.div” with “.html” will not include the CSS and it will be up to you to provide the style for the CSS classes.
Take It Out Of The Oven
Here is what the colorized code will look like on your blog, webpage or presentation…
class HomeController < ApplicationController
FEATURE_ICONS_DIRECTORY = File.join(::Rails.root, "public/img/feature-icons")
def index
# Find all the images in the "feature-icons" directory, strip off
# the directory and extension, to just leave a list of icon names
@feature_names =
Dir.entries(File.join(FEATURE_ICONS_DIRECTORY)).
reject { |f| ! f.end_with? ".png" }.
map { |f| f.split(".")[0] }.
sort
end
end
And there it is. Not much more to say about that other than how easy it was. Although it was easy the CodeRay gem is very rich.
I prefer Array.select{ statement } over Array.reject{ !statement }; it lends itself to higher readability and maintenance.
Thanks for the tip Ted!
The missing feature from CodeRay is lack of support for CoffeeScript I had to implement CS highlighting using client-side libraries in my homepage
Hi Rodrigo,
I missed the fact that CodeRay did not support CoffeeScript. Thanks for pointing that out. Could you share some details about your implementation? I am sure that there are others hitting this post with the same issue.
Cheers,
Phil
Hi Phil, I’m using codemirror for highlighting CoffeeScript in my site. My site is a custom Rails application inspired by Toto’s idea:
https://codemirror.net/
https://github.com/cloudhead/toto
My articles are written in Markdown and code examples lie between “@@@ language-identifier”..”@@@” blocks.
Most are processed using CodeRay but for CoffeeScript I had to use client-side processing cause I couldn’t find any Ruby highlighting library that understands CoffeeScript
Sweet. Thanks for introducing me to Toto and https://codemirror.net/ looks cool! Pretty rich API https://codemirror.net/doc/manual.html
Question: When I’m editing with CodeMirror, is it sending the edited text back to Ruby for re-syntax-highlighting?
I never enabled editing to the codes blocks displayed in my articles
But anyway, Ruby is not involved at all with CodeMirror. It is all handled in the client-side. See an example here:
https://codemirror.net/demo/fullscreen.html
Pygments, which is similar to CodeRay but is written in Python, supports CoffeeScript. https://pygments.org/docs/lexers/
This is what GitHub uses for their syntax highlighting.
I recently used CodeRay to colorize syntax in the comments of a rails app, letting the users define the code sections, applying autolinking and simple formatting. Here’s how:
https://jaimeiniesta.posterous.com/how-to-colorize-code-in-comments-using-codera