Skip to content

Commit

Permalink
Render inlines before Markdown
Browse files Browse the repository at this point in the history
django_markup takes code blocks written in Markdown and wraps them in <pre> tags. Pygments, which is used by django_markup when available, takes all the words inside the code blocks and wraps them in various span tags so that the stylesheet may color them appropriately.

Inline rendering depends on BeautifulSoup. BeautifulSoup preserves whitespace in <pre> tags that have no further tags inside of them, but the trouble is that BeautifulSoup sees all those <span> tags, recognizes it as HTML, and strips what it thinks to be extraneous whitespace. Thus all the indentation is gone, and your Python code examples have pretty colors but are useless.

The solution is simply to render inlines before rendering Markdown. Then BeautifulSoup doesn't have any <pre> or <span> tags to mess up. It doesn't know what Markdown's markup is, so it doesn't do anything to it. This also increases efficiency compared to rendering inlines in templates. as long as we've got a model field specifically for rendered data, we might as well put rendered inlines in it, you know?

Sweet.
  • Loading branch information
pigmonkey committed May 31, 2011
1 parent d5def46 commit bb8ba70
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion basic/blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.conf import settings

from basic.blog.managers import PublicManager
from basic.inlines.parser import inlines

import datetime
from taggit.managers import TaggableManager
Expand Down Expand Up @@ -65,8 +66,11 @@ def __unicode__(self):
return u'%s' % self.title

def save(self, *args, **kwargs):
# Inlines must be rendered before markup in order to properly preserve
# whitespace
self.body_markup = inlines(self.body)
# Render the markup and save it in the body_markup field.
self.body_markup = mark_safe(formatter(self.body, filter_name=self.markup))
self.body_markup = mark_safe(formatter(self.body_markup, filter_name=self.markup))
# Call the real save.
super(Post, self).save(*args, **kwargs)

Expand Down

0 comments on commit bb8ba70

Please sign in to comment.