Exemple #1
0
    def addons_make_image_page(self, image_path, cover_page_path=None, width=None, heigth=None):
        """Make xhtml cover page contain the image you given.

        You must put the returned file to :class:`Epub2.files` by yourself

        :param image_path: Image path in your Epub2.files
        :param cover_page_path: Use this to get relative path to the image path
        :param width: Image width, automatic recognition if None
        :param heigth: Image heigth, automatic recognition if None
        :return: Cover xhtml page file.
        :rtype: File
        """

        img = Image.open(io.BytesIO(self.files[image_path].binary))
        width = width or img.size[0]
        height = heigth or img.size[1]

        relative = relative_path(os.path.split(cover_page_path or '')[0], image_path)

        xhtml_string = open(os.path.join(os.path.dirname(__file__), 'static', 'image_page.xhtml')).read()
        cover_page = xhtml_string.format(title='Cover', width=width, height=height, image_href=relative).encode()

        return File(cover_page)
Exemple #2
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