April 06, 2006

Source Code Highlighting from the Clipboard on OS X

Source Code Highlighting from the Clipboard on OS X

So it doesn't get lost here is a command you can use to highlight source code on OS X:

$ pbpaste | \
  tr '\r' '\n' | \
  source-highlight --no-doc --css foo.css --src-lang js | \
  pbcopy

First, pbpaste pastes the contents of the clipboard (or pasteboard in Mac lingo) onto standard out. We then pipe it to tr which is a simple substitution command to replace the Mac line breaks with UNIX line breaks that source-highlight, a GNU program, understands. This is then piped through to source-highlight, with options to format using CSS and a flag specifying the language (the CSS file name can be anything, it makes no difference with the --no-doc flag). Finally it's piped to pbcopy which sticks it back onto the clipboard. Easy!

I had to install source-highlight from source, the version on Fink was too old and didn't have the JS language definition (and what good is it without JavaScript!). The only dependancy is the Boost C++ RegExp library. This isn't too hard to install; first you have to download the bjam binary, a make tool, and use it to compile and install the Boost libraries.

Lo and behold, highlighted source code.

0 Comments:

Post a Comment

<< Home