]) toc22 = EpubNavPoint("toc.2.2", "2.2. Section 2", "text.html#toc.2.2") toc2.append(toc21) toc2.append(toc22) epub.open() epub.add_text_from_string(id="text", localname="text.html", content="""\ <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8"/> <title>Deep Table of Contents</title> </head> <body> <h1 id="toc.1">1. Chapter 1</h1> <h2 id="toc.1.1">1.1. Section 1</h2> <h2 id="toc.1.2">1.2. Section 2</h2> <h1 id="toc.2">2. Chapter 2</h1> <h2 id="toc.2.1">2.1. Section 1</h2> <h3 id="toc.2.1.1">2.1.1. Subsection 1</h3> <h3 id="toc.2.1.2">2.1.2. Subsection 2</h3> <h3 id="toc.2.1.3">2.1.3. Subsection 3</h3> <h2 id="toc.2.2">2.2. Section 2</h2> <h1 id="toc.3">3. Chapter 3</h1> </body> </html>""") epub.close()
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()