Mercurial > public > sg101
annotate gpp/core/markup.py @ 295:26fc9ac9a0eb
Fixing #133; add the ability to edit a topic's title when you edit the first post in that topic.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 08 Jan 2011 22:04:54 +0000 |
parents | 3a626c48e9ae |
children | a321685505cc |
rev | line source |
---|---|
bgneal@124 | 1 """ |
bgneal@128 | 2 Markup related utitlities useful for the entire project. |
bgneal@124 | 3 """ |
bgneal@124 | 4 import markdown as _markdown |
bgneal@124 | 5 from django.utils.encoding import force_unicode |
bgneal@124 | 6 |
bgneal@211 | 7 from smiley import SmilifyMarkdown |
bgneal@124 | 8 |
bgneal@124 | 9 class Markdown(object): |
bgneal@128 | 10 """ |
bgneal@128 | 11 This is a thin wrapper around the Markdown class which deals with the |
bgneal@128 | 12 differences in Markdown versions on the production and development server. |
bgneal@128 | 13 This code was inspired by the code in |
bgneal@128 | 14 django/contrib/markup/templatetags/markup.py. |
bgneal@128 | 15 Currently, we only have to worry about Markdown 1.6b and 2.0. |
bgneal@128 | 16 """ |
bgneal@124 | 17 def __init__(self, safe_mode='escape'): |
bgneal@124 | 18 # Unicode support only in markdown v1.7 or above. Version_info |
bgneal@124 | 19 # exists only in markdown v1.6.2rc-2 or above. |
bgneal@124 | 20 self.unicode_support = getattr(_markdown, "version_info", None) >= (1, 7) |
bgneal@124 | 21 self.md = _markdown.Markdown(safe_mode=safe_mode) |
bgneal@124 | 22 |
bgneal@124 | 23 def convert(self, s): |
bgneal@124 | 24 if self.unicode_support: |
bgneal@124 | 25 return self.md.convert(force_unicode(s)) |
bgneal@124 | 26 else: |
bgneal@124 | 27 return force_unicode(self.md.convert(s)) |
bgneal@124 | 28 |
bgneal@124 | 29 |
bgneal@124 | 30 def markdown(s): |
bgneal@128 | 31 """ |
bgneal@128 | 32 A convenience function for one-off markdown jobs. |
bgneal@128 | 33 """ |
bgneal@124 | 34 md = Markdown() |
bgneal@124 | 35 return md.convert(s) |
bgneal@128 | 36 |
bgneal@128 | 37 |
bgneal@128 | 38 class SiteMarkup(object): |
bgneal@128 | 39 """ |
bgneal@128 | 40 This class provides site markup by combining markdown and |
bgneal@128 | 41 our own smiley markup. |
bgneal@128 | 42 """ |
bgneal@128 | 43 def __init__(self): |
bgneal@128 | 44 self.md = Markdown() |
bgneal@211 | 45 self.smiley = SmilifyMarkdown() |
bgneal@128 | 46 |
bgneal@128 | 47 def convert(self, s): |
bgneal@211 | 48 return self.md.convert(self.smiley.convert(s)) |
bgneal@128 | 49 |
bgneal@128 | 50 |
bgneal@128 | 51 def site_markup(s): |
bgneal@128 | 52 """ |
bgneal@128 | 53 Convenience function for one-off site markup jobs. |
bgneal@128 | 54 """ |
bgneal@128 | 55 sm = SiteMarkup() |
bgneal@128 | 56 return sm.convert(s) |