# HG changeset patch # User Brian Neal # Date 1428890936 18000 # Node ID 98d2388b6bb226473f4eaa10a559fe4af29c84c2 # Parent 78b459d4ab17d4233e2ac13fbb7fceafab6122d1 Smiley app refactor for Django 1.7.7 upgrade. diff -r 78b459d4ab17 -r 98d2388b6bb2 core/markup.py --- a/core/markup.py Thu Apr 09 19:43:07 2015 -0500 +++ b/core/markup.py Sun Apr 12 21:08:56 2015 -0500 @@ -5,7 +5,7 @@ import markdown as _markdown from django.utils.encoding import force_unicode -from smiley import SmilifyMarkdown +from smiley.utils import SmilifyMarkdown from core.mdexts.urlize import UrlizeExtension from core.mdexts.deleted import DelExtension from core.html import clean_html diff -r 78b459d4ab17 -r 98d2388b6bb2 shoutbox/models.py --- a/shoutbox/models.py Thu Apr 09 19:43:07 2015 -0500 +++ b/shoutbox/models.py Sun Apr 12 21:08:56 2015 -0500 @@ -7,7 +7,7 @@ from django.contrib.auth.models import User from django.utils.html import escape, urlize -from smiley import smilify_html +from smiley.utils import smilify_html class Shout(models.Model): diff -r 78b459d4ab17 -r 98d2388b6bb2 smiley/__init__.py --- a/smiley/__init__.py Thu Apr 09 19:43:07 2015 -0500 +++ b/smiley/__init__.py Sun Apr 12 21:08:56 2015 -0500 @@ -1,70 +0,0 @@ -""" -Smiley classes and functions. - -""" -from django.utils.safestring import SafeData -from django.utils.html import conditional_escape - -from smiley.models import Smiley - - -class SmilifyHtml(object): - """ - A class to "smilify" text by replacing text with HTML img tags for smiley - images. - """ - def __init__(self): - self.map = Smiley.objects.get_smiley_map() - - def convert(self, value, autoescape=False): - """ - Converts and returns the supplied text with the HTML version of the - smileys. - """ - if not value: - return u'' - - if not autoescape or isinstance(value, SafeData): - esc = lambda x: x - else: - esc = conditional_escape - - words = value.split() - for i, word in enumerate(words): - if word in self.map: - words[i] = self.map[word] - else: - words[i] = esc(words[i]) - return u' '.join(words) - - -class SmilifyMarkdown(object): - """ - A class to "smilify" text by replacing text with Markdown image syntax for - smiley images. - """ - def __init__(self, relative_urls=True): - self.regexes = Smiley.objects.get_smiley_regexes( - relative_urls=relative_urls) - - def convert(self, s): - """ - Returns a string copy of the input s that has the smiley codes replaced - with Markdown for smiley images. - """ - if not s: - return u'' - - for regex, repl in self.regexes: - s = regex.sub(repl, s) - return s - - -def smilify_html(value, autoescape=False): - """ - A convenience function to "smilify" text by replacing text with HTML - img tags of smilies. - """ - s = SmilifyHtml() - return s.convert(value, autoescape=autoescape) - diff -r 78b459d4ab17 -r 98d2388b6bb2 smiley/templatetags/smiley_tags.py --- a/smiley/templatetags/smiley_tags.py Thu Apr 09 19:43:07 2015 -0500 +++ b/smiley/templatetags/smiley_tags.py Sun Apr 12 21:08:56 2015 -0500 @@ -1,11 +1,12 @@ """ -Template tags for the smiley application. +Template tags for the smiley application. """ from django import template from django.template.defaultfilters import stringfilter from django.utils.safestring import mark_safe from smiley.models import Smiley +from smiley.utils import smilify_html register = template.Library() @@ -14,7 +15,6 @@ @stringfilter def smiley_html(value, autoescape=False): """A filter to "smilify" text by replacing text with HTML img tags of smilies.""" - from smiley import smilify_html return mark_safe(smilify_html(value, autoescape=autoescape)) smiley_html.needs_autoescape = True diff -r 78b459d4ab17 -r 98d2388b6bb2 smiley/utils.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/smiley/utils.py Sun Apr 12 21:08:56 2015 -0500 @@ -0,0 +1,69 @@ +""" +Smiley classes and functions. + +""" +from django.utils.safestring import SafeData +from django.utils.html import conditional_escape + +from models import Smiley + + +class SmilifyHtml(object): + """ + A class to "smilify" text by replacing text with HTML img tags for smiley + images. + """ + def __init__(self): + self.map = Smiley.objects.get_smiley_map() + + def convert(self, value, autoescape=False): + """ + Converts and returns the supplied text with the HTML version of the + smileys. + """ + if not value: + return u'' + + if not autoescape or isinstance(value, SafeData): + esc = lambda x: x + else: + esc = conditional_escape + + words = value.split() + for i, word in enumerate(words): + if word in self.map: + words[i] = self.map[word] + else: + words[i] = esc(words[i]) + return u' '.join(words) + + +class SmilifyMarkdown(object): + """ + A class to "smilify" text by replacing text with Markdown image syntax for + smiley images. + """ + def __init__(self, relative_urls=True): + self.regexes = Smiley.objects.get_smiley_regexes( + relative_urls=relative_urls) + + def convert(self, s): + """ + Returns a string copy of the input s that has the smiley codes replaced + with Markdown for smiley images. + """ + if not s: + return u'' + + for regex, repl in self.regexes: + s = regex.sub(repl, s) + return s + + +def smilify_html(value, autoescape=False): + """ + A convenience function to "smilify" text by replacing text with HTML + img tags of smilies. + """ + s = SmilifyHtml() + return s.convert(value, autoescape=autoescape)