"""
Show a list of recent blog post titles.
To use, install pyrecentposts.py in the plugins directory and
optionally customize its display behavior. To display the recent posts
list, include the variable $recentchanges in your template somewhere.
You can customize this module's behavior using the following
configuration options:
recentposts_start
string to output at the start of the recent posts list (default: '
')
recentposts_end
string to output at the end of the recent posts list (default: '
')
recentposts_item
format string to apply to each item in list. The following
parameters are avavailable in the format string:
%(url)s
The fully-qualified URL of the post
%(title)s
The title of the post
The default is '%(title)s;
recentposts_limit
maximum number of posts to display (default 10)
This code is licensed under the terms of the GNU General Public
License, version 2, available at
http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
Copyright 2008 Phillip Groce
"""
__author__ = "Phil Groce - pgroce at gmail dot com"
__version__ = "0.1"
__url__ = "http://philgroce.com/"
__description__ = "Show recent posts"
from Pyblosxom import tools
import os
from time import time, localtime
DEFAULT_START = r''
DEFAULT_ITEM = r'%(title)s'
# Default is last ten
DEFAULT_LIMIT = 10
def gen_recentposts(req):
log = tools.getLogger()
try:
config = req.getConfiguration()
root = config["datadir"]
def to_url(ename):
# trim root directory from entry name
trimmed = ename[len(root)+1:]
# trim possible extension, which pyblosxom will interpret
# as a flavour
trimmed = os.path.splitext(trimmed)[0]
return "%s/%s" % (config.get("base_url", ""), trimmed)
limit = config.get("recentposts_limit", DEFAULT_LIMIT)
start = config.get("recentposts_start", DEFAULT_START)
end = config.get("recentposts_end", DEFAULT_END)
item = config.get("recentposts_item", DEFAULT_ITEM)
# sort entries by mtime and clip off most recent limit entries
# (or whole list if limit > len(elist))
def cmp_mtime(a, b):
return cmp(tools.filestat(req, a), tools.filestat(req, b))
elist = sorted(tools.Walk(req, root), cmp_mtime)[(limit * -1):]
# reverse for most-recent-first order
elist.reverse()
# parse titles and map them to entry file name
items = []
for ename in elist:
f = open(ename, 'r')
# grab title (i.e., first line of post)
try:
l = f.readline().rstrip()
finally:
f.close()
d = { 'url': to_url(ename),
'title': l}
items.append(item % d)
return start + "%s" % '\n'.join(items) + end
except Exception, e:
log.exception(e)
## Pyblosxom hardware #######################################################
def verify_installation(request):
config = request.getConfiguration()
return 1
def cb_prepare(args):
req = args["request"]
data = req.getData()
data["recentposts"] = gen_recentposts(req)
if __name__ == '__main__':
output = {}
class dumb_req(object):
def getData(self):
return output
cb_prepare({'request' : dumb_req()})
print output