Metadata (microcms-plugin)

During the scanning phase, microcms will invoke the consume function from this plugin on every file that it scans. The consume function adds a dict object to the cfile passed to it, containing the names (as keys) and contents (as values) of all <meta> elements in the file's header section where the name begins with "microcms". This information is then available to be used by other plugins.

The following <meta> tags are used by other plugins (for example the catalogue and sitemap plugins.

Code for the metadata plugin is shown below:

ametadata.py
# ametadata.py - microcms plugin for collecting metadata from HTML pages 
# Copyright (c) 2009 Niall McCarroll  
# Distributed under the MIT/X11 License (http://www.mccarroll.net/snippets/license.txt)

import sys
import os
import os.path

import StringIO
from xml.dom import minidom
from datetime import datetime
from time import strptime

def consume(cfile,contents):
    metadata = parsemeta(contents)
    if metadata != None:
        cfile.metadata = metadata    
        cfile.metadata["path"] = cfile.outpath

def parsemeta(contents):
    f = StringIO.StringIO(contents)
    dom = minidom.parse(f)
    meta = {}
    for node in dom.getElementsByTagName("title"):
        meta['microcms-title'] = node.firstChild.data
        meta['title'] = node.firstChild.data
    for node in dom.getElementsByTagName("meta"):
        metaname = node.getAttribute("name")
        if metaname == 'description':
            meta['microcms-description'] = node.getAttribute("content")
        if metaname.startswith("microcms"):
            meta[metaname] = node.getAttribute("content")
            if metaname == "microcms-tags":
                meta[metaname] = meta[metaname].split(",")
            elif metaname == 'microcms-date':
                meta[metaname] = datetime(*strptime(meta[metaname], "%Y-%m-%dT%H:%M:%S")[0:6])
    
    return meta
    

 

Leave a comment

Anti-Spam Check
Comment