Well, I created my first TextMate command. As with most development firsts, it’s really just a mashup of code scavenged elsewhere.
El Problemo…
I like creating technical documentation in plain text. It doesn’t really keep me from thinking about formatting as I intended it to, but it does make it easier to get my thoughts down. I usually end up switching from plain text to Markdown or Textile at some point — not to get it output as HTML, but only so I get TextMate’s handy symbol list to help me navigate the file.
However, when it comes time to share my documentation, I usually have to spend time makin’ it look purty for others. I usually used Textile so I could paste it right into a Basecamp Writeboard. But since I started using Tumblr I decided to take a closer look at Markdown. I do like it better, but that’s not really the point.
The point is I knew nobody was going to want to look at plain Markdown, so it was time for me to explore converting it to HTML. It made nice little ids for all my headings, but some of my headings had the same text, so ids were used twice — which made creating a ToC a little difficult.
Then I remembered the JS-generated ToC on QuirksMode. I pulled that code down — slightly modified to deal with my dueling ids, as well as some other details — and pasted it into my generated HTML 1.
But I thought… I’m probably going to be doing this sort of thing a lot. Ideally then, I want a command that converts Markdown to HTML, pastes in my JS and CSS, and then what the heck — why not have it save the file and open it in TextMate, instead of opening a new ‘untitled’ file.
As I Wish…
- First I duplicated the Markdown > MultiMarkdown > Convert Document to HTML command.
- I checked out the files that were called by that command, and created a new file in the same directory as those 2. Since I know PHP best and wanted to get this done in a hurry, I used PHP — sorry Ruby and/or Python lovahs. I named my script
InsertTOCScripts.php. You can check it out on Pastie.org. It creates an HTML file in the same directory with the same name as your Markdown file, then opens that file in TextMate. - The original convert-to-html command looked like this:
InsertFormatHeader.rb|MultiMarkdown.pl|"${TM_SMARTYPANTS:-SmartyPants.pl}". I changed it to thisInsertFormatHeader.rb|MultiMarkdown.pl|"${TM_SMARTYPANTS:-SmartyPants.pl}"|InsertTOCScripts.php. I don’t know what the pipes do there, but I just followed the pattern I saw. That usually works out pretty well.
Caveats
- I have no clue how you handle errors in TextMate bundle command.
- This assumes you have the PHP CLI set up on your machine, and the
mateshortcut to load files with TextMate from the command line. - Totally untested on any machine but my own.
- I’d call it a work-in-progress, but that could be misleading if I never work on it again.
- If something goes wrong, it’s probably about permissions. I mean, that’s what it always comes down to, right?