Example #1
0
class EpubExporter:

    contents_i18n = {
        'en': 'Table of Contents',
        'fr': 'Table des matières',
        'de': 'Inhaltsverzeichnis',
    }

    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
    <title>Chapter 2</title>
    <link rel="stylesheet" href="../styles/stylesheet.css" type="text/css"/>
  </head>
  <body>
    <h1>Chapter 2</h1>
    <p><img src="../images/logo.png" alt="ePUB Logo"/></p>
    <ul>
      <li>Proinde concepta rabie saeviore, quam desperatio</li>
      <li>Illud autem non dubitatur quod cum esset aliquando</li>
      <li>Nam sole orto magnitudine angusti gurgitis sed</li>
      <li>Quare hoc quidem praeceptum, cuiuscumque est, ad</li>
      <li>Cumque pertinacius ut legum gnarus accusatorem</li>
    </ul>
  </body>
</html>""")

epub.add_style_from_string("styles/stylesheet.css", """\
h1 {
    color: gray;
    border-bottom: 1px solid gray;
}

p:first-letter {
    font-size: 1.8em;
    float: left;
}""", "style")

epub.add_file_from_file("images/logo.png", "sample-logo.png", "logo", "image/png")

epub.close()