changeset 89:7f7f7313b4c4

add html to rss feeds
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 17 Nov 2011 16:06:23 -0800
parents a3a7ac9102dc
children d29100da202a
files bitsyblog/bitsyblog.py setup.py
diffstat 2 files changed, 19 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/bitsyblog/bitsyblog.py
+++ b/bitsyblog/bitsyblog.py
@@ -48,17 +48,18 @@ class BitsyBlog(object):
                  'subject': '[ %(date)s ]:', # default subject
                  'n_links': 5, # number of links for navigation
                  'site_name': 'bitsyblog', # name of the site (needed?)
                  'header': None, # text to insert as first child of body'
                  'template_directories': '', # space separated template_directories
                  'auto_reload': True, # reload the genshi templates
                  'help_file': None, # help to display
                  'feed_items': 10, # number of RSS/atom items to display
-                 'post_handlers': '' # post handlers
+                 'post_handlers': '', # post handlers
+                 'rss_html': True, # whether to render rss descriptions as HTML
                  }
 
     cooked_bodies = {}
 
     def __init__(self, kw, handler_args):
 
         # set values from defaults and kw
         for key, value in self.defaults.items():
@@ -68,29 +69,26 @@ class BitsyBlog(object):
             _type = type(value)
             if isinstance(kw_value, basestring) and not issubclass(_type, basestring) and value is not None:
                 if _type == bool:
                     kw_value = kw_value.lower() == 'true'
                 else:
                     kw_value = _type(kw_value)
             setattr(self, key, kw_value)
 
-        self.n_links = int(self.n_links) # could be a string from the .ini
         self.response_functions = {'GET': self.get,
                                    'POST': self.post,
                                    'PUT': self.put
                                    }
 
         # abstract attributes
         from user import FilespaceUsers
-
         self.users = FilespaceUsers(self.file_dir)
         self.blog = FileBlog(self.file_dir)
         self.cooker = self.restructuredText
-        self.feed_items = int(self.feed_items)
 
         # template renderer
         self.template_directories = self.template_directories.split() # no spaces in directory names, for now
 
         for directory in self.template_directories:
             assert os.path.isdir(directory), "Bitsyblog template directory %s does not exist!" % directory
 
         self.template_directories.append(resource_filename(__name__, 'templates'))
@@ -682,67 +680,74 @@ class BitsyBlog(object):
 #         try:
 #             foo = etree.fromstring(retval)
 #         except etree.XMLSyntaxError:
 #             return retval
 #         # should cleanup the <div class="system-message">
 #         for i in foo.getiterator():
 #             if dict(i.items()).get('class') ==  'system-message':
 #                 i.clear()
-                
 #         return etree.tostring(foo)
 
 
     ### feeds
 
     def site_rss(self, request, n_items=10):
         blog = self.blog.latest(list(self.users.users()), n_items)
         title = self.site_name + ' - rss'
-        link = request.application_url 
+        link = request.application_url
         description = "latest scribblings on %s" % self.site_name
         lastBuildDate = datetime.datetime.now()
         items = [ self.rss_item(request, entry.user, entry) for entry in blog ]
-        rss = PyRSS2Gen.RSS2(title=title, 
+        rss = PyRSS2Gen.RSS2(title=title,
                              link=link,
                              description=description,
                              lastBuildDate=lastBuildDate,
                              items=items)
         return rss.to_xml()
 
     def rss(self, request, user, blog):
         """
         rss feed for a user's blog
         done with PyRSS2Gen:
         http://www.dalkescientific.com/Python/PyRSS2Gen.html
-        """        
+        """
         title = "%s's blog" % user
         link = os.path.split(request.url)[0]
         description = "latest blog entries for %s on %s" % (user, self.site_name)
         lastBuildDate = datetime.datetime.now() # not sure what this means
-        
+
         items = [ self.rss_item(request, user, entry) for entry in blog ]
-        rss = PyRSS2Gen.RSS2(title=title, 
+        rss = PyRSS2Gen.RSS2(title=title,
                              link=link,
                              description=description,
                              lastBuildDate=lastBuildDate,
                              items=items)
         return rss.to_xml()
 
     def rss_item(self, request, user, entry):
         if hasattr(request, 'user') and request.user.name == user:
             prefs = request.user.settings
         else:
             prefs = self.users[user].settings
         subject = prefs.get('Subject', self.subject)
         date_format = prefs.get('Date format', self.date_format)
         title = entry.title()
         link = self.permalink(request, entry)
-        return PyRSS2Gen.RSSItem(title=title, 
+
+        # item description
+        if self.rss_html:
+            body = self.cooked_entry(entry)
+        else:
+            body = entry.body
+            body = unicode(body, errors='replace')
+
+        return PyRSS2Gen.RSSItem(title=title,
                                  link=link,
-                                 description=unicode(entry.body, errors='replace'),
+                                 description=body,
                                  author=user,
                                  guid=PyRSS2Gen.Guid(link),
                                  pubDate=entry.date)
 
 
     def atom(self, request, blog, author=None):
 
         # data for genshi template
@@ -750,17 +755,17 @@ class BitsyBlog(object):
         data = request.environ['data']
         data['blog'] = blog
         data['author'] = author
         data['date'] = date
 
         # render the template
         template = self.loader.load('atom.xml')
         return template.generate(**data).render()
-        
+
 
     ### forms
 
     def form_post(self, request, user):
 
         # genshi data
         self.site_nav(request)
         data = request.environ['data']
--- a/setup.py
+++ b/setup.py
@@ -1,12 +1,12 @@
 from setuptools import setup, find_packages
 import sys, os
 
-version = '2.4'
+version = '2.4.1'
 
 try:
     description = file('README.txt').read()
 except IOError:
     description = """
 Meet bitsyblog.  Posting is done with a POST request, so while you can use
 a web form to do this, its just as easy to use curl, urllib, or anything else
 to post.