Esempio n. 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['make_relative'] = lambda href: make_relative(path, href)
     
     template = self.config.template_env.get_template('document.html')
     return template.render(context)
Esempio n. 2
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['make_relative'] = lambda href: make_relative(path, href)

        template = self.config.template_env.get_template('document.html')
        return template.render(context)
Esempio n. 3
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["make_relative"] = lambda href: make_relative(path, href)

        template = self.config.template_env.get_template("document.html")
        return template.render(context)
Esempio n. 4
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['.xhtml']
        else:
            del response.content_length
            del response.content_type

        return response
Esempio n. 5
0
 def render_document(self, path, cache=True):
     if cache:
         return self.document_render_cache.render(path)
     
     context = self.meta(path)
     context['content'] = self.render(path)
     if not context.has_key('title'):
         context['title'] = self.title(path)
     context['crumbs'] = self.crumbs(path)
     context['make_relative'] = lambda href: make_relative(path, href)
     
     template = self.config.template_env.get_template('document.html', globals=self.globals)
     return template.render(context)
Esempio n. 6
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
Esempio n. 7
0
    def render_document(self, path, cache=True):
        if cache:
            return self.document_render_cache.render(path)

        context = self.meta(path)
        context['content'] = self.render(path)
        if not context.has_key('title'):
            context['title'] = self.title(path)
        context['crumbs'] = self.crumbs(path)
        context['make_relative'] = lambda href: make_relative(path, href)

        template = self.config.template_env.get_template('document.html',
                                                         globals=self.globals)
        return template.render(context)
Esempio n. 8
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)
Esempio n. 9
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)
Esempio n. 10
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)
Esempio n. 11
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['make_relative'] = lambda href: make_relative(path, href)

        template = 'document.html'
        # custom templates
        for item in self.config.get('custom-templates', []):
            if (any(re.match(regexp, path) for regexp in item['regexps'])):
                template = item['template']
                break
        
        template = self.config.template_env.get_template(template)
        return template.render(context)
Esempio n. 12
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['make_relative'] = lambda href: make_relative(path, href)

        template = 'document.html'
        # custom templates
        for item in self.config.get('custom-templates', []):
            if (any(re.match(regexp, path) for regexp in item['regexps'])):
                template = item['template']
                break

        template = self.config.template_env.get_template(template)
        return template.render(context)
Esempio n. 13
0
    def render_document(self, path, cache=True):
        if cache:
            return self.document_render_cache.render(path)

        doc = self.render(path)
        
        context = {}
        context['title'] = self.title(path)
        context['make_relative'] = lambda href: make_relative(self.config, path, href)

        new_page = get_redirect_page(doc)
        if new_page != '':
            # found redirect
            context['redirect'] = new_page
            
            template = self.config.template_env.get_template('redirect.html')
        else:
            context['content'] = doc
            context['crumbs'] = self.crumbs(path)
            
            template = self.config.template_env.get_template('document.html')
        return template.render(context)
Esempio n. 14
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),
     }
Esempio n. 15
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):
                if directory.startswith('.') or basename.startswith('.'):
                    continue
                elif directory == 'media' or directory.startswith(
                        'media') or basename == 'media':
                    continue
                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)
                    path = p.join(directory.strip('/'),
                                  basename.replace('.html', '.md'))
                    try:
                        file_dict.update(self.meta(path))
                    except:
                        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['date'], reverse=True)
        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),
        }
Esempio n. 16
0
 def make_relative(self, href):
     return make_relative(self.curr_path, href)
Esempio n. 17
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),
        }
Esempio n. 18
0
 def make_relative(self, href):
     return make_relative(self.curr_path, href)