Nanoc Plugin: Ultraviolet
comments | Posted in website on Sunday, March 02 2008 20:00:04 GMT

I am working on a few work and personal projects at this time. Work is primarly PHP5 projects (PDO, PHP5, MySQL) converting some existing sites from VBScript and ACCESS. At the same time my personal projects are in Ruby and also Java technologies (Java, JavaDB, JavaFX). Even though I am working on alot of things at this time I am still trying to find time to regularly update this nook of the web :]

Below is a simple plugin I adopted from the current Nanoc Wiki and my past experience with SimpleLog which uses the (Ultraviolet)[http://ultraviolet.rubyforge.org/] syntax highlightling library to generate pretty HTML output for hundreds of kinds of programming languages. I am also trying to tie in some more jQuery usage in the website for some rounded-corner goodness! Hopefully somebody finds this of use. At some point in the near future I will make the project section of this website contain a full listing of the code-works on this website.

A simple syntax highlighting plugin using UltraViolet.

class UltraVioletFilter < Nanoc::Filter

identifier :ultraviolet

def run(content)
    nanoc_require 'rio'
    nanoc_require 'uv'

    code_rule = %r{(<code class="(\w+)" (file="(.+?[^"])")?>(.+?[^(</code>)])</code>)}m

    while content =~ code_rule
        original, lang, filename, code = $1, $2, $4, $5

        # Create a plaintext file version for download.
        if filename
            code_path = [@config[:output_dir], @page.path, 'code']
            url = @page.path + 'code/' + filename
            copy_text_to_file(code, filename, code_path)
            content.gsub!(original,
                %{<a class="file" href="#{url}">
                get source - #{filename}
                </a>\n} + original
            )
        end

        # Substitute the un-highlighted code with the highlighted code.
        content.gsub!(
                original,
                Uv.parse(code, "xhtml", lang, true,
                @config[:ultraviolet_theme])
        )
    end
    content
end

private
def copy_text_to_file(str, fname, dir)
    dest_rio = rio(dir).mkpath
    frio = rio(dir, fname).delete
    puts "*** unlinked old code file ***"
    frio << str
    puts "*** Wrote plaintext file #{dir}/#{fname} ***"
end

end

blog comments powered by Disqus