Using SmartyPants with Haml

If only a tiny fraction of you is a typography nut, you probably adore correct punctuation and correct use of quotation marks. Despite that, maybe you simply do not remember the awkward keystrokes required to actually type those marks in your prose, though. (On the Mac, Option-[ and Cmd-Option-[ for example.)

Luckily, John Gruber created a Perl tool called SmartyPants several years back, which will take care of converting straight quotes into curly quotes, regular dashes into em-dashes and so forth. It has since been adopted as an optional filter-mode for most (if not all) of the modern Markdown implementations.

Using Haml in your Rails project, you may be aware of Haml's filter functionality available in your view templates:

:markdown
  Using filters, embedding [Markdown](http://daringfireball.net/projects/markdown/) is *easy*.

With proper indentation (and the availability of a supported Markdown interpreter such as RDiscount), Haml will automatically render the indented parts using Markdown.

Speaking of RDiscount, it also supports the :smart option, which will apply the punctuation transformations of SmartyPants to the given text. This is, however, not supported in the standard :markdown filter syntax of Haml.

It's very simple to build your own filter, though:

With this file in place in, for example, config/initializers/markdown_smart.rb, you can now use the :markdownsmart filter in your templates to render Markdown text and to automatically convert your punctuation to "smart" punctuation based on SmartyPants.