annotate gpp/smiley/__init__.py @ 133:c515b7401078

Use the new common way to apply markItUp to textareas and to get the smiley and markdown help dialogs for all the remaining apps except for forums and comments.
author Brian Neal <bgneal@gmail.com>
date Fri, 27 Nov 2009 00:21:47 +0000
parents 48621ba5c385
children 3a626c48e9ae
rev   line source
bgneal@124 1 """
bgneal@124 2 Smiley class and function.
bgneal@124 3 """
bgneal@124 4 import re
bgneal@124 5
bgneal@124 6 from django.utils.safestring import SafeData
bgneal@124 7 from django.utils.html import conditional_escape
bgneal@124 8
bgneal@124 9 from smiley.models import Smiley
bgneal@124 10
bgneal@124 11
bgneal@124 12 class Smilify(object):
bgneal@124 13 """
bgneal@128 14 A class to "smilify" text by replacing text with either HTML img tags
bgneal@128 15 or markdown syntax for smiley images.
bgneal@124 16 """
bgneal@128 17 HTML = 0
bgneal@128 18 MARKDOWN = 1
bgneal@128 19
bgneal@124 20 def __init__(self):
bgneal@124 21 self.map = Smiley.objects.get_smiley_map()
bgneal@124 22
bgneal@128 23 def _convert(self, value, rep_index, autoescape=False):
bgneal@128 24 """
bgneal@128 25 Converts and returns the supplied text with either the
bgneal@128 26 HTML or markdown version of the smileys according to the
bgneal@128 27 output parameter.
bgneal@128 28 """
bgneal@124 29 if not autoescape or isinstance(value, SafeData):
bgneal@124 30 esc = lambda x: x
bgneal@124 31 else:
bgneal@124 32 esc = conditional_escape
bgneal@124 33
bgneal@124 34 words = value.split()
bgneal@124 35 for i, word in enumerate(words):
bgneal@124 36 if word in self.map:
bgneal@128 37 words[i] = self.map[word][rep_index]
bgneal@124 38 else:
bgneal@124 39 words[i] = esc(words[i])
bgneal@124 40 return u' '.join(words)
bgneal@128 41
bgneal@128 42 def html(self, value, autoescape=False):
bgneal@128 43 """
bgneal@128 44 Converts the supplied text by replacing the smiley codes with
bgneal@128 45 HTML img tags.
bgneal@128 46 """
bgneal@128 47 return self._convert(value, self.HTML, autoescape=autoescape)
bgneal@128 48
bgneal@128 49 def markdown(self, value, autoescape=False):
bgneal@128 50 """
bgneal@128 51 Converts the supplied text by replacing the smiley codes with
bgneal@128 52 markdown image syntax.
bgneal@128 53 """
bgneal@128 54 return self._convert(value, self.MARKDOWN, autoescape=autoescape)
bgneal@124 55
bgneal@124 56
bgneal@128 57 def smilify_html(value, autoescape=False):
bgneal@124 58 """
bgneal@124 59 A convenience function to "smilify" text by replacing text with HTML
bgneal@124 60 img tags of smilies.
bgneal@124 61 """
bgneal@124 62 s = Smilify()
bgneal@128 63 return s.html(value, autoescape=autoescape)
bgneal@124 64
bgneal@128 65
bgneal@128 66 def smilify_markdown(value, autoescape=False):
bgneal@128 67 """
bgneal@128 68 A convenience function to "smilify" text by replacing text with
bgneal@128 69 markdown syntax for the images of smilies.
bgneal@128 70 """
bgneal@128 71 s = Smilify()
bgneal@128 72 return s.markdown(value, autoescape=autoescape)