Exemple #1
0
    def render_document(self, path, cache=True):
        if cache:
            return self.document_render_cache.render(path)

        context = {}
        context['content'] = self.render(path)
        context['title'] = self.title(path)
        context['crumbs'] = self.crumbs(path)
        context['active_nav'] = self.active_nav(path)
        context['make_relative'] = lambda href: make_relative(path, href)

        template = self.config.template_env.get_template('document.html')
        return template.render(context)
Exemple #2
0
    def error(self, request, status):

        """
        Serve a page for a given HTTP error.

        This works by rendering a template based on the HTTP error code; so an
        error of '404 Not Found' will render the '404.html' template. The
        context passed to the template is as follows:

        `request`
        : The `webob.Request` object for this HTTP request.

        `is_index`
        : A boolean indicating whether or not this is the index page. This may
        be useful in error pages where you want to link back to the home page;
        such a link will be useless in the index.

        `status`
        : An integer representing the HTTP status code of this error.

        `reason`
        : A string of the HTTP status 'reason', such as 'Not Found' for 404.

        The template is assumed to be valid XHTML.

        Note that the templating machinery is only invoked when the browser is
        expecting HTML. This is determined by calling
        `request.accept.accept_html()`. If not, an empty response (i.e. one
        without a content body) is returned.
        """

        response = webob.Response()
        response.status = status

        if request.accept.accept_html():
            context = {}
            context['request'] = request
            context['is_index'] = request.path_info in ['/', '/index.html']
            context['make_relative'] = lambda href: make_relative(request.path_info, href)
            context['status'] = status
            context['reason'] = webob.util.status_reasons[status]

            template = self.config.template_env.get_template('%d.html' % status)
            response.unicode_body = template.render(context)
            response.content_type = mimetypes.types_map['.html']
        else:
            del response.content_length
            del response.content_type

        return response
Exemple #3
0
    def render_listing(self, path):
        import jinja2

        context = self.listing_context(path)

        crumbs = [('index', '/')]
        if path not in ['', '/']:
            current_dir = ''
            for component in path.strip('/').split('/'):
                crumbs.append((component, '%s/%s/' % (current_dir, component)))
                current_dir += '/' + component
        crumbs.append((jinja2.Markup('<span class="list-crumb">list</span>'), None))

        context['crumbs'] = crumbs
        context['make_relative'] = lambda href: make_relative(path + '/', href)

        template = self.config.template_env.get_template('listing.html')
        return template.render(context)
Exemple #4
0
    def listing_context(self, directory):

        """
        Generate the template context for a directory listing.

        This method accepts a relative path, with the base assumed to be the
        HTML root. This means listings must be generated after the wiki is
        built, allowing them to list static media too.

        Directories should always be '/'-delimited when specified, since it is
        assumed that they are URL paths, not filesystem paths.

        For information on what the produced context will look like, consult the
        `listing` doctest.
        """

        # Ensure the directory name ends with '/'.
        directory = directory.strip('/')

        # Resolve to filesystem paths.
        fs_rel_dir = p.sep.join(directory.split('/'))
        fs_abs_dir = p.join(self.config.html_dir, fs_rel_dir)
        skip_files = set([self.config['listing-filename'], 'index.html'])

        sub_directories, pages, files = [], [], []
        for basename in os.listdir(fs_abs_dir):
            fs_abs_path = p.join(fs_abs_dir, basename)
            file_dict = {
                'basename': basename,
                'href': directory + '/' + basename}
            if not file_dict['href'].startswith('/'):
                file_dict['href'] = '/' + file_dict['href']

            if p.isdir(fs_abs_path):
                file_dict['href'] += '/'
                sub_directories.append(file_dict)

            else:
                if(basename in skip_files or basename.startswith('.') or
                   basename.startswith('_')):
                    continue

                file_dict['slug'] = p.splitext(basename)[0]
                file_dict['size'] = p.getsize(fs_abs_path)
                file_dict['humansize'] = humansize(file_dict['size'])

                if p.splitext(basename)[1] == (p.extsep + 'html'):
                    # Get the title from the file.
                    contents = read_from(fs_abs_path)
                    file_dict['title'] = get_title(file_dict['slug'], contents)
                    # Remove .html from the end of the href.
                    file_dict['href'] = p.splitext(file_dict['href'])[0]
                    pages.append(file_dict)
                else:
                    files.append(file_dict)

        sub_directories.sort(key=lambda directory: directory['basename'])
        pages.sort(key=lambda page: page['title'])
        files.sort(key=lambda file_: file_['basename'])

        return {
            'directory': directory,
            'sub_directories': sub_directories,
            'pages': pages,
            'files': files,
            'make_relative': lambda href: make_relative(directory, href),
        }