Mercurial > public > sg101
diff gpp/smiley/__init__.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 |
line wrap: on
line diff
--- a/gpp/smiley/__init__.py Mon Nov 16 01:00:28 2009 +0000 +++ b/gpp/smiley/__init__.py Fri Nov 20 01:43:32 2009 +0000 @@ -11,12 +11,21 @@ class Smilify(object): """ - A class to "smilify" text by replacing text with HTML img tags of smilies. + A class to "smilify" text by replacing text with either HTML img tags + or markdown syntax for smiley images. """ + HTML = 0 + MARKDOWN = 1 + def __init__(self): self.map = Smiley.objects.get_smiley_map() - def convert(self, value, autoescape=False): + def _convert(self, value, rep_index, autoescape=False): + """ + Converts and returns the supplied text with either the + HTML or markdown version of the smileys according to the + output parameter. + """ if not autoescape or isinstance(value, SafeData): esc = lambda x: x else: @@ -25,17 +34,39 @@ words = value.split() for i, word in enumerate(words): if word in self.map: - words[i] = self.map[word] + words[i] = self.map[word][rep_index] else: words[i] = esc(words[i]) return u' '.join(words) + + def html(self, value, autoescape=False): + """ + Converts the supplied text by replacing the smiley codes with + HTML img tags. + """ + return self._convert(value, self.HTML, autoescape=autoescape) + + def markdown(self, value, autoescape=False): + """ + Converts the supplied text by replacing the smiley codes with + markdown image syntax. + """ + return self._convert(value, self.MARKDOWN, autoescape=autoescape) -def smilify(value, autoescape=False): +def smilify_html(value, autoescape=False): """ A convenience function to "smilify" text by replacing text with HTML img tags of smilies. """ s = Smilify() - return s.convert(value, autoescape) + return s.html(value, autoescape=autoescape) + +def smilify_markdown(value, autoescape=False): + """ + A convenience function to "smilify" text by replacing text with + markdown syntax for the images of smilies. + """ + s = Smilify() + return s.markdown(value, autoescape=autoescape)