-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
64 lines (55 loc) · 1.53 KB
/
common.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
from codecs import open
from os import mkdir, system
from os.path import join, exists
try:
import hashlib
except ImportError: # python < 2.5
import md5
hashlib = None
def hexdigest_md5(data):
if hashlib:
return hashlib.md5(data).hexdigest()
else:
return md5.new(data).hexdigest()
def generatePage(page, title, content, folder, toc):
fname = hexdigest_md5(page) + ".html"
fpath = join(folder, fname)
toc.write("\t\t\t<a title=\"%s\" href=\"%s\" />\n" % (title, fname))
if not exists(fpath):
open(fpath, "wb", "utf-8").write(u"""<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<style type="text/css" title="override_css">
@page {padding: 0pt; margin:0pt}
</style>
<title>%s</title>
</head>
<body>
<h1>%s</h1>
%s
</body>
</html>""" % (title, title, content))
return True
else:
return False
def tocStart(folder):
if not exists(folder):
mkdir(folder)
toc = open(join(folder, "toc.html"), "wb", "utf-8")
toc.write("""<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>%s</title>
</head>
<body class="vcenter">
<div style="display:none">
""" % folder)
return toc
def tocEnd(toc):
toc.write("""\t\t</div>
</body>
</html>""")
toc.close()
def makeMobi(folder, author):
if not exists(folder + ".mobi"):
cmd = "rm -f book.zip && zip -j book.zip %s/* && ebook-convert book.zip \"%s.mobi\" --output-profile kindle --margin-top 0 --margin-bottom 0 --margin-left 0 --authors=\"%s\" --input-encoding=utf-8" %(folder.replace(" ", "\\ "), folder, author)
print cmd
system(cmd)