Media Wiki With mwlib

 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. Markup Selection in Admin by jonathan 6 years, 8 months ago
  2. MarkupTextField by myles 5 years, 5 months ago
  3. Generic markup converter by ubernostrum 7 years, 1 month ago
  4. Wiki-like markup for sub templates by luckystarr 6 years, 7 months ago
  5. Safing HTML Text Input by PizzaPanther 5 years, 4 months ago

Comments

(Forgotten your password?)