#! /usr/bin/env python #! -*- coding:utf-8 -*- import re import simplejson """ See METABOOK.txt for description of Metabook data """ class MetaBook(object): """Encapsulate meta information about an article collection""" title = u"" subtitle = u"" def __init__(self): self.type = 'collection' self.version = 1 self.items = [] def addArticles(self, articleTitles, chapterTitle=None, contentType='text/x-wiki'): """ @param articleTitles: sequence of article titles or dicts containing article title (value for key 'title') and optionally display title (value for key 'displaytitle'). @type articleTitles: [unicode|{str: unicode}] """ articles = [] for title in articleTitles: article = { 'type': 'article', 'content-type': contentType, } if isinstance(title, dict): article.update(title) else: article['title'] = title articles.append(article) if chapterTitle: self.items.append({ 'type': 'chapter', 'title': chapterTitle, 'items': articles, }) else: self.items.extend(articles) def dumpJson(self): return simplejson.dumps(vars(self)) def loadJson(self, jsonStr): for (var, value) in simplejson.loads(jsonStr).items(): setattr(self, var, value) def readJsonFile(self, filename): self.loadJson(open(filename, 'rb').read()) def loadCollectionPage(self, mwcollection): """Parse wikitext of a MediaWiki collection page @param mwcollection: wikitext of a MediaWiki collection page as created by the Collection extension for MediaWiki @type mwcollection: unicode """ titleRe = '^==\s+(?P