Source

superwiki / superwiki / templatetags / wiki_extra.py

# -*- coding: utf-8 -*-

from django import template
from django.conf import settings
from superwiki.common import url_getter
from superwiki.views import filter_wiki_pages

register = template.Library()


@register.simple_tag
def wiki_reverse(bind, url):
    return url_getter(bind, url)


@register.inclusion_tag('wiki/tags/index.html')
def wiki_index(bind):
    pages = sorted(filter_wiki_pages(bind), key=lambda page: len(page.url))
    roots = []
    for page in pages:
        for possible_parent in reversed(pages):
            if possible_parent == page:
                continue
            if page.url.startswith(possible_parent.url):
                if len(page.url) > len(possible_parent.url):
                    if page.url[len(possible_parent.url)] != '/':
                        continue
                    if not hasattr(possible_parent, 'childnodes'):
                        setattr(possible_parent, 'childnodes', [])
                    getattr(possible_parent, 'childnodes').append(page)
                    break
        else:
            roots.append(page)
    return {'roots': roots, 'bind': bind}


@register.inclusion_tag('wiki/tags/index_tree_item.html')
def wiki_tree_item(page, bind):
    return {'page': page, 'bind': bind}