Sitemap

A sitemap is a useful listing (usually nested to reflect the directory hierarchy) of all pages within a site.

The sitemap plugin supplies a process function which replaces the tag below with a site map.

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

The sitemap is based on Kim Siever's spruced up site maps.

Code for the sitemap plugin is shown below:

sitemap.py
import os.path
from tinplate.tinplate import tinplate

node_format = [ '<li><a href="$(href)">$(name)</a></li>\n' ]

relpath = ''

def format_node(d):
    t = tinplate()
    
    files = []
    directories = d.directories
    for f in d.files:
        fdetail = {}
        fdetail['name'] = f.metadata['microcms-title']
        fdetail['href'] = os.path.join(relpath,f.outpath)
        files.append(fdetail)

    result = ''
    for fdetail in files:
        result += t.process(node_format,[fdetail])
    if len(directories)>0:
        result += t.process(format,[directories])
    return result
		
root_format = [
    '<ul class="sitemap">\n',
    { 'foreach': [ format_node ] },
	"\n</ul>\n",
]

format = [
    "<ul>\n",
    { 'foreach': [ format_node ] },
	"\n</ul>\n",
]


class sitemapper:

    def __init__(self,rootdir,rpath):
        self.root = rootdir
        global relpath
        relpath = rpath
        
    def build(self):
        t = tinplate()
        return t.process(root_format,[[self.root]])
        
def process(cfile,attrs):
    rootdir = cfile.parentdir
    relpath = ""
    while rootdir.parentdir != None:
        rootdir = rootdir.parentdir
        relpath = os.path.join(relpath,"..")        
    s = sitemapper(rootdir,relpath)
    return s.build()

 

Leave a comment

Anti-Spam Check
Comment