Customizing post_excerpt

49 views
Skip to first unread message

jaap

unread,
May 3, 2011, 9:31:02 AM5/3/11
to blogofile-discuss
Hi all,

I am not a (python) programmer but I succeeded quite well in migrating
my typepad blog to blogofile. Thank you for the nice work! There is
one thing I do not have a solution for yet: I want to customize the
excerpt processing.

It seems I have to add a custom function in _config.py
So I have added :

def post_excerpt(post,num_words=50):
and so on ..

But it does not get called.
Probably I am missing out on some little python magic here ?

Jaap

Ryan McGuire

unread,
May 3, 2011, 9:56:12 AM5/3/11
to blogofile-discuss
Sorry, this isn't documented well. I'll make another note to add this
to the new docs.

You define your post_excerpt function like you have, then you need to
assign it to be the default excerpt function:

def my_post_excerpt(post, num_words=50):
return "Derp derp"

blog.post_excerpt.method = my_post_excerpt

Then, if for instance you wanted to show excerpts on the chronological
listings, but still show the full post on the permalink pages, you
also need to modify your chronological.mako file to include
post_excerpt.mako rather than post.mako:

<%include file="post_excerpt.mako" args="post=post" />

jaap

unread,
May 3, 2011, 5:07:07 PM5/3/11
to blogofil...@googlegroups.com
Probably I do something wrong but it still does not work (the template thing is alright) :

== My _config.py contains now:

def my_post_excerpt(post,num_words=50):
    return "Some excerpt ..."
    
blog.post_excerpt.method = my_post_excerpt

== Then I force the function to be called (in post.py) :

    def __parse_post_excerpting(self):
        if bf.config.controllers.blog.post_excerpts.enabled:
            length = bf.config.controllers.blog.post_excerpts.word_length
#            try:
            self.excerpt = bf.config.post_excerpt(self.content, length)
#            except AttributeError:
#                self.excerpt = self.__excerpt(length)

== I get the following error :

  File "_controllers/blog/post.py", line 138, in __parse_post_excerpting
    self.excerpt = bf.config.post_excerpt(self.content, length)
AttributeError: 'module' object has no attribute 'post_excerpt'

Ryan McGuire

unread,
May 3, 2011, 11:08:02 PM5/3/11
to blogofil...@googlegroups.com
Whoops, I take it back, I gave you bad info.

The blog.post_excerpt.method is bogus, apparently I never implemented that, I was going off a comment I wrote in the master _config.py which was incorrect. (I don't personally do post excerpting in any of my sites, and apparently I have no unit tests for it, so shame on me).

The way you had it in the first place should be correct. Create a method in _config.py with the exact name "post_excerpt":

    def post_excerpt(content, length):
        return "something"

And then do what I said before about modifying chronological.mako to include post_excerpt.mako.

You shouldn't need to comment out anything in post.py.

This whole post_excerpt business is from a really old version of Blogofile (probably 0.5 ish) and so the way it's configured is different than how most other configuration is done (through bf.config.controllers.blog namespace rather than a naked function in _config.py). I will clean this up ASAP.


--
You received this message because you are subscribed to the Google Groups "blogofile-discuss" group.
To post to this group, send email to blogofil...@googlegroups.com.
To unsubscribe from this group, send email to blogofile-disc...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/blogofile-discuss?hl=en.

Jaap Noordzij

unread,
May 4, 2011, 8:50:48 AM5/4/11
to blogofil...@googlegroups.com
Thanks, It's OK now.

jaap

unread,
May 6, 2011, 3:39:27 PM5/6/11
to blogofil...@googlegroups.com
In fact I do not need a customized function at all since the YAML header is very flexible so I could provide a thumbnail filename and an excerpt in the YAML header.  

For this to work I would like to suggest a little added feature in your default __exerpt (in post.py) :

In the existing code you test for the presence of an excerpt but if it is there, you ignore it.
The only thing I need is that you return the existing excerpt if it is there:

def __excerpt (self, num_words=50):
    if len(self.excerpt) == 0:
        # your existing code
    else:
        return self.excerpt

If you add the last two lines I am perfectly happy. 

Ryan McGuire

unread,
May 6, 2011, 10:21:40 PM5/6/11
to blogofil...@googlegroups.com
Thanks jaap, I think you've got a good solution there.

I've reworked the post excerpting based on your suggestion for the blog plugin for 0.8: http://bit.ly/kvdwWg - this works how you've described: if a user defines their own excerpt attribute in the post YAML it will use that, if not it will look for a custom method defined at plugins.blog.post_excerpts.method (for real this time), and then finally it will use the default post excerpt method in post.py.

The old post excerpt method used BeautifulSoup, which I really don't enjoy working with anymore. The new one requires lxml, which is still a bit of a pain to install given that it's a c library, so post excerpting is turned off by default. you have to set "plugins.blog.post_excerpts.enabled = True" in _config.py to enable it. If you enable it and you don't have lxml installed, you'll get an appropriate error message. Defining your own post excerpt function is still supported, so lxml is not an absolute requirement.

--
Reply all
Reply to author
Forward
0 new messages