Navigation

PHP Markdown Extra + PHP SmartyPants Typographer Text Filter for MarsEdit

Tuesday, November 11th, 2008

If you already know what this is and just want to download it, click here to skip over my explanation.

Markdown, MarsEdit and Text Filters

On this blog I’m using the wonderful PHP Markdown Extra and PHP SmartyPants Typographer WordPress plugins by Michel Fortin. Both are WordPress-ready PHP reimplementations of John Gruber projects, and have been very helpful in allowing me to write unencumbered by HTML.

I’m also a big fan of MarsEdit, Brent Simmons Gus Mueller Daniel Jalkut’s amazing blog editor, and most especially of its powerful Preview feature. You set up an HTML template that looks similar to your blog and MarsEdit will fill in that template and render your work in progress into a live-updating WebView.

It’s an amazing feature, made more amazing by the fact that you can define custom filters to run over your blog entry text before it’s displayed in HTML. This is extremely useful, since I write all my posts in Markdown and they are only ever transformed to HTML by WordPress at display time — they remain as Markdown in the database.

MarsEdit comes with a Markdown filter built-in, which I had used for a long time. However, it doesn’t support all of the features of PHP Markdown Extra, namely footnotes,1 which has increasingly gotten on my nerves. While writing my last post, OCMock and the iPhone, I was using footnotes enough that I decided the time to act had finally arrived.

Download it already!

This MarsEdit text filter will run PHP Markdown Extra 1.2.2 and PHP SmartyPants Typographer 1.0 over your posts before displaying them in the Preview window.

Download PHP Markdown Extra + PHP SmartyPants Typographer_1.0.zip

In case someone wants modify this to only run Markdown or SmartyPants, I’m providing this under the MIT license. Share and enjoy.

A Slight Quirk

MarsEdit’s text filtering system only allows you to write text filters with Perl. So the current setup is something like this:

  • A Perl script slurps all its input and feeds it to a child PHP process.
  • That PHP script runs Markdown() and then SmartyPants() and outputs the result.
  • When the PHP script exits, the Perl script reads its child’s output buffer and outputs whatever was in there to MarsEdit.

This is certainly not the most ideal scenario, but unfortunately only one MarsEdit’s developer can solve. In the meantime feel free to use my code a basis for other bridges2 to other languages.

I would like to see MarsEdit’s text filtering improved, where the TextFilter bundle’s Info.plist specifies a file to run and MarsEdit simply executes that file, pipes the article to its input and reads its output. That decision is of course up to MarsEdit’s developer.


  1. They’re really quite useful. 

  2. I would describe this bridge as “rickety, waterlogged and termite-infested”, but it seems to work! 

Comments

  1. Daniel Jalkut replied on November 11th, 2008:

    Thanks for writing about this! And bravo for making your own filter.

    The fact that users can create their own filters is under-publicized. I’d love for more people to know about this option, so I am especially happy that you wrote about it.

    It must be a side-effect of this relative lack of recognition, that there are some quirks remaining to be ironed out. I didn’t realize the limitations you mentioned about its perl assumptions.

    I want to point out that the system is currently more functional than you know, but it does have an obvious failing with led you to the conclusion that it only supports perl filters. In fact, it looks like it requires the text filter script to be the name of the filter, plus the extension “.pl”. That would make any reasonable person assume it must be perl, but in fact the script can be any shebang-style script.

    Try putting #!/usr/bin/php in the Whatever.pl script and you’ll see what I mean.

    I’ll get this sorted out in an update. I like your idea to remove all guesswork and just put the full name of the appropriate script inside an Info.plist.

    Daniel

  2. Colin Barrett replied on November 11th, 2008:

    That’s great! I’ll update this to remove the unnecessary Perl hackery :)

    Thanks a lot, Daniel. Nothing but love for MarsEdit, it’s great.

  3. mitcho replied on November 12th, 2008:

    Brilliant. Do you know of a Windows text editor to do essentially the same thing? I’m relegated to Windows machines at work and am tempted to write a similar filter for Notepad++.

  4. iamthewalr.us : PHP Markdown Extra + PHP SmartyPants Typographer Text Filter for MarsEdit 1.1 released replied on November 12th, 2008:

    […] The only thing different about this release from 1.0 is that I removed an unnecessary Perl script. Thanks to Daniel Jalkut for the suggestion. […]

  5. Colin Barrett replied on November 12th, 2008:

    mitcho:

    I don’t know much about Windows software, but ecto might do the trick?

  6. Daniel Jalkut replied on November 12th, 2008:

    Colin: the ecto link you listed is to the new owners of ecto for the Mac. Note that they do not maintain the (always separate, just identically named) ecto for Windows. I’m not sure if it supports this kind of extensible template, or not, but the project doesn’t seem particularly active: http://mineblogging.com/

    Other windows apps that are worth looking at are Windows Live Writer and BlogJet.

  7. Daniel Jalkut replied on November 13th, 2008:

    Colin - turns out I was wrong about the requirement for scripts to end in .py. That was some stale code I was looking at. The shipping version of MarsEdit should run any text filter with the rule:

    A script file is found with a name that matches the filter folder, and the script ends in any of “php”, “pl”, “py”, “rb”, or “sh”.

    I don’t see any reason to restrict it to these so I’ll be changing it to accept any script starting with the name of the filter folder. In the longer term I still like your idea of using an Info.plist.