Syntax Highlighting in Your Posts
Ever wish your blog articles had the nice syntax highlighting of your favorite Ruby editor? Now they can with Syntax.
Syntax can be installed as a RubyGem. On Mac OS X, I do it like this:
$ sudo gem install -r syntax
Syntax is easy to use. Here’s a script that runs Syntax on the file named as its first parameter and writes some nice html to stdout. Of course, you can redirect this to a file. (I ran this script on itself to to add syntax highlighting here).
require 'rubygems'
require_gem 'syntax'
require 'syntax/convertors/html'
# a comment
convertor = Syntax::Convertors::HTML.for_syntax "ruby"
puts( convertor.convert( File.read( ARGV[0] ) ) )
The output of this script is some nice HTML of your Ruby code ready to be customized with a CSS stylesheet of your choosing. For this article, I’ve embedded a stylesheet:
.keyword { font-family: monospace; color: #8B0000; }
.constant, .attribute, .global, .class, .module {
font-family: monospace; color: black;
}
.string { font-family: monospace; color: #00008B; }
.ident, .method { font-family: monospace; color: #006400; }
.number, .char { font-family: monospace; color: #888800; }
.comment { font-family: monospace; color: purple; }
.symbol { font-family: monospace; color: #884400; }
.regex { font-family: monospace; color: #808080; }
.punct { font-family: monospace; color: black; }
.escape, .interp, .expr {
font-family: monospace; color: black; background-color: #dddddd;
}
Simple to install and simple to use, color the Ruby code in your articles with Syntax.
About this entry
You’re currently reading “Syntax Highlighting in Your Posts,” an entry on Michigan User Ruby Group
- Published:
- March 4th 01:52 PM
- Updated:
- October 22nd 10:43 PM
- Sections:
- Articles
1 comment
Jump to comment form | comments rss [?]