import template import plugin import string import os import locale from datetime import datetime,timedelta import jinja2 import json class CalendarPlugin: def __init__(self): calpath = os.path.join(template.path, 'calendar.json') with open(calpath, 'r') as calfile: events = json.load(calfile) for event in events: event['start'] = datetime.strptime(event['start'], '%Y-%m-%dT%H:%M:%S') event['end'] = datetime.strptime(event['end'], '%Y-%m-%dT%H:%M:%S') event['multiday'] = (event['end'] - event['start']) > timedelta(days=1) self.events = sorted(events, key=lambda x:x['start']) template_loader = jinja2.FileSystemLoader(searchpath=os.path.join(template.path)) self.template_env = jinja2.Environment(loader=template_loader) locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8') def _get_calendar(self): return self.template_env.get_template('calendar.j2').render({ 'events': self.events, 'class': 'calendar-main' }).encode('utf-8') def _get_calendar_head(self): return self.template_env.get_template('calendar_head.j2').render({ 'events': self.events, 'class': 'calendar-head' }).encode('utf-8') def process_content(self, page_content): return_value = {} for filename, content in page_content.iteritems(): t = string.Template(content) return_value[filename] = t.safe_substitute( calendar = self._get_calendar(), calendar_head = self._get_calendar_head() ) return return_value plugin.plugin_manager.register(CalendarPlugin())