Autonomous Machine

Posts tagged with Mac

Convert HTML to Haml within Textmate

I have been integrating a lot of well built HTML templates into Rails applications this week. I've found Haml to be the best templating language, and I like to convert preexisting templates to use it as I add them to a project.

The Haml gem installs a command line executable that performs this conversion, html2haml. The program takes whatever is passed as standard input, performs the conversion, and writes the result to standard out. For a while I was using command this in combination with the pbcopy/pbpaste commands (I work on a Mac) to apply this to the content of the clipboard:

pbpaste | html2haml | pbcopy

This worked, but the extra copying and pasting was annoying. So I made a TextMate command to eliminate the tedium:

Screenshot of the TextMate bundle editor

The actual command is this (it's a bit hard to see above):

echo $TM_SELECTED_TEXT | html2haml

You could add a key binding as well, but so far I haven't bothered to.

  • November 13, 2009
  • Article
  • Code, Mac, Rails, Ruby

Duplicate a Mac OS X Terminal window with Ruby and Appscript

Even though OS X's Terminal now supports multiple tabs, I still find myself using lots of separate windows so I can see everything at once. A common pain point was opening a second Terminal window and changing to the current working directory (I do this a lot from the root of Rails apps: one window for running the server, and one for git and other work).

I've since written a little Ruby script that essentially duplicates the current Terminal window. Save the following as dup and put it somewhere on your path:

#!/usr/bin/env ruby
#
# Duplicates a terminal window, optionally running the passed command
# % dup (creates new window)
# % dup command arg1 arg 2 (new window, plus runs passed command)

require 'rubygems'
require 'appscript'

include Appscript

def quote(command)
  command.gsub(/(\$`\\!")/){|m|"\\\\#{m}"}
end

path = ENV['PWD']
terminal = app('Terminal')
coords = terminal.windows.first.position.get
dimensions = terminal.windows.first.size.get

command = "cd \"#{quote(path)}\" && clear"
command << " && #{quote(ARGV.join(' '))}" unless ARGV.empty?

terminal.do_script(command)
terminal.windows.first.size.set([dimensions[0], dimensions[1]])
terminal.windows.first.position.set([coords[0] + dimensions[0] + 10, coords[1]])

Appscript is required, but it is only a sudo gem install rb-appscript away. The script will pass and arguments passed to it to the new terminal window. One common use for me is dup script/server.

  • May 30, 2009
  • Article
  • Code, Mac, Ruby

Building Sphinx 0.9.9RC1 with PostgreSQL 8.3 support via MacPorts on Mac OS 10.5

Up until today I was using the version of Sphinx provided by MacPorts, 0.9.8, as a lot of developers probably are. It installed painlessly, but I'm in the process of auditioning several different search engine options for a project and I wanted to make sure I was using the latest in my comparisons. I'm also using a PostgreSQL 8.3 database, which requires specifying a few additional options to configure.

Here are the commands I used to install Sphinx 0.9.9RC1 (run inside the Sphinx source directory after you download it):

export LDFLAGS="-L/usr/lib"
./configure --with-pgsql --with-pgsql-includes=/opt/local/include/postgresql83/ \
    --with-pgsql-libs=/opt/local/lib/postgresql83/
make
sudo make install

The first line ensures that make can find the system iconv libraries. Otherwise, it's a pretty straight forward process.

Update

A simpler solution is to make sure that the pg_config executable can be found on your path. Add /opt/local/lib/postgresql83/bin/ to your path, and then you can simply use:

export LDFLAGS="-L/usr/lib"
./configure --with-pgsql
make
sudo make install
  • January 24, 2009
  • Article
  • Installation, Mac