summaryrefslogtreecommitdiffstats
path: root/wiki/templatetags/wikitags.py
diff options
context:
space:
mode:
authoreliott <eliott@cactuswax.net>2007-11-03 08:45:10 +0100
committereliott <eliott@cactuswax.net>2007-11-03 08:45:10 +0100
commit39a548fd2629f3b6383990264b2e331b3aea99fb (patch)
treef68c3156dad5f7814473ceff2461679ddf11a2e8 /wiki/templatetags/wikitags.py
downloadarchweb-39a548fd2629f3b6383990264b2e331b3aea99fb.tar.gz
archweb-39a548fd2629f3b6383990264b2e331b3aea99fb.tar.xz
Initial import for public release...
Special Note Prior to git import, approx 90% of the code was done by Judd Vinet. Thanks Judd!
Diffstat (limited to 'wiki/templatetags/wikitags.py')
-rw-r--r--wiki/templatetags/wikitags.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/wiki/templatetags/wikitags.py b/wiki/templatetags/wikitags.py
new file mode 100644
index 0000000..bdf8df6
--- /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("%%", "<br />", lines[i])
+ # Internal Links
+ lines[i] = re.sub("\(\(([A-z0-9 :/-]+)\)\)", "<a href=\"/wiki/\\1\">\\1</a>", lines[i])
+ # Small Text
+ lines[i] = re.sub("----([^----]+)----", "<span style=\"font-size:x-small\">\\1</span>", lines[i])
+ lines[i] = re.sub("--([^--]+)--", "<span style=\"font-size:small\">\\1</span>", lines[i])
+ # TT text
+ lines[i] = re.sub("\{\{([^}\}]+)\}\}", "<tt>\\1</tt>", 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 = "<td colspan=\"%d\">" % (colspan)
+ first = False
+ elif count == 0:
+ repl = "</td>"
+ else:
+ repl = "</td><td colspan=\"%d\">" % (colspan)
+ lines[i] = re.sub("(\|\|+)", repl, lines[i], 1)
+ # find the next chunk
+ m2 = re.search("(\|\|+)", lines[i])
+ lines[i] = "<tr>" + lines[i] + "</tr>"
+ if not in_table:
+ lines[i] = "<table>" + lines[i]
+ in_table = True
+ elif in_table:
+ lines[i] = "</table>" + lines[i]
+ in_table = False
+ # close leftover table, if open
+ if in_table:
+ lines[len(lines)] = lines[len(lines)] + "</table>"
+ return lines
+
+@register.filter
+def wikify(value):
+ md = markdown.Markdown(value)
+ md.preprocessors.insert(0, WikiProcessor())
+ html = md.toString()
+ return html