File: wiki.py

package info (click to toggle)
python-wikkid 0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 728 kB
  • sloc: python: 3,051; makefile: 12
file content (60 lines) | stat: -rw-r--r-- 1,951 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-
#
# Copyright (C) 2010 Wikkid Developers
#
# This software is licensed under the GNU Affero General Public License
# version 3 (see the file LICENSE).

"""View classes to control the rendering of wiki pages."""

from webob.exc import HTTPTemporaryRedirect

from wikkid.formatter.registry import get_wiki_formatter
from wikkid.interface.resource import IWikiTextFile
from wikkid.view.base import BaseView


def format_content(bytes, base_name, default_format):
    """ Format the content with the right formatter.

    Check the first line of the content to see if it specifies a
    formatter. The default is currently ReST, but we should have it
    configurable shortly.
    """
    content, formatter = get_wiki_formatter(bytes, default_format)
    return formatter.format(base_name, content)


class WikiPage(BaseView):
    """A wiki page is a page that is going to be rendered for viewing."""

    for_interface = IWikiTextFile
    name = 'view'
    is_default = True
    template = 'view_page'

    @property
    def content(self):
        bytes = self.context.get_bytes()
        try:
            text = bytes.decode('utf-8')
        except UnicodeDecodeError:
            try:
                text = bytes.decode('latin-1')
            except UnicodeDecodeError:
                text = bytes.decode('ascii', 'replace')

        default_format = self.execution_context.default_format
        return format_content(text, self.context.base_name, default_format)

    def _render(self, skin):
        """If the page is not being viewed with the preferred path, redirect.

        For example, /FrontPage.txt will redirect to /FrontPage
        """
        preferred = self.context.preferred_path
        if self.context.path != preferred:
            location = self.canonical_url(self.context)
            raise HTTPTemporaryRedirect(location=location)
        else:
            return super(WikiPage, self)._render(skin)