示例#1
0
    def _build_tags(self, tags):
        """
        :type tags: list[(str, list[Post])]
        """
        output_dir = os.path.join(self._site_dir, "tag")
        ensure_dir_exists(output_dir)
        template = self._jinja.get_template("tag.html")

        tq = tqdm.tqdm(total=len(tags))
        for tag, posts in tags:
            posts.sort(key=lambda p: p.published, reverse=True)
            html = template.render(name=tag, posts=posts)
            filepath = os.path.join(output_dir, "%s.html" % tag)
            write_file(filepath, html)
            tq.update(1)
        tq.close()

        # taglist
        taglist = [(tag, len(posts)) for tag, posts in tags]
        taglist.sort(key=lambda item: (item[1], item[0]), reverse=True)

        template = self._jinja.get_template("taglist.html")
        html = template.render(tags=taglist)
        filepath = os.path.join(self._page_dir, "tags.html")
        write_file(filepath, html)
示例#2
0
    def _build_pickys(self, pickys):
        output_dir = os.path.join(self._site_dir, "picky")
        ensure_dir_exists(output_dir)
        template = self._jinja.get_template("picky.html")

        for picky in tqdm.tqdm(pickys):
            html = template.render(picky=picky)
            filepath = os.path.join(output_dir, "%s.html" % picky.slug)
            write_file(filepath, html)
示例#3
0
    def _build_pickys(self, pickys):
        output_dir = os.path.join(self._site_dir, "picky")
        ensure_dir_exists(output_dir)
        template = self._jinja.get_template("picky.html")

        for picky in tqdm.tqdm(pickys):
            html = template.render(picky=picky)
            filepath = os.path.join(output_dir, "%s.html" % picky.slug)
            write_file(filepath, html)
示例#4
0
    def build(self):
        echo.info("start, output to %s", self._site_dir)
        self._build_assets()

        echo.info("parsing pickys...")
        pickys = self._parse_pickys()
        echo.info("parsing posts...")
        posts, tags = self._parse_posts()

        echo.info("building pickys...")
        self._build_pickys(pickys)
        echo.info("building posts...")
        self._build_posts(posts)
        echo.info("building tags...")
        self._build_tags(tags)

        # home
        echo.info("building index...")
        template = self._jinja.get_template("home.html")
        home_posts = filter(lambda post: post.slug not in self._privacies,
                            posts[:self.HomePosts])
        html = template.render(posts=home_posts)
        write_file(os.path.join(self._site_dir, "index.html"), html)

        # feed
        echo.info("building feed...")
        template = self._jinja.get_template("feed.xml")
        xml = template.render(posts=posts[:self.FeedPosts])
        write_file(os.path.join(self._page_dir, "feed.xml"), xml)

        # 404
        echo.info("building 404...")
        template = self._jinja.get_template("e404.html")
        html = template.render()
        write_file(os.path.join(self._page_dir, "e404.html"), html)
示例#5
0
    def build(self):
        echo.info("start, output to %s", self._site_dir)
        self._build_assets()

        echo.info("parsing pickys...")
        pickys = self._parse_pickys()
        echo.info("parsing posts...")
        posts, tags = self._parse_posts()

        echo.info("building pickys...")
        self._build_pickys(pickys)
        echo.info("building posts...")
        self._build_posts(posts)
        echo.info("building tags...")
        self._build_tags(tags)

        # home
        echo.info("building index...")
        template = self._jinja.get_template("home.html")
        home_posts = filter(lambda post: post.slug not in self._privacies, posts[: self.HomePosts])
        html = template.render(posts=home_posts)
        write_file(os.path.join(self._site_dir, "index.html"), html)

        # feed
        echo.info("building feed...")
        template = self._jinja.get_template("feed.xml")
        xml = template.render(posts=posts[: self.FeedPosts])
        write_file(os.path.join(self._page_dir, "feed.xml"), xml)

        # 404
        echo.info("building 404...")
        template = self._jinja.get_template("e404.html")
        html = template.render()
        write_file(os.path.join(self._page_dir, "e404.html"), html)
示例#6
0
    def _build_posts(self, posts):
        """
        :type posts: list[Post]
        """
        output_dir = self._site_dir
        template = self._jinja.get_template("post.html")

        count = len(posts)
        tq = tqdm.tqdm(total=count)
        for i, post in enumerate(posts):
            post.next = None if i < 1 else posts[i - 1]
            post.prev = None if i > count - 2 else posts[i + 1]
            html = template.render(post=post)
            filepath = os.path.join(output_dir, "%s.html" % post.slug)
            write_file(filepath, html)
            tq.update(1)
        tq.close()

        # archives
        template = self._jinja.get_template("archives.html")
        archives = {}
        for post in posts:
            if post.slug in self._privacies:
                continue

            year = post.published.year
            if year in archives:
                archives[year].append(post)
            else:
                archives[year] = [post]

        archives = sorted(
            archives.items(), key=lambda item: item[0], reverse=True
        )

        # count 包括私密文章
        html = template.render(count=count, archives=archives)
        filepath = os.path.join(self._page_dir, "all.html")
        write_file(filepath, html)
示例#7
0
    def _build_posts(self, posts):
        """
        :type posts: list[Post]
        """
        output_dir = self._site_dir
        template = self._jinja.get_template("post.html")

        count = len(posts)
        tq = tqdm.tqdm(total=count)
        for i, post in enumerate(posts):
            post.next = None if i < 1 else posts[i - 1]
            post.prev = None if i > count - 2 else posts[i + 1]
            html = template.render(post=post)
            filepath = os.path.join(output_dir, "%s.html" % post.slug)
            write_file(filepath, html)
            tq.update(1)
        tq.close()

        # archives
        template = self._jinja.get_template("archives.html")
        archives = {}
        for post in posts:
            if post.slug in self._privacies:
                continue

            year = post.published.year
            if year in archives:
                archives[year].append(post)
            else:
                archives[year] = [post]

        archives = sorted(archives.items(),
                          key=lambda item: item[0],
                          reverse=True)

        # count 包括私密文章
        html = template.render(count=count, archives=archives)
        filepath = os.path.join(self._page_dir, "all.html")
        write_file(filepath, html)