Пример #1
0
    def build_categories(self):
        """No paging
        """
        for category in self._categories:
            posts = self._posts.re_group(group_by_category(category))

            # insert sub-category to posts
            cate_node = self._node_list.get('_'.join(['category', category]),
                                            None)
            sub_cates = []
            for node in cate_node._childern:
                if node._type == 'category':
                    cate = {
                        "title":
                        node._title,
                        "permalink":
                        self._router.gen_permalink('category', node._slug)
                    }
                    sub_cates.insert(0, cate)

            current_route, local_path = self._router.gen(
                "category", category, 1)
            local_path += "index.html"

            template = self._env.get_template("categories.html")
            output = template.render(current_route=current_route,
                                     cate_name=category,
                                     sub_cates=sub_cates,
                                     content_list=posts)
            safe_write(local_path, output)
Пример #2
0
    def build_posts(self):
        total_posts = len(self._posts)
        for index in range(total_posts):
            content = self._posts[index]

            # find visible prev and next
            index_next = index
            content_next = None
            while content_next is None and index_next > 0:
                index_next -= 1
                if not self._posts[index_next].skip:
                    content_next = self._posts[index_next]

            index_prev = index
            content_prev = None
            while content_prev is None and index_prev < total_posts - 1:
                index_prev += 1
                if not self._posts[index_prev].skip:
                    content_prev = self._posts[index_prev]

            meta = content.meta
            current_route, local_path = self._router.gen_by_meta(meta)
            local_path += "index.html"

            template = self._env.get_template("post.html")
            output = template.render(current_route=current_route,
                                     content=content,
                                     content_prev=content_prev,
                                     content_next=content_next)
            safe_write(local_path, output)
            print('Finished: ' + content.get_meta('title'))
Пример #3
0
    def build_search_cache(self):
        """build search cache json
        """
        def render_search_cache(post_list, page_list):
            router = self._router

            def strip(text):
                r = re.compile(r'<[^>]+>', re.S)
                return r.sub('', text)

            def gen_entry(content):
                entry = {
                    "title": content.get_meta('title'),
                    "date": str(content.get_meta('date')),
                    "path": router.gen_permalink_by_content(content),
                    "text": strip(content.parsed),
                    "categories": [],
                    "tags": []
                }
                if (content.get_meta('layout') == 'post'):
                    for cate in content.get_meta('categories'):
                        entry['categories'].append({
                            "name":
                            cate,
                            "slug":
                            cate,
                            "permalink":
                            router.gen_permalink('category', cate, 1)
                        })
                    for tag in content.get_meta('tags'):
                        entry['tags'].append({
                            "name":
                            tag,
                            "slug":
                            tag,
                            "permalink":
                            router.gen_permalink('tag', tag, 1)
                        })
                return entry

            posts = [gen_entry(post) for post in post_list if not post.skip]
            pages = [gen_entry(page) for page in page_list if not page.skip]

            cache = json.dumps({"posts": posts, "pages": pages})

            return cache

        cache_str = render_search_cache(self._posts, self._pages)
        search_cache_hash = gen_hash(cache_str)
        safe_write(
            unify_joinpath(self._config.build_dir,
                           search_cache_hash + '.json'), cache_str)

        self._env.globals['search_cache_hash'] = search_cache_hash
Пример #4
0
    def build_archives(self):
        pager = Pager(self._posts, self._config.archives_page_size)
        total_pages = pager.get_total_pages()

        for current_page, current_list in pager:
            _, local_path = self._router.gen("archives", "", current_page)
            local_path += "index.html"

            template = self._env.get_template("archives.html")
            output = template.render(content_list=current_list,
                                     current_page=current_page,
                                     max_pages=total_pages)
            safe_write(local_path, output)
