changeset 83:666f3051ce31

move cook_entry to its own function
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 17 Nov 2011 12:15:43 -0800
parents 9e5dd802ba95
children e5a23f5ea14e
files bitsyblog/bitsyblog.py bitsyblog/blog.py
diffstat 2 files changed, 11 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/bitsyblog/bitsyblog.py
+++ b/bitsyblog/bitsyblog.py
@@ -598,32 +598,35 @@ class BitsyBlog(object):
         users = blogs.keys()
 
         # display latest active user first
         users.sort(key=lambda user: blogs[user][0].date, reverse=True)
 
         data['blogs'] = blogs
         data['users'] = users
         data['date_formats'] = dict([(user, self.users[user].settings.get('Date format', self.date_format)) for user in users])
-        
+
         # render the template
         template = self.loader.load('index.html')
         return template.generate(**request.environ['data']).render()
 
-        
+    def cooked_entry(self, entry):
+        """cook the entry"""
+        if (entry.user, entry.datestamp()) not in self.cooked_bodies:
+           self.cooked_bodies[(entry.user, entry.datestamp())] = self.cooker(entry.body)
+        return self.cooked_bodies[(entry.user, entry.datestamp())]
+
     def write_blog(self, user, blog, path, n_links, request):
         """return the user's blog in HTML"""
 
-        # XXX probably should go elsewhere
+        # cook the entry
         for entry in blog:
-            if (user, entry.datestamp()) not in self.cooked_bodies:
-                self.cooked_bodies[(user, entry.datestamp())] = self.cooker(entry.body)
-            entry.cooked_body = Markup(self.cooked_bodies[(user, entry.datestamp())])
+            entry.cooked_body = Markup(self.cooked_entry(entry))
 
-        # site nav 
+        # site nav
         # XXX def site_nav() puts directly in request.environ['data']
         # should return instead
         self.site_nav(request)
 
         # user data -> should be moved up the chain
         data = request.environ['data']
         data['user'] = user
         data['role'] = self.role(user, request)
--- a/bitsyblog/blog.py
+++ b/bitsyblog/blog.py
@@ -12,33 +12,32 @@ class BlogEntry(object):
     def __init__(self, date, body, privacy, user):
         self.date = date
         self.body = body
         self.privacy = privacy
         if user is not None:
             self.user = user
 
     def title(self, characters=80):
-        
+
         if '\n' in self.body:
             lines = [i.strip() for i in self.body[:characters].split('\n')]
             if len(lines[0]) > characters:
                 retval = self.snippet(charachters)
             elif len(lines) > 1 and not lines[1]:
                 retval = lines[0]
             else:
                 retval = self.snippet(characters)
         else:
             retval = self.snippet(characters)
         return retval.decode('utf-8')
 
     def snippet(self, characters=80):
         if characters:
             if len(self.body) > characters:
-                
 
                 text = ' '.join(self.body[:characters].split()[:-1])
                 if '\n' in text:
                     lines = [ i.strip() for i in text.split('\n') ]
                     if '' in lines:
                         return '\n'.join(lines[:lines.index('')])
 
                 if text: