Esempio n. 1
0
    def make_book(self, stories):
        """Make an ebook out of the parsed stories.

        :param stories: stories of this issue
        :type stories: as returned by get_stories
        :return: resulting epub book
        :rtype: Epub3
        """
        LOGGER.info('Make ebook ...')
        book = Epub3()

        LOGGER.info('Add metadata ...')
        book_title = 'Jungle World {}'.format(self.issue_no)
        book.metadata.append(Title(book_title))
        book.metadata.append(Identifier(book_title))
        book.metadata.append(Language('de'))
        book.metadata.append(Creator('Redaktion Jungle World'))
        book.metadata.append(Publisher('Jungle World Verlags GmbH'))
        book.metadata.append(Contributor('jw2epub'))
        book.metadata.append(get_dcterm('modified')(w3c_utc_date()))

        LOGGER.info('Add cover image ...')
        filename_cover = self.download_cover()
        # Might be jpg or gif, but epub lib barfs at that extension
        book.files['cover.png'] = File(open(filename_cover, 'rb').read())
        book.cover_image = 'cover.png'

        for story in stories:
            LOGGER.info('Add story %s ...', story['title'])
            page = '{}.html'.format(os.path.basename(story['uri']))
            book.files[page] = File(story['html'].encode('utf-8'))
            book.spine.append(Joint(page))
            book.toc.append(Section(story['title'], page))

        return book
Esempio n. 2
0
def test_epub3():
    from epubaker import Epub3
    from epubaker.metas import get_dcterm
    from epubaker.tools import w3c_utc_date

    book = make_epub(Epub3, Section)

    book.metadata.append(get_dcterm('modified')(w3c_utc_date()))

    book.files['cover.png'] = File(open(os.path.join(cur_path, 'cover', 'cover.png'), 'rb').read())
    book.cover_image = 'cover.png'

    # make user toc
    toc_page_path = 'user_toc.xhtml'
    toc_page_file = book.addons_make_toc_page()
    book.files[toc_page_path] = toc_page_file
    book.spine.insert(0, Joint(toc_page_path))
    book.toc.insert(0, Section('Table of Contents', href=toc_page_path))

    book_path = os.path.join(BUILT_BOOK_DIR, '3.epub')
    book.write(book_path)

    check_xml(book_path)
Esempio n. 3
0
def make_book(nikaya):
    """

    :param nikaya:
     :type nikaya: Nikaya
    :return:
    """
    from epubaker import Epub3, Section, File, Joint
    from epubaker.metas import Language, Title, Identifier, get_dcterm

    from epubaker.tools import w3c_utc_date, relative_path

    book = Epub3()
    for lang in nikaya.languages:
        book.metadata.append(Language(lang))

    book.metadata.append(Title(nikaya.title_chinese))

    book.metadata.append(get_dcterm('modified')(w3c_utc_date()))

    book.metadata.append(Identifier('identifier_' + uuid.uuid4().hex))

    js_path = 'Scripts/a.js'
    book.files[js_path] = File(open('xhtml/js/a.js', 'rb').read())

    sutra_template = jinja2.Template(open('xhtml/templates/sutra.xhtml', 'r').read())

    js_relative_path = relative_path('Pages', js_path)

    last_modified = None

    gmt_format = '%a, %d %b %Y %H:%M:%S GMT'

    def add_page_make_toc(section, subs):
        for sub in subs:

            if not isinstance(sub.sec_title, str):
                print(type(sub), sub.sec_title)
                exit()

            s = Section(title=sub.sec_title or sub.title)

            if not isinstance(sub, Sutra):

                add_page_make_toc(section=s, subs=sub.subs)

            else:
                sutra = sub
                path = 'Pages/{}.xhtml'.format(sutra.abbreviation)

                sutra_xhtml_str = sutra_template.render(head_title=sutra.abbreviation + ' ' + sutra.title,
                                                        title=sutra.abbreviation + ' ' + sutra.title,
                                                        main_lines=sutra.main_lines,

                                                        chinese_lines=[l for l in sutra.chinese.strip().splitlines()
                                                                       if l.strip()],

                                                        pali_lines=[l for l in sutra.pali.strip().splitlines()
                                                                    if l.strip()],

                                                        js_path=js_relative_path)

                book.files[path] = File(sutra_xhtml_str.encode())
                book.spine.append(Joint(path))

                s.href = path

                sutra_modified = datetime.datetime.strptime(sutra.modified, gmt_format)

                nonlocal last_modified
                if last_modified is None:
                    last_modified = sutra_modified

                if sutra_modified > last_modified:
                    last_modified = sutra_modified

            if hasattr(section, 'subs'):
                section.subs.append(s)
            else:
                section.append(s)

    add_page_make_toc(book.toc, nikaya.subs)

    introduction_template = jinja2.Template(open('xhtml/templates/说明.xhtml', 'r').read())
    introduction = introduction_template.render(homepage=HOME_PAGE,
                                                modified_time=last_modified.strftime('%Y-%m-%d'),
                                                created_time=datetime.datetime.now().strftime('%Y-%m-%d %H:%M'))
    introduction_path = 'introduction.xhtml'
    book.files[introduction_path] = File(introduction.encode())

    book.toc.append(Section(title='说明', href=introduction_path))
    book.spine.append(Joint(introduction_path))

    return book