All in the <head> – Ponderings and code by Drew McLellan –

Local Textile

I’m sure most people are familiar with the Textile text editing language used by many web-based tools to make creating and modifying web content more humane. I live and breathe Textile and use it pretty much wherever I can. Its simplicity and ability to get out of my way makes it the quickest way to edit XHTML that I know of.

I use Textile for writing blog entries like this one, I use it for leaving comments around the web on other people’s blogs. My wiki of choice utilises it, and I build Textile into an awful lot of the web apps I work on. It’s easy, quick, and simple to teach to a client or colleague. The one place I don’t have Textile, however, is in my favourite text editors and local applications. Until now…

Installing Textile on your Mac

If you’ve used rails you may have come across a ruby implementation of Textile called redcloth. The neat thing I noted about redcloth is that it runs as a stand-alone ruby script. You can invoke it from the command line and it’ll translate standard input into XHTML. You can then use any smart command line-aware apps to hook into this. Here’s how to get it running.

I’m going to assume that you’re running ruby 1.8 on your Mac. I think Tiger ships with version 1.8, but Panther only sported 1.6. The quick test:

Malachi:/ drew$ ruby -v
ruby 1.8.2 (2004-12-25) [powerpc-darwin8.0]

I’m running 1.8.2. You’d be advised to do similarly.

Redcloth

The next step is to download redcloth. After some searching and poking around on mailing lists, I found a good redcloth download which seamed to do the trick.

Decompress the download, navigate into the resulting folder, and run the ruby script install.rb using sudo (to give the script the privs it needs to copy things to the right folders).

Malachi:~/Downloads drew$ cd RedCloth-3.0.3
Malachi:~/Downloads/RedCloth-3.0.3 drew$ sudo ruby install.rb config
Malachi:~/Downloads/RedCloth-3.0.3 drew$ sudo ruby install.rb install

This script does a fair bit, but the crucial result is /usr/bin/redcloth – a program to which you can send your plaintext and get back XHTML.

Path to Ruby

One small change I had to make after installation was correcting the path to ruby that was at the top of /usr/bin/redcloth. Use a text editor (like nano or pico) and adjust the path to reflect where ruby lives on your system. Mine’s at /usr/bin/ruby/. You’ll need to sudo to edit this file.

Malachi:/ drew$ sudo nano /usr/bin/redcloth

(It’s Ctrl-X to save and get out of nano or pico).

Check that it’s all working by piping a small text string to the program:

Malachi:/ drew$ echo hello world | /usr/bin/redcloth
<p>hello world</p>

It all looks good. Next we have to put it to use more practically.

Using Textile in Textmate

I use Textmate as my weapon of choice at the moment (although I’m running an older version as they’ve really screwed it up lately). It has Commands which can be used to run scripts and such. For example, you could right a command to save, compile and then run your current document. Or, if you’re me, you can write a command to take the contents of the current document, run it through redcloth and bung the result in a new window.

In the Key Equivalent, I entered Ctrl-T as the option to invoke this particular command. This means that I can happily type away in a document, marking up with Textile syntax as I go. A simply keystroke launches a new document containing my transformed XHTML. Perfect!

Give it a go and let me know if you find any mistakes in the above.