Rake + Builder + TextMate = Sweet

Jim Weirich gives a cool example of using Rake to automate searching your source code. The example even includes Emacs integration. But I live in TextMate these days, so here's the TextMate integration for Jim's fic.rb:

  #!/usr/bin/env ruby
  #fl2tm.rb converts 'file:lineno context' into HTML for TextMate
  require 'rubygems'
  require_gem 'builder'

  base = Dir.pwd
  title = ENV["TITLE"] || "Stdout 4 TextMate"
  lines = []
  xml = Builder::XmlMarkup.new(:target=>STDOUT, :indent=>2)
  xml.html do
    xml.head do
      xml.title(title)
    end
    xml.body do
      xml.h1(title)
      while line = gets
        lines.push line
        # doesn't handle whitespace in path names 
        if line =~ /\s*([^:]+):(\d+):(.*)/
          (file, line, context) = [$1,$2,$3].map {|i| i.chomp}
          xml.li do
            if file =~ %r{^/}
              href = "txmt://open?url=file://#{file}&line=#{line}" 
            else
              href = "txmt://open?url=file://#{base}/#{file}&line=#{line}" 
            end
            xml.a "href" => href do
              xml.text! "#{file}:#{line}"
            end
            xml.text! context
          end
        end
      end
      xml.hr
      xml.pre(lines.join())
    end
  end

This little script converts the output of fic into HTML with TextMate links, so that you can click on the links to navigate TextMate. To add to TextMate, create a command that looks like this:

  #!/usr/bin/env ruby
  $: << ENV['TM_SUPPORT_PATH'] + '/lib'
  require "dialog"

  Dialog.request_string(:title => "Find in Code",
                       :prompt => "Extensions Then Res",
                      :button1 => "Find") do |line|
    Dir.chdir(ENV['TM_PROJECT_DIRECTORY'])
    path = ENV['TM_RUBY_SAMPLES'] 
    system "#{path}/rake/fic.rb #{line} | #{path}/rake/fl2tm.rb"
  end

In the TextMate Bundle Editor, set Input to 'None' and Output to 'Show as HTML.' You'll have to replace the TM_RUBY_SAMPLES with your path to fic.rb and fl2tm.rb--I haven't had time to package this (and other goodies) into a TextMate bundle yet. You will need to have RubyGems and Builder. I hope Jim has those installed. :-)

Get In Touch