Source

superwiki / superwiki / markup.py

Full commit
# -*- coding: utf-8 -*-

import re
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.utils.html import urlize
from common import url_getter, URL_R
from soup import html_links, strip_script_tags


def markdown_processor(wiki_page):
    import markdown
    wiki_page.rendered_body = markdown.markdown(wiki_page.rendered_body)


wikilink_re = re.compile('\[wiki:(%s)\s*([^\]]*)\]' % URL_R, re.UNICODE)

def wikilinks_processor(wiki_page):
    from views import get_wiki_page
    bind = wiki_page.bind_to
    linked_pages = []
    def repl(m):
        url, title = m.group(1, 2)
        page_url = url_getter(bind, url)
        exists = False
        try:
            page = get_wiki_page(bind, url=url)
            exists = True
            if page not in linked_pages:
                linked_pages.append(page)
        except ObjectDoesNotExist:
            pass
        if exists:
            link = '<a href="%s">%s</a>' % (page_url, title or url)
        else:
            link = '<a class="missing" href="%s">%s</a>' % (page_url, title or url)
        return link
    
    wiki_page.rendered_body = wikilink_re.sub(repl, wiki_page.rendered_body)
    
    # TODO should use delayed save if has no pk
    if wiki_page.pk is not None:
        wiki_page.linked_pages = linked_pages


def autolinks_processor(wiki_page):
    wiki_page.body = html_links(wiki_page.body)
    wiki_page.rendered_body = html_links(wiki_page.rendered_body)


def noscript_processor(wiki_page):
    wiki_page.body = strip_script_tags(wiki_page.body)
    wiki_page.rendered_body = strip_script_tags(wiki_page.rendered_body)