Esempio n. 1
0
cover_page = '''
<div>
<img src="../images/cover.png" alt="Dhamma Wheel"/>
</div>
'''

introduction_page = '''
<h1>Long Discourses</h1>

<p>This is a draft eBook based on the translations by Bhante Sujato from suttacentral.net, it is not for distribution</p>
'''

stylesheet = '''
'''

outfile = 'dn-predraft.epub'

book = Book(title=title, author=author)
book.add_stylesheet('\n')
book.add_image(name='cover.png', data=open(cover, 'rb').read())
book.add_page(title=title, content=cover_page, uid='cover')
title_page = book.add_page(title='Introduction', content=introduction_page, uid='intro')

for uid in [f'dn{i}' for i in range(1, 35)]:
    chapter_content = make_clean_html(uid)
    chapter_title = regex.search('<h1>(.*)</h1>', chapter_content)[1].strip()
    chapter = book.add_page(title=chapter_title, content=chapter_content, uid='uid)

book.save(outfile)
epubcheck(outfile)
Esempio n. 2
0
    def test_book(self):
        book = Book()
        book.title = 'Most Wanted Tips for Aspiring Young Pirates'
        book.add_creator('Monkey D Luffy')
        book.add_creator('Guybrush Threepwood')
        book.add_meta_info('contributor', 'Smalltalk80', role='bkp')
        book.add_meta_info('date', '2010', event='publication')

        book.enable_title_page()
        book.enable_table_of_contents()

        book.add_css(r'templates/main.css', 'main.css')

        n1 = book.add_html('', '1.html', get_minimal_html('Chapter 1'))
        n11 = book.add_html('', '2.html', get_minimal_html('Section 1.1'))
        n111 = book.add_html('', '3.html', get_minimal_html('Section 1.1.1'))
        n12 = book.add_html('', '4.html', get_minimal_html('Section 1.2'))
        n2 = book.add_html('', '5.html', get_minimal_html('Chapter 2'))

        book.add_spine_item(n1)
        book.add_spine_item(n11)
        book.add_spine_item(n111)
        book.add_spine_item(n12)
        book.add_spine_item(n2)

        # You can use both forms to add TOC map
        #t1 = book.add_toc_node(n1.dest_path, '1')
        #t11 = book.add_toc_node(n11.dest_path, '1.1', parent = t1)
        #t111 = book.add_toc_node(n111.dest_path, '1.1.1', parent = t11)
        #t12 = book.add_toc_node(n12.dest_path, '1.2', parent = t1)
        #t2 = book.add_toc_node(n2.dest_path, '2')

        book.add_toc_node(n1.dest_path, '1')
        book.add_toc_node(n11.dest_path, '1.1', 2)
        book.add_toc_node(n111.dest_path, '1.1.1', 3)
        book.add_toc_node(n12.dest_path, '1.2', 2)
        book.add_toc_node(n2.dest_path, '2')

        rootDir = r'/tmp/epubtest/'
        if os.path.exists(rootDir):
            shutil.rmtree(rootDir)
        book.raw_publish(rootDir)
        Book.create_epub(rootDir, "%s.epub" % os.path.dirname(rootDir))
        Book.validate_epub('../epubcheck-1.1.jar', "%s.epub" %
                           os.path.dirname(rootDir))
Esempio n. 3
0
 def start(self):
     raw_book_list = self.init_book_data()
     book_list = [self.translate_book_into_html(book) for book in raw_book_list]
     for book in book_list:
         BaseClass.change_dir(self.base_path)
         BaseClass.change_dir(self.base_path + u'/知乎电子书临时资源库')
         epub = Book(book['info']['title'], 27149527)
         book.start_download_image()
         for page in book['page_list']:
             with open(self.base_path + u'/知乎电子书临时资源库' + page['filename'], 'w') as html:
                 html.write(page['content'])
             epub.addHtml(self.base_path + u'/知乎电子书临时资源库' + page['filename'], page['title'])
         for filename in book['image_list']:
             epub.addImg(self.base_path + u'/知乎电子书临时资源库/知乎图片池' + filename)
             book.addLanguage('zh-cn')
             epub.addCreator('ZhihuHelp1.7.0')
             epub.addDesc(u'该电子书由知乎助手生成,知乎助手是姚泽源为知友制作的仅供个人使用的简易电子书制作工具,源代码遵循WTFPL,希望大家能认真领会该协议的真谛,为飞面事业做出自己的贡献 XD')
             epub.addRight('CC')
             epub.addPublisher('ZhihuHelp')
             epub.addCss(u'../../../epubResource/markdownStyle.css')
             epub.addCss(u'../../../epubResource/userDefine.css')
             epub.buildingEpub()
     return
Esempio n. 4
0
def main():
    templates = TemplateLoader("templates")
    newspaper = Edition(templates,
                        settings.OUTPUT_FOLDER,
                        settings.RSS_CATEGORIES)
    newspaper.save()
    book = Book()
    book.title = newspaper.title
    book.add_creator("Star-Slurper")
    book.add_meta_info('date', '2010', event='publication')
    book.enable_title_page()
    book.set_table_of_contents(newspaper.toc_path)
    book.add_css(r'templates/main.css', 'main.css')
    for c_index, c_data in enumerate(newspaper.iter_categories()):
        i, o, category = c_data
        print "%s %s %s" % c_data
        manifest_item = book.add_html(i, o, None)
        book.add_spine_item(manifest_item)
        book.add_toc_node(
            manifest_item.dest_path,
            category.name,
            1,
        )
        for a_index, a_data in enumerate(category.iter_articles()):
            i, o, article = a_data
            print "%s %s %s" % a_data
            manifest_item = book.add_html(i, o, None)
            book.add_spine_item(manifest_item)
            book.add_toc_node(
                manifest_item.dest_path,
                article.get_title(),
                2,
            )
    [book.add_image(i, o) for (i, o) in newspaper.iter_images()]
    root_dir = os.path.join("/", "tmp", newspaper.date)
    if os.path.exists(root_dir):
        shutil.rmtree(root_dir)
    book.raw_publish(root_dir)
    target_file = os.path.join(settings.OUTPUT_FOLDER,
                               "%s.epub" % newspaper.date)
    Book.create_epub(root_dir, target_file)
    log.info("Done!")
Esempio n. 5
0
 def create(self):
     self.image_container.set_save_path(Path.image_pool_path)
     self.image_container.start_download()
     title = '_'.join([book.epub.title for book in self.book_list])
     title = title.strip()[:128] # 避开window文件名长度限制
     title = ExtraTools.fix_filename(title) # 移除特殊字符
     if not title:
         # 电子书题目为空时自动跳过
         # 否则会发生『rm -rf / 』的惨剧。。。
         return
     Path.chdir(Path.base_path + u'/知乎电子书临时资源库/')
     epub = Book(title, 27149527)
     html_tmp_path = Path.html_pool_path + '/'
     image_tmp_path = Path.image_pool_path + '/'
     for book in self.book_list:
         page = book.page_list[0]
         with open(html_tmp_path + page.filename, 'w') as html:
             html.write(page.content)
         #epub.createChapter(html_tmp_path + page.filename, ExtraTools.get_time(), page.title)
         epub.addInfoPage(html_tmp_path + page.filename, page.title)
         for page in book.page_list[1:]:
             with open(html_tmp_path + page.filename, 'w') as html:
                 html.write(page.content)
             epub.addHtml(html_tmp_path + page.filename, page.title)
     for image in self.book.image_list:
         epub.addImg(image_tmp_path + image['filename'])
     epub.addLanguage('zh-cn')
     epub.addCreator('ZhihuHelp1.7.0')
     epub.addDesc(u'该电子书由知乎助手生成,知乎助手是姚泽源为知友制作的仅供个人使用的简易电子书制作工具,源代码遵循WTFPL,希望大家能认真领会该协议的真谛,为飞面事业做出自己的贡献 XD')
     epub.addRight('CC')
     epub.addPublisher('ZhihuHelp')
     epub.addCss(Path.base_path + u'/www/css/markdown.css')
     epub.addCss(Path.base_path + u'/www/css/front.css')
     epub.buildingEpub()
     Path.reset_path()
     return
Esempio n. 6
0
    def create(self):
        self.image_container.set_save_path(Path.image_pool_path)
        self.image_container.start_download()
        title = "_".join([book.property.epub.title for book in self.book_list])
        title = title.strip()
        Path.chdir(Path.base_path + u"/知乎电子书临时资源库/")
        epub = Book(title, 27149527)
        html_tmp_path = Path.html_pool_path + "/"
        image_tmp_path = Path.image_pool_path + "/"
        for book in self.book_list:
            page = book.page_list[0]
            with open(html_tmp_path + page.filename, "w") as html:
                html.write(page.content)
            epub.createChapter(html_tmp_path + page.filename, ExtraTools.get_time(), page.title)

            for page in book.page_list[1:]:
                with open(html_tmp_path + page.filename, "w") as html:
                    html.write(page.content)
                epub.addHtml(html_tmp_path + page.filename, page.title)
        for image in self.book["image_list"]:
            epub.addImg(image_tmp_path + image["filename"])
        epub.addLanguage("zh-cn")
        epub.addCreator("ZhihuHelp1.7.0")
        epub.addDesc(u"该电子书由知乎助手生成,知乎助手是姚泽源为知友制作的仅供个人使用的简易电子书制作工具,源代码遵循WTFPL,希望大家能认真领会该协议的真谛,为飞面事业做出自己的贡献 XD")
        epub.addRight("CC")
        epub.addPublisher("ZhihuHelp")
        Debug.logger.debug(u"当前目录为")
        Path.pwd()
        epub.addCss(Path.base_path + u"/epubResource/markdown.css")
        epub.addCss(Path.base_path + u"/epubResource/front.css")
        epub.buildingEpub()
        return