Smartypants 12.23.2006

Just installed smartypants — so that things like quotes and dashes will be converted to proper typographic form.

Integrating it with posting was incredibly easy: I’m basically overriding the save method in my models.py:

  def save(self):
            import markdown
            import smartypants
            self.html_body = markdown.markdown(smartypants.smartyPants(self.body))
            super(Entry, self).save() #Call the real save() method.

html_body is a database field that stores the converted html source version of the post — and body stores the actual markdown body text.

I chose to do it this way to save on processing when viewing a page. Rather than running each post through markdown+smartypants on the fly, that conversion process will only run on each entry save.

Edit later: Since this post, I’ve realized that I needed to reverse the order of processing. Now, I have:

 self.html_body = smartypants.smartyPants(markdown.markdown(self.body))

The reason is that before, smartypants converts the quotes in code and pre blocks to the correct typographical HTML escape. By default, smartypants is set to ignore stuff in <code> and <pre>, but since it was acting on the text prior to Markdown, it wasn’t registering.

After the change, Markdown processes the text first, converting shorthand into valid HTML — and then Smartypants does its thing whilst correctly leaving code and pre blocks alone.