[PATCH] pyarchives_enhanced.py
Mario Fernández <[email protected]>
| Newsgroups | gmane.comp.web.pyblosxom.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi, I've been using the pyarchives_enhanced plugin, and while I found it pretty cool, I wanted to show a list of posts for each month entry, like blogger does. Here is the modified code to do this. This has been tested by me and by Klaus Trainer. The whole code is available under http://svn.hceris.com/pyblosxom Cheers, Mario ------------------------------------------------------------------------- Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://sourceforge.net/services/buy/index.php _______________________________________________ Pyblosxom-devel mailing list Pyblosxom-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org https://lists.sourceforge.net/lists/listinfo/pyblosxom-devel
pyarchives_enhanced.py.diff
(application/octet-stream, 7.8 KB)
--- old_pyarchives_enhanced.py 2008-06-14 18:39:29.000000000 -0700
+++ pyarchives_enhanced.py 2008-06-14 18:19:10.000000000 -0700
@@ -6,12 +6,14 @@
You can format the output by setting these variables in your config.py:
"archives_start",
"archive_template_year",
-"archive_template_month" and
+"archive_template_month",
+"archive_template_post" and
"archives_finish" .
"archives_start" starts the archive listing and is only used at the very
beginning. The "archive_template_year" and "archive_template_month" properties
-are the templates for each year or rather month item. Then, after all the
+are the templates for each year or rather month item. "archive_template_post"
+represents a single post. Then, after all the
archives are printed, "archives_finish" ends the archives listing.
A config.py example:
@@ -20,7 +22,9 @@
py['archive_template_year'] = '<div class="archiveYear"><a href="%(base_url)s/%(Y)s">%(Y)s</a> (%(c)s)</div>'
-py['archive_template_month'] = '<div><a href="%(base_url)s/%(Y)s/%(m)s">%(B)s %(Y)s</a> (%(c)s)</div>'
+py['archive_template_month'] = '<div class="archiveMonth"><a href="%(base_url)s/%(Y)s/%(m)s">%(B)s %(Y)s</a> (%(c)s)</div>'
+
+py['archive_template_post'] = '<div class="archivePost"><a href="%(base_url)s/%(post)s">%(title)s</a> </div>'
py['archives_finish'] = '</li></ul>'
@@ -40,6 +44,7 @@
.clicker
.archiveYear
.archiveMonth
+ .archivePost
CSS example:
#archives {
@@ -60,7 +65,11 @@
.archiveMonth {
text-indent: 2em;
}
-
+.archivePost {
+ text-indent: 2.5em;
+ font-size : 0.9em;
+ margin: 2px 0px;
+}
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
@@ -82,6 +91,7 @@
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+
Copyright 2004, 2005 Wari Wahab
"""
__author__ = "Wari Wahab - wari at wari dot per dot sg"
@@ -90,7 +100,7 @@
__description__ = "Creates yearly and monthly archives for PyBlosxom."
from Pyblosxom import tools
-import time, os
+import re, time, os, os.path
def verify_installation(request):
config = request.getConfiguration()
@@ -106,12 +116,22 @@
print "Missing optional config property 'archive_template_month' which"
print " allows you to specify how the archive links are created.\n"
print "Refer to pyarchive plugin documentation for more details."
+ if not config.has_key("archives_template_post"):
+ print "Missing optional config property 'archives_template_post' which"
+ print " allows you to specify how the archive links are created.\n"
+ print "Refer to pyarchive plugin documentation for more details."
if not config.has_key("archives_finish"):
print "Missing optional config property 'archives_finish' which "
print "allows you to specify how the archive links are created.\n"
print "Refer to pyarchive plugin documentation for more details."
return 1
+class Record:
+ def __init__(self, repr):
+ self.count = 0
+ self.archives = {}
+ self.repr = repr
+
class PyblArchives:
def __init__(self, request):
self._request = request
@@ -126,57 +146,86 @@
config = self._request.getConfiguration()
root = config["datadir"]
year_archives = {}
- month_archives = {}
archiveList = tools.Walk(self._request, root)
fulldict = {}
fulldict.update(config)
-
+
year_template = (config.get('archive_template_year',
'<div class="archiveYear"><a href="%(base_url)s/%(Y)s">%(Y)s</a> (%(c)s)</div>'))
month_template = (config.get('archive_template_month',
- '<div><a href="%(base_url)s/%(Y)s/%(m)s">%(B)s %(Y)s</a> (%(c)s)</div>'))
+ '<div class="archiveMonth"><a href="%(base_url)s/%(Y)s/%(m)s">%(B)s %(Y)s</a> (%(c)s)</div>'))
+ post_template = (config.get('archive_template_post',
+ '<div class="archivePost"><a href="%(base_url)s/%(post)s">%(title)s</a> </div>'))
- count = {} # here the number of an archive's entries is stored
for mem in archiveList:
timetuple = tools.filestat(self._request, mem)
timedict = {}
# "%(c)d" will be correctly replaced
# later by the number of archive entries...
timedict["c"] = "%(c)d"
- for x in ["B", "b", "m", "Y", "y"]:
+ for x in ["B", "b", "m", "Y", "y", "d", "H", "M", "S"]:
timedict[x] = time.strftime("%" + x, timetuple)
-
+ fulldict.update(timedict)
+
+ # Create year entry if needed, and update count
if not year_archives.has_key(timedict['Y']):
- fulldict.update(timedict)
- year_archives[timedict['Y']] = (year_template % fulldict)
- count[timedict['Y']] = 1
- else:
- count[timedict['Y']] += 1
-
- if not month_archives.has_key(timedict['Y'] + timedict['m']):
- fulldict.update(timedict)
- month_archives[timedict['Y'] + timedict['m']] = (month_template
- % fulldict)
- count[timedict['Y'] + timedict['m']] = 1
- else:
- count[timedict['Y'] + timedict['m']] += 1
-
+ year_archives[timedict['Y']] = Record(year_template % fulldict)
+
+ year_entry = year_archives[timedict['Y']]
+ year_entry.count += 1
+
+ # Create month entry if needed, update count
+ month_archives = year_entry.archives
+
+ if not month_archives.has_key(timedict['m']):
+ month_archives[timedict['m']] = Record(month_template % fulldict)
+
+ month_entry = month_archives[timedict['m']]
+ month_entry.count += 1
+
+ # Save title of the post
+ post_archives = month_entry.archives
+ text = open(mem).readline()
+ title = re.match(".+", text).group()
+ post = os.path.basename(mem)
+ fulldict.update({ 'title' : title, 'post' : post})
+ post_archives[timedict['d'] + timedict['H'] + timedict['M'] + timedict['S']] = post_template % fulldict
+
year_keys = year_archives.keys()
year_keys.sort()
year_keys.reverse()
- month_keys = month_archives.keys()
- month_keys.sort()
- month_keys.reverse()
- result = []
+
+ # Generate output
result = [ config.get('archives_start', '<ul id="archives"><li>') ]
for year_key in year_keys:
- result.append(year_archives[year_key] % {"c": count[year_key]})
- result.append('<div class="archiveMonth">')
+
+ year_entry = year_archives[year_key]
+
+ result.append(year_entry.repr % {"c": year_entry.count})
+ result.append('<div>')
+
+ month_keys = year_entry.archives.keys()
+ month_keys.sort()
+ month_keys.reverse()
+
for month_key in month_keys:
- if year_key == month_key[:len(year_key)]:
- result.append(month_archives[month_key]
- % {"c": count[month_key]})
+
+ month_entry = year_entry.archives[month_key]
+ result.append(month_entry.repr % {"c": month_entry.count})
+ result.append('<div>')
+
+ post_keys = month_entry.archives.keys()
+ post_keys.sort()
+ post_keys.reverse()
+
+ for post_key in post_keys:
+
+ post_entry = month_entry.archives[post_key]
+ result.append(post_entry)
+
+ result.append('</div>')
result.append('</div>')
+
result.append(config.get('archives_finish', '</li></ul>'))
self._archives = '\n'.join(result)