Mercurial > public > sg101
comparison gpp/smiley/__init__.py @ 211:3a626c48e9ae
Fix #81: could not get paragraphs in Markdown due to the interaction between smiley and Markdown. Refactored the smilify code to use a series of regular expressions over the text when working with markdown.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 08 May 2010 23:44:59 +0000 |
parents | 48621ba5c385 |
children | 72fd300685d5 |
comparison
equal
deleted
inserted
replaced
210:77b606e0ad5f | 211:3a626c48e9ae |
---|---|
1 """ | 1 """ |
2 Smiley class and function. | 2 Smiley classes and functions. |
3 """ | 3 """ |
4 import re | 4 import re |
5 | 5 |
6 from django.utils.safestring import SafeData | 6 from django.utils.safestring import SafeData |
7 from django.utils.html import conditional_escape | 7 from django.utils.html import conditional_escape |
8 | 8 |
9 from smiley.models import Smiley | 9 from smiley.models import Smiley |
10 | 10 |
11 | 11 |
12 class Smilify(object): | 12 class SmilifyHtml(object): |
13 """ | 13 """ |
14 A class to "smilify" text by replacing text with either HTML img tags | 14 A class to "smilify" text by replacing text with HTML img tags for smiley |
15 or markdown syntax for smiley images. | 15 images. |
16 """ | 16 """ |
17 HTML = 0 | |
18 MARKDOWN = 1 | |
19 | |
20 def __init__(self): | 17 def __init__(self): |
21 self.map = Smiley.objects.get_smiley_map() | 18 self.map = Smiley.objects.get_smiley_map() |
22 | 19 |
23 def _convert(self, value, rep_index, autoescape=False): | 20 def convert(self, value, autoescape=False): |
24 """ | 21 """ |
25 Converts and returns the supplied text with either the | 22 Converts and returns the supplied text with the HTML version of the |
26 HTML or markdown version of the smileys according to the | 23 smileys. |
27 output parameter. | |
28 """ | 24 """ |
29 if not autoescape or isinstance(value, SafeData): | 25 if not autoescape or isinstance(value, SafeData): |
30 esc = lambda x: x | 26 esc = lambda x: x |
31 else: | 27 else: |
32 esc = conditional_escape | 28 esc = conditional_escape |
33 | 29 |
34 words = value.split() | 30 words = value.split() |
35 for i, word in enumerate(words): | 31 for i, word in enumerate(words): |
36 if word in self.map: | 32 if word in self.map: |
37 words[i] = self.map[word][rep_index] | 33 words[i] = self.map[word] |
38 else: | 34 else: |
39 words[i] = esc(words[i]) | 35 words[i] = esc(words[i]) |
40 return u' '.join(words) | 36 return u' '.join(words) |
41 | 37 |
42 def html(self, value, autoescape=False): | 38 |
39 class SmilifyMarkdown(object): | |
40 """ | |
41 A class to "smilify" text by replacing text with Markdown image syntax for | |
42 smiley images. | |
43 """ | |
44 def __init__(self): | |
45 self.regexes = Smiley.objects.get_smiley_regexes() | |
46 | |
47 def convert(self, s): | |
43 """ | 48 """ |
44 Converts the supplied text by replacing the smiley codes with | 49 Returns a string copy of the input s that has the smiley codes replaced |
45 HTML img tags. | 50 with Markdown for smiley images. |
46 """ | 51 """ |
47 return self._convert(value, self.HTML, autoescape=autoescape) | 52 for regex, repl in self.regexes: |
53 s = regex.sub(repl, s) | |
54 return s | |
48 | 55 |
49 def markdown(self, value, autoescape=False): | |
50 """ | |
51 Converts the supplied text by replacing the smiley codes with | |
52 markdown image syntax. | |
53 """ | |
54 return self._convert(value, self.MARKDOWN, autoescape=autoescape) | |
55 | |
56 | 56 |
57 def smilify_html(value, autoescape=False): | 57 def smilify_html(value, autoescape=False): |
58 """ | 58 """ |
59 A convenience function to "smilify" text by replacing text with HTML | 59 A convenience function to "smilify" text by replacing text with HTML |
60 img tags of smilies. | 60 img tags of smilies. |
61 """ | 61 """ |
62 s = Smilify() | 62 s = SmilifyHtml() |
63 return s.html(value, autoescape=autoescape) | 63 return s.convert(value, autoescape=autoescape) |
64 | 64 |
65 | |
66 def smilify_markdown(value, autoescape=False): | |
67 """ | |
68 A convenience function to "smilify" text by replacing text with | |
69 markdown syntax for the images of smilies. | |
70 """ | |
71 s = Smilify() | |
72 return s.markdown(value, autoescape=autoescape) |