Mercurial > public > sg101
comparison gpp/core/markup.py @ 128:48621ba5c385
Fixing #36, Smilify doesn't work when a smiley appears first before other text. Refactored the smiley system to produce markdown as well as HTML.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Fri, 20 Nov 2009 01:43:32 +0000 |
parents | 9c18250972d5 |
children | 3a626c48e9ae |
comparison
equal
deleted
inserted
replaced
127:2d299909e074 | 128:48621ba5c385 |
---|---|
1 """ | 1 """ |
2 This is a thin wrapper around the Markdown class which deals with the | 2 Markup related utitlities useful for the entire project. |
3 differences in Markdown versions on the production and development server. | |
4 This code was inspired by the code in | |
5 django/contrib/markup/templatetags/markup.py. | |
6 Currently, we only have to worry about Markdown 1.6b and 2.0. | |
7 """ | 3 """ |
8 import markdown as _markdown | 4 import markdown as _markdown |
9 from django.utils.encoding import force_unicode | 5 from django.utils.encoding import force_unicode |
10 | 6 |
11 from smiley.utils import smilify | 7 from smiley import Smilify |
12 | 8 |
13 class Markdown(object): | 9 class Markdown(object): |
14 | 10 """ |
11 This is a thin wrapper around the Markdown class which deals with the | |
12 differences in Markdown versions on the production and development server. | |
13 This code was inspired by the code in | |
14 django/contrib/markup/templatetags/markup.py. | |
15 Currently, we only have to worry about Markdown 1.6b and 2.0. | |
16 """ | |
15 def __init__(self, safe_mode='escape'): | 17 def __init__(self, safe_mode='escape'): |
16 # Unicode support only in markdown v1.7 or above. Version_info | 18 # Unicode support only in markdown v1.7 or above. Version_info |
17 # exists only in markdown v1.6.2rc-2 or above. | 19 # exists only in markdown v1.6.2rc-2 or above. |
18 self.unicode_support = getattr(_markdown, "version_info", None) >= (1, 7) | 20 self.unicode_support = getattr(_markdown, "version_info", None) >= (1, 7) |
19 self.md = _markdown.Markdown(safe_mode=safe_mode) | 21 self.md = _markdown.Markdown(safe_mode=safe_mode) |
24 else: | 26 else: |
25 return force_unicode(self.md.convert(s)) | 27 return force_unicode(self.md.convert(s)) |
26 | 28 |
27 | 29 |
28 def markdown(s): | 30 def markdown(s): |
31 """ | |
32 A convenience function for one-off markdown jobs. | |
33 """ | |
29 md = Markdown() | 34 md = Markdown() |
30 return md.convert(s) | 35 return md.convert(s) |
36 | |
37 | |
38 class SiteMarkup(object): | |
39 """ | |
40 This class provides site markup by combining markdown and | |
41 our own smiley markup. | |
42 """ | |
43 def __init__(self): | |
44 self.md = Markdown() | |
45 self.smiley = Smilify() | |
46 | |
47 def convert(self, s): | |
48 return self.md.convert(self.smiley.markdown(s)) | |
49 | |
50 | |
51 def site_markup(s): | |
52 """ | |
53 Convenience function for one-off site markup jobs. | |
54 """ | |
55 sm = SiteMarkup() | |
56 return sm.convert(s) |