Code Syntax Highlighting
October 17th, 2008
Having just implemented code syntax highlighting in this blog in addition to Textile formatting, I thought I’d share how I did it.
After a little research, I decided that Arya Asemanfar’s tm_syntax_highlighting plugin looked the most promising. Arya’s blog post about it was particularly helpful.
Prerequisites
Install the oniguruma system library (see link in the plugin README).
Install the ultraviolet gem, which will also install the textpow and oniguruma gems.
sudo gem install ultraviolet
1. Install the Plugin
./script/plugin install git://github.com/arya/tm_syntax_highlighting
2. Copy all themes from ultraviolet:
./script/generate syntax_css
3. Create defaults initializer
# config/initializers/tm_syntax.rb TmSyntaxHighlighting.defaults = {:theme => "mac_classic", :line_numbers => false, :lang => "ruby"}
4. Create an application helper method to parse the Textile and code:
Note: I have used “c0de” instead of “code” here just to enable this code to be parsed!
# app/helpers/application_helper.rb def parse_textile_and_code_syntax(text) text_pieces = text.split(/(<c0de>|<c0de lang="[A-Za-z0-9_-]+">|<c0de lang='[A-Za-z0-9_-]+'>|<\/c0de>)/) in_pre = false language = nil text_pieces.collect do |piece| if piece =~ /^<c0de( lang=(["'])?(.*)\2)?>$/ language = $3 in_pre = true nil elsif piece == "</c0de>" in_pre = false language = nil nil elsif in_pre code(piece.strip, :lang => language, :theme => "mac_classic") else RedCloth.new(piece.strip).to_html end end end
5. Use the helper in the views
<%= parse_textile_and_code_syntax(@blog_post.post) %>
6. Add CSS for horizontal scrolling
pre { margin: 10px 10px; padding: 10px 10px; line-height: 15px; overflow: auto; font-family: "Monaco", "Courier New", courier; font-size: 12px; }
Navigation
Latest Blog Posts
- 11 Aug: RVM, Bundler and Ruby Tracker
- 10 Feb: Ten Years On
- 10 Oct: Configuring CapGun
- 6 Sep: Inspirations for a Ruby DSL
- 28 Jul: At Your Service
Syndication
Tags
actionwebservice automated testing bdd blog bundler capgun capistrano collaboration css cucumber deployment dsl estimating jaoo javascript osdc osdc2008au pdf prawn rails railscamp rake rest rorosyd rspec ruby ruby tracker rvm selenium services syntax highlighting tdd technology unconference wds08 web design webrat xml xml-rpc
Comments
None yet.
Post a Comment
Please use your real name and be respectful. Textile formatting is supported.