Mercurial > public > sg101
comparison gpp/smiley/__init__.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 | dbd703f7d63a |
children | 48621ba5c385 |
comparison
equal
deleted
inserted
replaced
123:3ae999b0c53b | 124:9c18250972d5 |
---|---|
1 """ | |
2 Smiley class and function. | |
3 """ | |
4 import re | |
5 | |
6 from django.utils.safestring import SafeData | |
7 from django.utils.html import conditional_escape | |
8 | |
9 from smiley.models import Smiley | |
10 | |
11 | |
12 class Smilify(object): | |
13 """ | |
14 A class to "smilify" text by replacing text with HTML img tags of smilies. | |
15 """ | |
16 def __init__(self): | |
17 self.map = Smiley.objects.get_smiley_map() | |
18 | |
19 def convert(self, value, autoescape=False): | |
20 if not autoescape or isinstance(value, SafeData): | |
21 esc = lambda x: x | |
22 else: | |
23 esc = conditional_escape | |
24 | |
25 words = value.split() | |
26 for i, word in enumerate(words): | |
27 if word in self.map: | |
28 words[i] = self.map[word] | |
29 else: | |
30 words[i] = esc(words[i]) | |
31 return u' '.join(words) | |
32 | |
33 | |
34 def smilify(value, autoescape=False): | |
35 """ | |
36 A convenience function to "smilify" text by replacing text with HTML | |
37 img tags of smilies. | |
38 """ | |
39 s = Smilify() | |
40 return s.convert(value, autoescape) | |
41 |