Mercurial > public > sg101
annotate gpp/core/markup.py @ 124:9c18250972d5
Refactored the markdown/smiley logic. Created classes for Markdown and Smilify. No longer call render_to_string() in models.py for various models.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 14 Nov 2009 04:32:32 +0000 |
parents | |
children | 48621ba5c385 |
rev | line source |
---|---|
bgneal@124 | 1 """ |
bgneal@124 | 2 This is a thin wrapper around the Markdown class which deals with the |
bgneal@124 | 3 differences in Markdown versions on the production and development server. |
bgneal@124 | 4 This code was inspired by the code in |
bgneal@124 | 5 django/contrib/markup/templatetags/markup.py. |
bgneal@124 | 6 Currently, we only have to worry about Markdown 1.6b and 2.0. |
bgneal@124 | 7 """ |
bgneal@124 | 8 import markdown as _markdown |
bgneal@124 | 9 from django.utils.encoding import force_unicode |
bgneal@124 | 10 |
bgneal@124 | 11 from smiley.utils import smilify |
bgneal@124 | 12 |
bgneal@124 | 13 class Markdown(object): |
bgneal@124 | 14 |
bgneal@124 | 15 def __init__(self, safe_mode='escape'): |
bgneal@124 | 16 # Unicode support only in markdown v1.7 or above. Version_info |
bgneal@124 | 17 # exists only in markdown v1.6.2rc-2 or above. |
bgneal@124 | 18 self.unicode_support = getattr(_markdown, "version_info", None) >= (1, 7) |
bgneal@124 | 19 self.md = _markdown.Markdown(safe_mode=safe_mode) |
bgneal@124 | 20 |
bgneal@124 | 21 def convert(self, s): |
bgneal@124 | 22 if self.unicode_support: |
bgneal@124 | 23 return self.md.convert(force_unicode(s)) |
bgneal@124 | 24 else: |
bgneal@124 | 25 return force_unicode(self.md.convert(s)) |
bgneal@124 | 26 |
bgneal@124 | 27 |
bgneal@124 | 28 def markdown(s): |
bgneal@124 | 29 md = Markdown() |
bgneal@124 | 30 return md.convert(s) |