Mercurial > public > sg101
comparison core/markup.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/core/markup.py@f54bf3b3bece |
children | 216f06267e2d |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 Markup related utitlities useful for the entire project. | |
3 """ | |
4 import markdown as _markdown | |
5 from django.utils.encoding import force_unicode | |
6 | |
7 from smiley import SmilifyMarkdown | |
8 | |
9 class Markdown(object): | |
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 """ | |
17 def __init__(self, safe_mode='escape'): | |
18 # Unicode support only in markdown v1.7 or above. Version_info | |
19 # exists only in markdown v1.6.2rc-2 or above. | |
20 self.unicode_support = getattr(_markdown, "version_info", None) >= (1, 7) | |
21 self.md = _markdown.Markdown(safe_mode=safe_mode, | |
22 extensions=['urlize', 'nl2br', 'del']) | |
23 | |
24 def convert(self, s): | |
25 if self.unicode_support: | |
26 return self.md.convert(force_unicode(s)) | |
27 else: | |
28 return force_unicode(self.md.convert(s)) | |
29 | |
30 | |
31 def markdown(s): | |
32 """ | |
33 A convenience function for one-off markdown jobs. | |
34 """ | |
35 md = Markdown() | |
36 return md.convert(s) | |
37 | |
38 | |
39 class SiteMarkup(object): | |
40 """ | |
41 This class provides site markup by combining markdown and | |
42 our own smiley markup. | |
43 """ | |
44 def __init__(self): | |
45 self.md = Markdown() | |
46 self.smiley = SmilifyMarkdown() | |
47 | |
48 def convert(self, s): | |
49 return self.md.convert(self.smiley.convert(s)) | |
50 | |
51 | |
52 def site_markup(s): | |
53 """ | |
54 Convenience function for one-off site markup jobs. | |
55 """ | |
56 sm = SiteMarkup() | |
57 return sm.convert(s) |