changeset 792:7429c98c8ece

Issue #71: use relative URLs for smileys on the web and absolute for RSS.
author Brian Neal <bgneal@gmail.com>
date Mon, 26 May 2014 14:59:55 -0500
parents 0ca691cccf8d
children fa7377d90160
files core/markup.py forums/latest.py sg101/templates/forums/post_rss.html smiley/__init__.py smiley/models.py
diffstat 5 files changed, 44 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/core/markup.py	Fri May 23 21:52:41 2014 -0500
+++ b/core/markup.py	Mon May 26 14:59:55 2014 -0500
@@ -8,11 +8,8 @@
 
 class Markdown(object):
     """
-    This is a thin wrapper around the Markdown class which deals with the
-    differences in Markdown versions on the production and development server.
-    This code was inspired by the code in
-    django/contrib/markup/templatetags/markup.py.
-    Currently, we only have to worry about Markdown 1.6b and 2.0.
+    This is a thin wrapper around the Markdown class.
+
     """
     def __init__(self, safe_mode='escape'):
         self.md = _markdown.Markdown(safe_mode=safe_mode,
@@ -34,18 +31,22 @@
     """
     This class provides site markup by combining markdown and
     our own smiley markup.
+
+    The relative_urls parameter controls whether the smileys are generated with
+    relative or absolute URLs.
+
     """
-    def __init__(self):
+    def __init__(self, relative_urls=True):
         self.md = Markdown()
-        self.smiley = SmilifyMarkdown()
+        self.smiley = SmilifyMarkdown(relative_urls=relative_urls)
 
     def convert(self, s):
         return self.md.convert(self.smiley.convert(s))
 
 
-def site_markup(s):
+def site_markup(s, relative_urls=True):
     """
     Convenience function for one-off site markup jobs.
     """
-    sm = SiteMarkup()
+    sm = SiteMarkup(relative_urls=relative_urls)
     return sm.convert(s)
--- a/forums/latest.py	Fri May 23 21:52:41 2014 -0500
+++ b/forums/latest.py	Mon May 26 14:59:55 2014 -0500
@@ -64,6 +64,7 @@
 from forums.views.subscriptions import notify_topic_subscribers
 from forums.tools import auto_favorite, auto_subscribe
 from core.services import get_redis_connection
+from core.markup import site_markup
 
 # This constant controls how many latest posts per forum we store
 MAX_POSTS = 50
@@ -453,16 +454,17 @@
     """Serialize a post to JSON and return it.
 
     """
+    # Use absolute URLs for smileys for RSS. This means we have to reconvert the
+    # post Markdown to HTML.
+    content = site_markup(post.body, relative_urls=False)
+
     # get any attachments for the post
-
     attachments = Attachment.objects.filter(post=post).select_related(
             'embed').order_by('order')
     embeds = [item.embed for item in attachments]
-    if len(embeds) == 0:
-        content = post.html
-    else:
+    if len(embeds):
         content = render_to_string('forums/post_rss.html', {
-            'post': post,
+            'content': content,
             'embeds': embeds,
         })
 
--- a/sg101/templates/forums/post_rss.html	Fri May 23 21:52:41 2014 -0500
+++ b/sg101/templates/forums/post_rss.html	Mon May 26 14:59:55 2014 -0500
@@ -1,4 +1,4 @@
-{{ post.html|safe }}
+{{ content|safe }}
 <hr />
 <p>Video attachments:</p>
 <ol>
--- a/smiley/__init__.py	Fri May 23 21:52:41 2014 -0500
+++ b/smiley/__init__.py	Mon May 26 14:59:55 2014 -0500
@@ -43,8 +43,9 @@
     A class to "smilify" text by replacing text with Markdown image syntax for
     smiley images.
     """
-    def __init__(self):
-        self.regexes = Smiley.objects.get_smiley_regexes()
+    def __init__(self, relative_urls=True):
+        self.regexes = Smiley.objects.get_smiley_regexes(
+                                        relative_urls=relative_urls)
 
     def convert(self, s):
         """
--- a/smiley/models.py	Fri May 23 21:52:41 2014 -0500
+++ b/smiley/models.py	Mon May 26 14:59:55 2014 -0500
@@ -39,19 +39,24 @@
         cache.set(key, smilies, CACHE_TIMEOUT)
         return smilies
 
-    def get_smiley_regexes(self):
+    def get_smiley_regexes(self, relative_urls=True):
         """
         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.
+
+        If relative_urls is true, the smiley images will use relative URLs. If
+        False, absolute URLs will be used.
+
         """
-        regexes = cache.get('smiley_regexes')
+        key = 'smiley_regexes_rel' if relative_urls else 'smiley_regexes_abs'
+        regexes = cache.get(key)
         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)
+            r"\1%s\2" % s.markdown(relative_urls=relative_urls)) for s in self.all()]
+        cache.set(key, regexes, CACHE_TIMEOUT)
         return regexes
 
 
@@ -82,10 +87,19 @@
         return u''
     html.allow_tags = True
 
-    def markdown(self):
-        """Returns a markdown representation of the smiley."""
+    def markdown(self, relative_urls=True):
+        """Returns a markdown representation of the smiley.
+
+        If relative_urls is True, relative URLs will be generated. If False,
+        absolute URLs will be used.
+
+        """
         if self.image:
-            site = Site.objects.get_current()
-            return (u'![%s](http://%s%s "%s")' % 
-                    (self.title, site.domain, self.get_absolute_url(), self.title))
+            if relative_urls:
+                return u'![%s](%s "%s")' % (self.title, self.image.url,
+                        self.title)
+            else:
+                site = Site.objects.get_current()
+                return (u'![%s](http://%s%s "%s")' %
+                    (self.title, site.domain, self.image.url, self.title))
         return u''