All in the <head>

– Ponderings & code by Drew McLellan –

– Live from The Internets since 2003 –

About

Local Textile

9 September 2005

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.

Creating a new command

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!

Textile in action

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

- Drew McLellan

Comments

  1. § Jemaleddin: Pretty cool idea, but the code that worked for me:

    jemaleddin$ sudo ruby install.rb config
    jemaleddin$ sudy ruby install.rb install

    Otherwise I get some message about reading the help and no global option for install.
  2. § Jemaleddin: Since I was retyping, obviously that should be sudo and not sudy
  3. § Drew: Thanks Jemaleddin – you’re quite right. (I’ve updated the instructions above).

    Writing the instructions from memory has its pitfalls.
  4. § kukkurovaca: Oh, this is wonderful. I’ve always wondered how one might go about doing this sort of thing.

    For the benefit of inept folk such as myself who happen not to use textmate, a less application-specific solution might be to use something along the lines of this crappy little applescript for in-clipboard conversion:

    set theTextile to the clipboard as string
    set the clipboard to (do shell script (“echo ” & theTextile & ” | /usr/bin/redcloth”))
  5. § Lisa McMillan: Dude. oh. my. It worked like a charm. I’ve been trying to figure out how to do this for EVER. You are so my HERO. (yes, I am yelling) LOL Thank you sooooo much. You have saved me HOURS of time. It’s like you’ve given me a day off a week. I owe you. You know where to find me if you ever need anything.
  6. § Sam MacCutchan: This is a really good idea. Why didn’t I think of it?

    I’ve been using TextMate since about March and have been loving it. I’d be interested in hearing what problems you are having with it as it’s been very stable for me and I’ve been running all the betas and even the new daily release builds that they’ve been putting out.
  7. § Jon Hicks: While not quite the same thing, the current nighlty builds of Textmate now include a Textile Preview command!
  8. § Charles Roper: In a similar vein, if you’re using jEdit there’s the rather good Xilize plugin, which is another Textile clone. Great for those that don’t have TextMate.
  9. § daveiler: with RedCloth 3.0.4 the install script is actually setup as in:

    ~/Desktop/downloads/RedCloth-3.0.4 deilers$ sudo ruby setup.rb config
    ~/Desktop/downloads/RedCloth-3.0.4 deilers$ sudo ruby setup.rb install

    and there shouldn’t be a slash at the end of the shebang line:

    #!/usr/bin/ruby
  10. § AndyS: If you are a jEdit user and what to check out a pre-release of Xilize2 just send me an email (xilize [at] centeredwork.com).

    Also for Firefox users there is a GreaseMonkey script by Phil Wilson that allows you to write textile input into any textarea on any webpage.
  11. § Fred:

    (although I’m running an older version as they’ve really screwed it up lately)

    What are you drinking? TM is better than ever.
    Maybe you have to change syntaxes you made for yourself, but the new system is muuuuch better. Really. And TM is much more polished now.

  12. § Drew:

    Fred – back in September (when this was posted) the latest builds of TM were breaking code colouring and all sorts. The text editor is an important tool for me – if I download a version and I suddenly can’t work I don’t have time to mess about, I just roll back to the version which did work.

    Since then I’ve had time to troubleshoot it a bit, and it seems that TextMate isn’t bundled with many languages by default any more. You have to go check them out from a subversion repository. Luckily I like TM enough to be bothered with that, and now I’m back running up-to-date builds and loving it. But it seems like a great way to put off new users. If I was trailling it for the first time, I’d give up before getting that far.

    Which reminds me .. I should post about why I love TextMate.

  13. § Chris:

    And if you put /usr/bin/redcloth in Window – Web Preview – Show options – Pipe text through, it will show a live (controlled) preview of your textiled text. Woo-hoo!
    Many thanks for the tutorial.

  14. § rahul benegal:

    Thanks a lot for your piece. I got this from another site:

    put these lines into a file, say textile.sh:

    #!/usr/bin/env ruby

    # Textile’ise things.

    # Take Textile markup on STDIN, and output HTML to STDOUT
    require ‘rubygems’
    require ‘redcloth’
    print (RedCloth.new STDIN.read).to_html

    Place in your path, and do a:

    chmod +x textile.sh

    Now you may do from the command line:

    textile.sh < file.html > output.html

    Or if you use the VIM editor:

    :%!textile.sh

    My need arose because some blogs like the very popular blogspot.com does not provide textile support.
    Hope this helps!
    rahul

    btw, seems whenever i update MAC OS X, my rails and rubygems breaks. While trying to install using “sudo ruby install.rb config” i kept getting a loaderror install.rb not found !

    So had then i resetup gems and did a gem install RedCloth.

  15. § Alderete:

    Although the above commands will work, your download link is slightly out-of-date, Redcloth is now at version 3.0.4. An alternative which will always install the latest version would be:

    sudo gem install redcloth --include-dependencies

    This requires you to have the Ruby Gems utility installed, but once that’s set up, the gem command makes installing almost any Ruby package that simple.

Textile Help

Photographs

Work With Me

edgeofmyseat.com logo

At edgeofmyseat.com we build custom content management systems, ecommerce solutions and develop web apps.

Recent Links

Affiliation

  • Web Standards Project
  • Britpack
  • 24 ways

About Drew McLellan

Photo of Drew McLellan

Drew McLellan (@drewm) has been hacking on the web since around 1996 following an unfortunate incident with a margarine tub. Since then he’s spread himself between both front- and back-end development projects, and now is Director and Senior Web Developer at edgeofmyseat.com in Maidenhead, UK (GEO: 51.5217, -0.7177). Prior to this, Drew was a Web Developer for Yahoo!, and before that primarily worked as a technical lead within design and branding agencies for clients such as Nissan, Goodyear Dunlop, Siemens/Bosch, Cadburys, ICI Dulux and Virgin.net. Somewhere along the way, Drew managed to get himself embroiled with Dreamweaver and was made an early Macromedia Evangelist for that product. This lead to book deals, public appearances, fame, glory, and his eventual downfall.

Picking himself up again, Drew is now a strong advocate for best practises, and stood as Group Lead for The Web Standards Project 2006-08. He has had articles published by A List Apart, Adobe, and O’Reilly Media’s XML.com, mostly due to mistaken identity. Drew is a proponent of the lower-case semantic web, and is currently expending energies in the direction of the microformats movement, with particular interests in making parsers an off-the-shelf commodity and developing simple UI conventions. He writes here at all in the head and, with a little help from his friends, at 24 ways.