Login

Media Wiki With mwlib

Author:
nosrednakram
Posted:
January 5, 2009
Language:
Python
Version:
1.0
Score:
0 (after 0 ratings)

Simple wiki with MediaWiki and Markdown Support

Once you install the mwlib you can use mwit to convert Mediawiki markup to HTML. I am include the model that uses it to hopefully provide a good example. I maintain a version and only one copy of each wiki entry in the main table and archive replaced markup into another table, you will need to create the archive model or remove that section of code. The line ending changes in mwit are so that it will work with IE.

 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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())

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 4 weeks ago

Comments

Please login first before commenting.