Ejemplo n.º 1
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))
Ejemplo n.º 2
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!")