WIKI_FORMATS = ( ('MD', 'Markdown'), ('MW', 'Media Wiki')) class WikiPage(models.Model): name = models.CharField(max_length = 128) slug = models.SlugField(unique=True, editable=False) version = models.IntegerField(editable=False, default=0) format = models.CharField(max_length=2, choices=WIKI_FORMATS) area = models.ForeignKey(WikiArea) markup = models.TextField() html = models.TextField(editable=False, blank=True) modified = models.DateTimeField( editable=False, default=datetime.datetime.now) def save(self): from django.template.defaultfilters import slugify from development.wiki.forms import WikiTextArchiveForm from django import forms if self.version is None: self.version = 1 else: self.version = self.version + 1 self.slug = "%s_%s" % (self.area.slug, slugify(self.name)) if self.format == 'MW': from development.wiki.utils import mwit self.html = mwit(self.slug, self.markup) elif self.format == 'MD': from markdown import markdown self.html = markdown(self.markup) self.modified = datetime.datetime.now() super(WikiPage, self).save() archive = {'page': self.id, 'version': self.version, 'format': self.format, 'markup': self.markup, 'modified': self.modified } form = WikiTextArchiveForm(archive) form.save() def mwit(title, input): from mwlib.uparser import parseString from mwlib.htmlwriter import HTMLWriter from mwlib.dummydb import DummyDB from django.utils.encoding import smart_unicode db = DummyDB() import StringIO out = StringIO.StringIO() if input.endswith(chr(13)+chr(10)): input = input.replace(chr(13)+chr(10),chr(10)) if input.endswith(chr(13)): input = input.replace(chr(13),chr(10)) try: p = parseString(title, input, db) except: return u'Unable to parse input!' try: w = HTMLWriter(out) except: return u'Unable call HTMLWriter!' try: w.write(p) except: return u'Unable to write input!' return unicode(out.getvalue())