-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathez_epub.py
92 lines (81 loc) · 3.32 KB
/
ez_epub.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
# Copyright (c) 2012, Bin Tan
# Copyright (c) 2013, Andrea Bernardini
# This file is distributed under the BSD Licence (2-clause).
import epub, os
from genshi.template import TemplateLoader, MarkupTemplate
from epub import modulepath
class Section:
def __init__(self):
self.title = ''
self.subsections = []
self.css = ''
self.text = []
self.templateFileName = os.path.join(modulepath, 'templates/ez-section-mod.html')
class Book:
def __init__(self):
self.impl = epub.EpubBook()
self.title = ''
self.authors = []
self.cover = ''
self.lang = 'en-US'
self.sections = []
self.makefunc = {'plain': self.__addSection, 'html': self.__addHtmlSection}
self.templateLoader = TemplateLoader('templates')
if not os.path.exists(os.path.join('/tmp' , 'epub-builder')):
os.makedirs(os.path.join('/tmp' , 'epub-builder'))
def __addHtmlSection(self, section, id, depth):
if depth > 0:
content = ''
for text in section.text:
content = content + text
urls = self.impl.downloadHtmlImages(content)
content = self.impl.remoteToLocalImageUrls(urls, content)
stream = MarkupTemplate("""<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://genshi.edgewall.org/">
<head>
<title>${section.title}</title>
<style type="text/css">
h1 { text-align: center; }
${section.css}
</style>
</head>
<body>
<h1>${section.title}</h1>
""" + content + """
</body>
</html>""").generate(section = section)
html = stream.render('xhtml', doctype = 'xhtml11', drop_xml_decl = False)
item = self.impl.addHtml('', '%s.html' % id, html)
self.impl.addSpineItem(item)
self.impl.addTocMapNode(item.destPath, section.title, depth)
id += '.'
if len(section.subsections) > 0:
for i, subsection in enumerate(section.subsections):
self.__addHtmlSection(subsection, id + str(i + 1), depth + 1)
def __addSection(self, section, id, depth):
if depth > 0:
stream = self.templateLoader.load(section.templateFileName).generate(section = section)
html = stream.render('xhtml', doctype = 'xhtml11', drop_xml_decl = False)
item = self.impl.addHtml('', '%s.html' % id, html)
self.impl.addSpineItem(item)
self.impl.addTocMapNode(item.destPath, section.title, depth)
id += '.'
if len(section.subsections) > 0:
for i, subsection in enumerate(section.subsections):
self.__addSection(subsection, id + str(i + 1), depth + 1)
def make(self, outputDir, type):
outputFile = outputDir + '.epub'
self.impl.setTitle(self.title)
self.impl.setLang(self.lang)
for author in self.authors:
self.impl.addCreator(author)
if self.cover:
self.impl.addCover(self.cover)
self.impl.addTitlePage()
self.impl.addTocPage()
root = Section()
root.subsections = self.sections
self.makefunc[type](root, 's', 0)
self.impl.createBook()
self.impl.createArchive(outputFile)