blog stats
seamusc.com

Django Wiki

Published on May 19, 2007

Update: I have expanded on this some more here

Update: Fixed a number of issues, should now be working

Update: I have noticed a lot of people coming here after searching google for "django wiki". If you decide to use this code as a basis of your own template tag please let me know just so I know if it actually works reliably.

I've started designing a wiki for django as I can't seem to find one that exists already. It shouldn't be all that hard to do. I've started by just creating some simple search and replace statements using python's regex module re foo/templatetags/wikify.py

from django import template
import re
import cgi
import string

register = template.Library()

# register the templatetag
@register.filter("wikify") 
def wikify(content):
    out = ''
    for rt in string.split(content,"\n"):
        rt = rt.replace('&', '&')
        rt = rt.replace('<', '&lt;')
        rt = rt.replace('>', '&gt;')
        rt = rt.replace('"', '&quot;')
        rt = rt.replace("'", '&#39;')
        # link  [[URL]] 
        try:
            rt = re.sub('\[\[([^\]|]*)\]\]','<a href="\\1">\\1</a>',rt)
        except:pass
        # link with title [[URL|TITLE]]
        try:
            rt = re.sub('\[\[([^\]|]*)\|([^\]]*)\]\]','<a href="\\1">\\2</a>',rt)
        except:pass
        # bold **TEXT**
        try: 
            rt = re.sub('\*\*([^\*]*)\*\*','<strong>\\1</strong>',rt)
        except:pass
        # italic //TEXT//
        try: 
            rt = re.sub('//([^\*]*)//','<strong>\\1</strong>',rt)
        except:pass
        # image {{IMG}}
        try: 
            rt = re.sub('\{\{([^\]|]*)\}\}','<img src="\\1" alt=""/>',rt)
        except:pass
        # h5 ==H5==
        try:
            rt = re.sub('^==([^=]*)==$','<h5>\\1</h5>',rt)
        except:pass
        # h4 ===H4===
        try:
            rt = re.sub('^===([^=]*)===$','<h4>\\1</h4>',rt)
        except:pass
        # h3  ====H3====
        try:
            rt = re.sub('^====([^=]*)====$','<h3>\\1</h3>',rt)
        except:pass
        # code [code]SOME CODE[/code]
        try:
            rt = re.sub('^\[code\]([^\]]*)\[/code\]$','<code>\\1</code>',rt)
        except:pass
        out += '<p>'+rt+'</p>'
    return out


templates/foo/bar.html

{% load wikify %}
{% block content|wikify %}
{% endblock %}

Comments

Mikko
November 25, 2007 at 10:44 p.m.

maybe you want to have a look here: http://www.djangosnippets.org/snippets/1...

h3
February 2, 2008 at 7:30 p.m.

markdown ? :D