예제 #1
0
def test_render_toc_md(render: Render, output_folder: str):
    title = 'hello'
    headers = ['标题1', '标题2']
    render.render_toc_md(title, headers)
    fn = os.path.join(output_folder, 'toc.md')

    assert os.path.isfile(fn)
    with open(fn) as f:
        ls = f.readlines()
        assert len(ls) == 3
        assert ls[0].rstrip('\n') == title
        assert ls[1].rstrip('\n') == '# {}'.format(headers[0])
        assert ls[2].rstrip('\n') == '# {}'.format(headers[1])

    os.remove(fn)
예제 #2
0
    def _render_source_files(self,
                             course_intro: dict,
                             course_content: list,
                             out_dir: str,
                             force: bool = False,
                             **kwargs) -> None:
        """
        下载课程源文件
        """
        articles = course_content
        column_title = course_intro['column_title']
        _out_dir = os.path.join(out_dir, column_title)
        if not os.path.isdir(_out_dir):
            os.makedirs(_out_dir)

        render = Render(_out_dir)
        # introduction
        if not force and os.path.isfile(os.path.join(_out_dir, '简介.html')):
            sys.stdout.write('{}简介 exists\n'.format(column_title))
        else:
            render.render_article_html('简介', course_intro['column_intro'],
                                       **kwargs)
            sys.stdout.write('下载{}简介 done\n'.format(column_title))
        # cover
        if not force and os.path.isfile(os.path.join(_out_dir, 'cover.jpg')):
            sys.stdout.write('{}封面 exists\n'.format(column_title))
        else:
            render.generate_cover_img(course_intro['column_cover'])
            sys.stdout.write('下载{}封面 done\n'.format(column_title))
        # toc
        ebook_name = self._format_title(course_intro)
        render.render_toc_md(
            ebook_name, ['简介'] +
            [render.format_file_name(t['article_title']) for t in articles])
        sys.stdout.write('下载{}目录 done\n'.format(column_title))
        # articles
        articles = tqdm(articles)
        for article in articles:
            articles.set_description('HTML 文件下载中:{}'.format(
                article['article_title'][:10]))
            title = render.format_file_name(article['article_title'])
            fn = os.path.join(_out_dir, '{}.html'.format(title))
            if not force and os.path.isfile(fn):
                continue
            render.render_article_html(title, article['article_content'],
                                       **kwargs)