Example #1
0
    def run(self, document, filename):
        docparser = DocumentParser()
        docparser.parse(document.root)

        identifier = 'uuid'  # FIXME
        lang = document.manifest['lang']
        title = document.manifest['title']
        subtitle = document.manifest['subtitle']
        authors = document.manifest['authors']

        templates_folder = os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            'templates'
        )
        env = Environment(loader=FileSystemLoader(templates_folder))

        self.epub = EpubBuilder(filename)
        self.epub.identifier = identifier
        self.epub.title = title
        self.epub.metadata.add_language(lang)
        self.epub.open()

        # add navpoints
        if len(docparser.navpoints) == 0:  # add one navpoint if none (else invalid)
            self.epub.add_navpoint(EpubNavPoint(
                id='section-0',
                source='texts/section-0.xhtml',
                label='Contents',  # FIXME i18n
            ))
        else:
            for navpoint in docparser.navpoints:
                self.epub.add_navpoint(navpoint)

        # titlepage
        template = env.get_template('epub_titlepage.xhtml')
        self.epub.add_text_from_string(
            'texts/titlepage.xhtml',
            template.render(lang=lang, title=title, subtitle=subtitle, authors=', '.join(authors)),
            'titlepage'
        )
        template = env.get_template('epub_titlepage_style.css')
        self.epub.add_style_from_string('styles/titlepage.css', template.render(), 'style_titlepage')
        self.epub.add_guide("title-page", "Title page", "texts/titlepage.xhtml")

        # contents
        if docparser.navpoints:
            contents_title = self.contents_i18n[lang]
            template = env.get_template('epub_contents.xhtml')
            self.epub.add_text_from_string(
                'texts/contents.xhtml',
                template.render(lang=lang, title=contents_title, navpoints=self.epub.navpoints),
                'contents'
            )
            self.epub.add_guide("contents", contents_title, "texts/contents.xhtml")

        # add pages
        renderer = EpubRenderer()
        template = env.get_template('epub_main.xhtml')
        for section in docparser.section_list:
            self.epub.add_text_from_string(
                'texts/{}.xhtml'.format(section['id']),
                template.render(
                    lang=lang,
                    title=section['title'],
                    content=renderer.run(section['nodes']).rstrip().replace("\n", "\n    ")  # FIXME indent?
                ),
                section['id']
            )

        # add main.css
        template = env.get_template('epub_style.css')
        self.epub.add_style_from_string('styles/main.css', template.render(), 'style_main')

        self.epub.close()
Example #2
0
from epub2 import EpubBuilder, EpubNavPoint

epub = EpubBuilder("contents.epub")

epub.identifier = "contents"
epub.title = "Deep Table of Contents"

epub.metadata.add_language("en")

toc1 = EpubNavPoint("toc.1", "1. Chapter 1", "text.html#toc.1")
toc2 = EpubNavPoint("toc.2", "2. Chapter 2", "text.html#toc.2")
toc3 = EpubNavPoint("toc.3", "3. Chapter 3", "text.html#toc.3")
epub.add_navpoint(toc1)
epub.add_navpoint(toc2)
epub.add_navpoint(toc3)

toc11 = EpubNavPoint("toc.1.1", "1.1. Section 1", "text.html#toc.1.1")
toc12 = EpubNavPoint("toc.1.2", "1.2. Section 2", "text.html#toc.1.2")
toc1.append(toc11)
toc1.append(toc12)

toc21 = EpubNavPoint("toc.2.1", "2.1. Section 1", "text.html#toc.2.1", [
    EpubNavPoint("toc.2.1.1", "2.1.1. Subsection 1", "text.html#toc.2.1.1"),
    EpubNavPoint("toc.2.1.2", "2.1.2. Subsection 2", "text.html#toc.2.1.2"),
    EpubNavPoint("toc.2.1.3", "2.1.3. Subsection 3", "text.html#toc.2.1.3")
])
toc22 = EpubNavPoint("toc.2.2", "2.2. Section 2", "text.html#toc.2.2")
toc2.append(toc21)
toc2.append(toc22)

epub.open()
Example #3
0
from epub2 import EpubBuilder, EpubNavPoint

epub = EpubBuilder("sample.epub")
epub.open()

epub.identifier = "sample"
epub.title = "Sample ePUB file"

epub.metadata.add_title("Title N°2")
epub.metadata.add_title("Title N°3")
epub.metadata.add_creator("Creator N°1")
epub.metadata.add_creator("Creator N°2", role="aut")
epub.metadata.add_creator("Creator N°3", file_as="Creator 3", role="oth")
epub.metadata.add_subject("Subject N°1")
epub.metadata.add_subject("Subject N°2")
epub.metadata.set_description("Description")
epub.metadata.set_publisher("Publisher")
epub.metadata.add_contributor("Contributor N°1")
epub.metadata.add_contributor("Contributor N°2", role="aut")
epub.metadata.add_contributor("Contributor N°3", file_as="Contributor 3", role="oth")
epub.metadata.add_date("2014")
epub.metadata.add_date("2014-12", event="modification")
epub.metadata.add_date("2014-12-25", event="publication")
epub.metadata.set_type("type")
epub.metadata.set_format("format")
epub.metadata.add_identifier("uuid002")
epub.metadata.add_identifier("uuid003", "myid", "scheme")
epub.metadata.set_source("source")
epub.metadata.add_language("en-US")
epub.metadata.add_language("fr-fr")
epub.metadata.add_language("de")