Wiki-like markup for sub templates

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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('<div style="color:red">(%s does not exist)</div>' % e.filename)
      return t.render(template.Context({'param': e}))

    r = re.compile(r"\[\[([A-Za-z\|]+):?([^\]]*)\]\]")
    return r.sub('%s', text) % \
          tuple([render_wiki_template(x) for x in r.findall(text)])

More like this

  1. Tags & filters for rendering search results by exogen 6 years ago
  2. "Partial Templates" - an alternative to "include" by vigrid 5 years, 2 months ago
  3. FieldsetForm by Ciantic 7 years ago
  4. Multiple-Submit-Button Widget for Choice Field by Archatas 5 years, 8 months ago
  5. YUI Loader as Django middleware by akaihola 5 years, 12 months ago

Comments

(Forgotten your password?)