From 39a548fd2629f3b6383990264b2e331b3aea99fb Mon Sep 17 00:00:00 2001 From: eliott Date: Sat, 3 Nov 2007 03:45:10 -0400 Subject: Initial import for public release... Special Note Prior to git import, approx 90% of the code was done by Judd Vinet. Thanks Judd! --- wiki/__init__.py | 0 wiki/models.py | 16 ++++++++++++ wiki/templatetags/__init__.py | 0 wiki/templatetags/wikitags.py | 57 ++++++++++++++++++++++++++++++++++++++++ wiki/views.py | 61 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 134 insertions(+) create mode 100644 wiki/__init__.py create mode 100644 wiki/models.py create mode 100644 wiki/templatetags/__init__.py create mode 100644 wiki/templatetags/wikitags.py create mode 100644 wiki/views.py (limited to 'wiki') diff --git a/wiki/__init__.py b/wiki/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/wiki/models.py b/wiki/models.py new file mode 100644 index 00000000..2b8b16fc --- /dev/null +++ b/wiki/models.py @@ -0,0 +1,16 @@ +from django.db import models +from django.contrib.auth.models import User + +class Wikipage(models.Model): + """Wiki page storage""" + title = models.CharField(maxlength=255) + content = models.TextField() + last_author = models.ForeignKey(User) + class Meta: + db_table = 'wikipages' + + def editurl(self): + return "/wiki/edit/" + self.title + "/" + + def __repr__(self): + return self.title diff --git a/wiki/templatetags/__init__.py b/wiki/templatetags/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/wiki/templatetags/wikitags.py b/wiki/templatetags/wikitags.py new file mode 100644 index 00000000..bdf8df61 --- /dev/null +++ b/wiki/templatetags/wikitags.py @@ -0,0 +1,57 @@ +from django.template import Library +from django.conf import settings +from archlinux.lib import markdown +import re + +register = Library() + +class WikiProcessor: + def run(self, lines): + in_table = False + for i in range(len(lines)): + # Linebreaks + lines[i] = re.sub("%%", "
", lines[i]) + # Internal Links + lines[i] = re.sub("\(\(([A-z0-9 :/-]+)\)\)", "\\1", lines[i]) + # Small Text + lines[i] = re.sub("----([^----]+)----", "\\1", lines[i]) + lines[i] = re.sub("--([^--]+)--", "\\1", lines[i]) + # TT text + lines[i] = re.sub("\{\{([^}\}]+)\}\}", "\\1", lines[i]) + # Tables + m = re.match("(\|\|)", lines[i]) + if m: + count = len(re.findall("(\|\|+)", lines[i])) + first = True + m2 = re.search("(\|\|+)", lines[i]) + while m2 and count: + count -= 1 + colspan = len(m2.group(1)) / 2 + if first: + repl = "" % (colspan) + first = False + elif count == 0: + repl = "" + else: + repl = "" % (colspan) + lines[i] = re.sub("(\|\|+)", repl, lines[i], 1) + # find the next chunk + m2 = re.search("(\|\|+)", lines[i]) + lines[i] = "" + lines[i] + "" + if not in_table: + lines[i] = "" + lines[i] + in_table = True + elif in_table: + lines[i] = "
" + lines[i] + in_table = False + # close leftover table, if open + if in_table: + lines[len(lines)] = lines[len(lines)] + "" + return lines + +@register.filter +def wikify(value): + md = markdown.Markdown(value) + md.preprocessors.insert(0, WikiProcessor()) + html = md.toString() + return html diff --git a/wiki/views.py b/wiki/views.py new file mode 100644 index 00000000..c78da37d --- /dev/null +++ b/wiki/views.py @@ -0,0 +1,61 @@ +# +# Based on code from http://e-scribe.com/news/210 +# +from django.http import HttpResponse, HttpResponseRedirect +from django.contrib.auth.decorators import login_required +from archlinux.utils import render_template +from archlinux.wiki.models import Wikipage + +@login_required +def index(request): + """Return a list of all wiki pages""" + pages = Wikipage.objects.all().order_by('title') + return render_template('wiki/home.html', request, {'pages':pages}) + +def main(request): + """Return the Index wiki page""" + return HttpResponseRedirect("/wiki/WikiIndex/") + +@login_required +def page(request, title): + """Display page, or redirect to root if page doesn't exist yet""" + try: + page = Wikipage.objects.get(title__exact=title) + return render_template('wiki/page.html', request, {'page':page}) + except Wikipage.DoesNotExist: + return HttpResponseRedirect("/wiki/edit/%s/" % title) + +@login_required +def edit(request, title): + """Process submitted page edits (POST) or display editing form (GET)""" + if request.POST: + try: + page = Wikipage.objects.get(title__exact=title) + except Wikipage.DoesNotExist: + # Must be a new one; let's create it + page = Wikipage(title=title) + page.content = request.POST['content'] + page.title = request.POST['title'] + page.last_author = request.user + page.save() + return HttpResponseRedirect("/wiki/" + page.title + "/") + else: + try: + page = Wikipage.objects.get(title__exact=title) + except Wikipage.DoesNotExist: + # create a dummy page object -- note that it is not saved! + page = Wikipage(title=title) + page.body = "" + return render_template('wiki/edit.html', request, {'page':page}) + +@login_required +def delete(request): + """Delete a page""" + if request.POST: + title = request.POST['title'] + try: + page = Wikipage.objects.get(title__exact=title) + except Wikipage.DoesNotExist: + return HttpResponseRedirect("/wiki/") + page.delete() + return HttpResponseRedirect("/wiki/") -- cgit v1.2.3-54-g00ecf