Agile Web Development

Build it. Launch it. Love it.

Radiograph

Radiograph is a small Rails plugin to expose Ultraviolet (http://ultraviolet.rubyforge.org/) to Rails views. Ultraviolet is a syntax highlighting library which makes use of Textpow which parses Textmate bundles (whew!). This plugin lets you use Ultraviolet’s syntax highlighting capabilities in Rails applications. It parses a Textmate syntax and generates HTML based on a Textmate theme.

Usage

You can use it in views like so:

  <%= code("show_menu if current_user") %>

The default theme is ‘all_hallows_eve’ and the default syntax is ‘ruby.’ The above snippet would generate some simple XHTML that syntax highlights the code show. If you don’t want to type code(), you can use the convenience alias: c().

  <%= c("my_value ? puts 'Good!' : puts 'Oh noez!'", :theme => 'amy') %>

Note that this example changes the default theme (learn more about the parameters to Radiograph#code in its documentation).

CSS is required

You will also need to require the proper CSS file for the theme(s) you use using the require_syntax_css method.

  <%= require_syntax_css %>

If you’ve used anything other than the default theme (as I did with the ‘amy’ theme above), then you’ll need to link those also.

  <%= require_syntax_css 'amy' %>

You should now have a nicely highlighted block of code on your page. Look at the documentation for the individual methods for more information.

Setting defaults

You can set defaults in environment.rb by setting values on the Radiograph class.

  Radiograph.theme = 'cobalt'
  Radiograph.syntax = 'lisp'

These values will then become the defaults in your application.

Things left…

Here are some things I’d like to add in the future…

  • Caching — Ultraviolet is a little slower than I’d like…it may be Oniguruma’s fault. Caching would alleviate some of the problem.
  • Asset hosts — Right now the CSS is just dumped into your public/ folder; I’d like to hook into Rails and use whatever the app is using.

Vitals

Home http://www.jeremymcanally.com/
Repository http://code.jeremymcanally.com/radiograph
License Rails' (MIT)
Rating (11 votes)
Owner Jeremy McAnally
Created 7 June 2007

Comments

  • Avatar
    Dizan Vasquez
    9 June 2007

    Hi Jeremy,

    I am ultraviolet's developer, this is great stuff! I will add a link to your plugin in ultraviolet's page rightaway!

    Dizan

  • Avatar
    Dizan Vasquez
    9 June 2007

    I forgot. I think that the main bottleneck in uv comes from the fact that all syntaxes are loaded in memory on first run, anyway further development should include lazy loading, thus improving performance.

  • Avatar
    11 June 2007

    Hi Dizan, Thanks! That would be great. I'll look into hackin on uv when I get time; it looks like a cool project. I'm sure lazy loading wouldn't be too difficult to implement.

  • Lee Caine
    2 September 2008

    Hi Jeremy, where does it actually copy the CSS files from? it does not seem to be copying them.. unless I am doing something completely wrong

  • Avatar
    Bobby Prill
    8 March 2009

    Jeremy, this is wonderful! Is there a way to use Radiograph to format blocks of code within a blog post that is formatted using Textile? I would like for Radiograph to do it's stuff to the code snippets that I denote with "pre" and "code" html tags in my Textile posts.

  • Avatar
    19 August 2009

    Good write-up, but there are some really robust syntax highlighting solutions available for ruby and rails. I just did a whole write-up on them, as well as Radiograph, here:

    <a href="http://doblock.com/articles/a-whistle-stop-tour-of-syntax-highlighting-and-markdown-solutions-for-rails">http://doblock.com/articles/a-whistle-stop-tour-of-syntax-highlighting-and-markdown-solutions-for-rails</a>

    I hope you find it useful. Thanks again.

Add a comment