Like a lot of blogging nerds I’m a huge fan of John Gruber’s Markdown. If you’re reading this you’re likely familiar with it, but if you’re not, it’s basically a way to write content for the web as plain text, including links and basic formatting like these italics, and have it converted to nice (X)HTML. The last paragraph looked like this in its original Markdown:
Like a lot of blogging nerds I'm a huge fan of John Gruber's [Markdown][md]. If you're reading this you're likely familiar with it, but if you're not, it's basically a way to write content for the web as plain text, including links and _basic formatting like these italics,_ and have it converted to nice (X)HTML. The last paragraph looked like this in its original Markdown: [md]: http://daringfireball.net/projects/markdown/
Some blogging services—like Tumblr—support Markdown directly, and there are some editors like TextMate which come bundled with Gruber’s Markdown script. Once you get used to Markdown, though, you find yourself wanting to use it everywhere. That’s where OS X’s Automator actions come in. I’ve set up services for three different things: Markdown to HTML, HTML back to Markdown, and copying Markdown to the clipboard as formatted text.
(If you are not an OS X user, or you are not interested in this level of nerdity, I will not be offended if you stop reading now.)
To do this, you’re going to need to get three Unix scripts:
You’ll want to save Gruber’s scripts somewhere on your local file system; I have them in
/usr/local/bin (the canonical “Unix stuff I’ve added to my system” directory). You can save Swartz’s script there, too, although I actually have it saved as part of the service.
In Automator, create a new service. At the top of the service’s action list, set “Service receives selected text in any application” and check the box for “Replaces selected text.”
The service has only one action: “Run Shell Script”. You can leave the default shell selected (for me that’s
/bin/zsh but for you it may be
/bin/sh), and set “Pass input:” to “to stdin.”
In the box for the shell script, type:
/usr/local/bin/markdown | /usr/local/bin/smartypants
Save this as Markdown to HTML. This should look something like this when you’re finished:
Now create a new service and follow exactly the same steps as above, but uncheck “Replaces selected text” and in the box for the shell script type
/usr/local/bin/markdown | /usr/local/bin/smartypants | /usr/bin/textutil -stdin -stdout -convert rtf -format html | /usr/bin/pbcopy
(That’s all one big command, so don’t type it with line breaks.)
Save that as Copy Markdown as RTF.
Now, create one more service, and follow the same instructions as the first one, but this time, select
/usr/bin/python as the shell in the Run Shell Script service. “Replaces selected text” should be checked again for this one). This time, paste the whole
html2text.py script right into the box for the shell script. Save that as (yes) HTML to Markdown.
To use these services, select the text you want to convert and then select the appropriate command from the Services menu. The Markdown to HTML and HTML to Markdown services replace the selected text. The Copy Markdown as RTF service converts the text and then puts it on the clipboard, so you can then go to your favorite word processor (or whatever) and paste the formatted text in.