Пример #5
0
    def build_tags(self):
        """No paging
        """
        for tag in self._tags:
            posts = self._posts.re_group(group_by_tagname(tag))

            current_route, local_path = self._router.gen("tag", tag, 1)
            if not os.path.exists(local_path):
                os.makedirs(local_path)
            local_path += "index.html"

            template = self._env.get_template("tags.html")
            output = template.render(current_route=current_route,
                                     tag_name=tag,
                                     content_list=posts)
            safe_write(local_path, output)
Пример #6
0
    def build_pages(self):
        total_pages = len(self._pages)
        for index in range(total_pages):
            content = self._pages[index]
            content_next = self._pages[index - 1] if index > 0 else None
            content_prev = self._posts[index +
                                       1] if index < total_pages - 1 else None

            _, local_path = self._router.gen_by_content(content)
            local_path += "index.html"

            template = self._env.get_template("page.html")
            output = template.render(content=content,
                                     content_prev=content_prev,
                                     content_next=content_next)
            safe_write(local_path, output)
            print('Finished: ' + content.get_meta('title'))
Пример #7
0
    def build_tags(self):
        for tag in self._tags:
            posts = self._posts.re_group(group_by_tagname(tag))

            pager = Pager(posts, self._config.archives_page_size)
            total_pages = pager.get_total_pages()

            for current_page, current_list in pager:
                _, local_path = self._router.gen("tag", tag, current_page)
                local_path += "index.html"

                template = self._env.get_template("tags.html")
                output = template.render(tag_name=tag,
                                         content_list=current_list,
                                         current_page=current_page,
                                         max_pages=total_pages)
                safe_write(local_path, output)
Пример #8
0
    def build_index(self):
        page_size = self._config.index_page_size
        total_contents = len(self._posts)
        total_pages = math.ceil(total_contents / page_size)

        for page in range(0, total_pages):
            current_list = self._posts[page * page_size:min(
                (page + 1) * page_size, total_contents)]

            _, local_path = self._router.gen("index", "", page + 1)
            local_path += "index.html"

            template = self._env.get_template("index.html")
            output = template.render(content_list=current_list,
                                     current_page=page + 1,
                                     max_pages=total_pages)
            safe_write(local_path, output)
Пример #9
0
    def build_categories(self):
        for category in self._categories:
            posts = self._posts.re_group(group_by_category(category))

            pager = Pager(posts, self._config.archives_page_size)
            total_pages = pager.get_total_pages()

            for current_page, current_list in pager:
                _, local_path = self._router.gen("category", category,
                                                 current_page)
                local_path += "index.html"

                template = self._env.get_template("categories.html")
                output = template.render(cate_name=category,
                                         content_list=current_list,
                                         current_page=current_page,
                                         max_pages=total_pages)
                safe_write(local_path, output)
Пример #10
0
    def build_categories(self):
        for category in self._categories:
            posts = self._posts.re_group(group_by_category(category))

            page_size = self._config.archives_page_size
            total_pages = math.ceil(len(posts) / page_size)

            for page in range(0, total_pages):
                current_list = \
                    posts[page * page_size:min(
                        (page+1)*page_size, len(posts))]

                _, local_path = self._router.gen("category", category,
                                                 page + 1)
                local_path += "index.html"

                template = self._env.get_template("categories.html")
                output = template.render(cate_name=category,
                                         content_list=current_list,
                                         current_page=page + 1,
                                         max_pages=total_pages)
                safe_write(local_path, output)
Пример #11
0
    def build_tags(self):
        for tag in self._tags:
            posts = self._posts.re_group(group_by_tagname(tag))

            page_size = self._config.archives_page_size
            total_pages = math.ceil(len(posts) / page_size)

            for page in range(0, total_pages):
                current_list = \
                    posts[page * page_size:min(
                        (page+1)*page_size, len(posts))]

                _, local_path = self._router.gen("tag", tag, page + 1)
                if not os.path.exists(local_path):
                    os.makedirs(local_path)
                local_path += "index.html"

                template = self._env.get_template("tags.html")
                output = template.render(tag_name=tag,
                                         content_list=current_list,
                                         current_page=page + 1,
                                         max_pages=total_pages)
                safe_write(local_path, output)