Mercurial > public > sg101
diff gpp/smiley/models.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 |
line wrap: on
line diff
--- a/gpp/smiley/models.py Fri May 07 02:56:49 2010 +0000 +++ b/gpp/smiley/models.py Sat May 08 23:44:59 2010 +0000 @@ -1,6 +1,8 @@ """ Models for the smiley application. """ +import re + from django.db import models from django.core.cache import cache @@ -11,20 +13,22 @@ def get_smiley_map(self): """ - Returns a dictionary of 2-tuples, indexed by smiley codes. - Element 0 of the tuple is the HTML representation of the smiley, - and element 1 is the markdown version. + Returns a dictionary, the keys are smiley codes. + The values are the HTML representations of the keys. The dictionary is cached. """ map = cache.get('smiley_map') if map: return map - map = dict((s.code, (s.html(), s.markdown())) for s in self.all()) + map = dict((s.code, s.html()) for s in self.all()) cache.set('smiley_map', map, CACHE_TIMEOUT) return map def get_smilies(self, extra=False): + """ + Returns smiley model instances filtered by the extra flag. + """ key = 'smileys' if not extra else 'smileys_extra' smilies = cache.get(key) if smilies: @@ -34,6 +38,21 @@ cache.set(key, smilies, CACHE_TIMEOUT) return smilies + def get_smiley_regexes(self): + """ + Returns a list of 2-tuples of the form: (regex, repl) + where regex is a regular expression for a smiley and + repl is the replacement image in Markdown format. + """ + regexes = cache.get('smiley_regexes') + if regexes: + return regexes + + regexes = [(re.compile(r"(^|\s|(?<=\s))%s(\s|$)" % re.escape(s.code)), + r"\1%s\2" % s.markdown()) for s in self.all()] + cache.set('smiley_regexes', regexes, CACHE_TIMEOUT) + return regexes + class Smiley(models.Model): image = models.ImageField(upload_to='smiley/images/')