This is the way we tie shoes here. Don’t be fooled, it is all a lie.
module Source include ::Haml::Filters::Base def initialize(text) @text = highlight_text(text) end def highlight_text(text, opts = DEFAULT_OPTIONS) Uv.parse( text, "xhtml", opts[:format], opts[:lines], opts[:theme]) end def parse_opts(text) opts = {} all_lines = text.split(/\n/) return [opts, text] unless all_lines.first =~ /#!source/ line = all_lines.first opts[:format] = $1 if line =~ /lang\s*=\s*([\w-]+)/ opts[:theme] = $1 if line =~ /theme\s*=\s*([\w-]+)/ opts[:lines] = (line =~ /lines\s*=\s*(\w+)/) ? ($1 == 'true') : false [opts, all_lines[1..-1].join("\n")] end def render(text) opts, text = parse_opts(text) Haml::Helpers.preserve(highlight_text(text.rstrip, DEFAULT_OPTIONS.merge(opts))) end private DEFAULT_OPTIONS = {:format => "ruby", :theme => "twilight", :lines => false} end