Catalogue

A catalogue in a parent page consists of a set of links to child pages (pages directly under the parent page in the site hierarchy). Because it is compiled automatically, there is no need to explicitly add links to a parent page when a new child page is added to the site. Microcms uses a tinplate template to render the list, using metadata on each child page collected by the metadata plugin.

<microcms function="catalogue" path="..." />

Microcms replaces this <microcms> tag with a catalogue generated using a tinplate template loaded from the file identified by the path attribute (example), applied to a list of catalogue objects collected by microcms from scanning files in the immediate sub-directories of the directory containing this file.

Code for the catalogue plugin is shown below:

catalogue.py
# catalogue.py - microcms plugin for adding a catalogue to an HTML page
# (a catalogue is a table linking to HTML files in immediate sub-directories)
# Copyright (c) 2009 Niall McCarroll  
# Distributed under the MIT/X11 License (http://www.mccarroll.net/snippets/license.txt)

import os.path
from tinplate.tinplate import tinplate

from datetime import datetime
from time import strptime

import copy

def stringizer(o):
    if isinstance(o,datetime):
        return o.strftime("%b %Y") # eg Jan 2008
    else:
        return str(o)

def datecompare(c1,c2):
    d1 = d2 = datetime(1970,1,1)
    if 'microcms-date' in c1:
        d1 = c1['microcms-date']
    if 'microcms-date' in c2:
        d2 = c2['microcms-date']
    if d1 == d2:
	    return 0
    if d2 < d1:
	    return -1
    return 1

def process(cfile,attrs):
    npath = attrs["path"]
    template = open(os.path.join(os.path.split(cfile.path)[0],npath),"r").read()
    t = tinplate()
    catalogue = []
    rootpath = ""
    for x in xrange(0,cfile.parentdir.depth()):
        rootpath = os.path.join("..",rootpath)
    for d in cfile.parentdir.directories:
        for f in d.files:
            metacopy = copy.deepcopy(f.metadata)
            metacopy["path"]=os.path.join(rootpath,f.metadata["path"])
            tagstring = ""
            if 'microcms-hidden' in metacopy:
                continue
            if 'microcms-tags' in metacopy:
                tagstring = reduce(lambda a,b: a + ", " + b,metacopy['microcms-tags'])
            metacopy["tags"] = tagstring
            catalogue.append(metacopy)
    catalogue.sort(datecompare)
    return t.process(eval(template),[catalogue],stringizer)  
               

 

Leave a comment

Anti-Spam Check
Comment