from django import template from django.template.loader import get_template import re register = template.Library() @register.filter(name='wiki') def wiki(text): """Replaces [[...]] with the actual content""" class WikiElement: """Represents a [[Foo:Bar|baz=true]] construct""" def __init__(self, element): self.element = element self._opts = self.element[1].split('|') @property def filename(self): return "wiki/wiki_%s.html" % self.element[0].lower() def __str__(self): return self.element[0] def value(self): return self._opts[0] def opts(self): # transform "a=1 b=2" to {'a':'1', 'b':'2'} try: i = iter(re.split(r"([^ =]+)=", self._opts[1])[1:]) return dict(zip(i, i)) except: return dict() def render_wiki_template(element): e = WikiElement(element) try: t = get_template(e.filename) except template.TemplateDoesNotExist: # Only for debugging purposes. On productions sites just "pass" t = template.Template('