bgneal@124: """ bgneal@124: Smiley class and function. bgneal@124: """ bgneal@124: import re bgneal@124: bgneal@124: from django.utils.safestring import SafeData bgneal@124: from django.utils.html import conditional_escape bgneal@124: bgneal@124: from smiley.models import Smiley bgneal@124: bgneal@124: bgneal@124: class Smilify(object): bgneal@124: """ bgneal@124: A class to "smilify" text by replacing text with HTML img tags of smilies. bgneal@124: """ bgneal@124: def __init__(self): bgneal@124: self.map = Smiley.objects.get_smiley_map() bgneal@124: bgneal@124: def convert(self, value, autoescape=False): bgneal@124: if not autoescape or isinstance(value, SafeData): bgneal@124: esc = lambda x: x bgneal@124: else: bgneal@124: esc = conditional_escape bgneal@124: bgneal@124: words = value.split() bgneal@124: for i, word in enumerate(words): bgneal@124: if word in self.map: bgneal@124: words[i] = self.map[word] bgneal@124: else: bgneal@124: words[i] = esc(words[i]) bgneal@124: return u' '.join(words) bgneal@124: bgneal@124: bgneal@124: def smilify(value, autoescape=False): bgneal@124: """ bgneal@124: A convenience function to "smilify" text by replacing text with HTML bgneal@124: img tags of smilies. bgneal@124: """ bgneal@124: s = Smilify() bgneal@124: return s.convert(value, autoescape) bgneal@124: