Mercurial > public > sg101
annotate gpp/smiley/__init__.py @ 460:2ff5f4c1476d
Fixing #221. Found some additional cases where the forum moderator check was failing. Replaced this code with calls to the new permissions module.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 03 Jul 2011 19:35:09 +0000 |
parents | 72fd300685d5 |
children |
rev | line source |
---|---|
bgneal@124 | 1 """ |
bgneal@211 | 2 Smiley classes and functions. |
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@211 | 12 class SmilifyHtml(object): |
bgneal@124 | 13 """ |
bgneal@211 | 14 A class to "smilify" text by replacing text with HTML img tags for smiley |
bgneal@211 | 15 images. |
bgneal@124 | 16 """ |
bgneal@124 | 17 def __init__(self): |
bgneal@124 | 18 self.map = Smiley.objects.get_smiley_map() |
bgneal@124 | 19 |
bgneal@211 | 20 def convert(self, value, autoescape=False): |
bgneal@128 | 21 """ |
bgneal@211 | 22 Converts and returns the supplied text with the HTML version of the |
bgneal@211 | 23 smileys. |
bgneal@128 | 24 """ |
bgneal@286 | 25 if not value: |
bgneal@286 | 26 return u'' |
bgneal@286 | 27 |
bgneal@124 | 28 if not autoescape or isinstance(value, SafeData): |
bgneal@124 | 29 esc = lambda x: x |
bgneal@124 | 30 else: |
bgneal@124 | 31 esc = conditional_escape |
bgneal@124 | 32 |
bgneal@124 | 33 words = value.split() |
bgneal@124 | 34 for i, word in enumerate(words): |
bgneal@124 | 35 if word in self.map: |
bgneal@211 | 36 words[i] = self.map[word] |
bgneal@124 | 37 else: |
bgneal@124 | 38 words[i] = esc(words[i]) |
bgneal@124 | 39 return u' '.join(words) |
bgneal@128 | 40 |
bgneal@211 | 41 |
bgneal@211 | 42 class SmilifyMarkdown(object): |
bgneal@211 | 43 """ |
bgneal@211 | 44 A class to "smilify" text by replacing text with Markdown image syntax for |
bgneal@211 | 45 smiley images. |
bgneal@211 | 46 """ |
bgneal@211 | 47 def __init__(self): |
bgneal@211 | 48 self.regexes = Smiley.objects.get_smiley_regexes() |
bgneal@211 | 49 |
bgneal@211 | 50 def convert(self, s): |
bgneal@128 | 51 """ |
bgneal@211 | 52 Returns a string copy of the input s that has the smiley codes replaced |
bgneal@211 | 53 with Markdown for smiley images. |
bgneal@128 | 54 """ |
bgneal@286 | 55 if not s: |
bgneal@286 | 56 return u'' |
bgneal@286 | 57 |
bgneal@211 | 58 for regex, repl in self.regexes: |
bgneal@211 | 59 s = regex.sub(repl, s) |
bgneal@211 | 60 return s |
bgneal@128 | 61 |
bgneal@124 | 62 |
bgneal@128 | 63 def smilify_html(value, autoescape=False): |
bgneal@124 | 64 """ |
bgneal@124 | 65 A convenience function to "smilify" text by replacing text with HTML |
bgneal@124 | 66 img tags of smilies. |
bgneal@124 | 67 """ |
bgneal@211 | 68 s = SmilifyHtml() |
bgneal@211 | 69 return s.convert(value, autoescape=autoescape) |
bgneal@124 | 70 |