-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
93 lines (68 loc) · 2.32 KB
/
Main.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
93
import json
import os
from os.path import join
import zipfile
from Story import Story
from templates import Manifest, MainStory, Preface, TitlePage, TableOfContent
def main():
# Read in Stories and metadata
inputFilenames = [
"story/story-template.json",
"story/no-chapters.json"
]
stories = readInStories(inputFilenames)
for i, story in enumerate(stories):
i += 1
if story.hasPreface():
writePreface(i, story.preface)
writeMainContent(i, story)
writeTitlePage(i, story)
writeTableOfContent(stories)
writeManifest()
# Export ebook files
makeEbook()
def readInStories(inputFilenames):
stories = []
for fileName in inputFilenames:
with open(fileName, 'r', encoding="UTF-8") as jsonFile:
data = json.load(jsonFile)
stories.append(Story(data))
return stories
def writeTableOfContent(stories):
filename = join('Text', 'Section0001.xhtml')
message = TableOfContent.formatMainTableOfContent(stories)
writeContent(filename, message)
def writeTitlePage(index, story):
filename = join('Text', 'Section%02d01.xhtml' % index)
story.filenameTitlePage = filename
message = TitlePage.formatTitlePage(story)
writeContent(filename, message)
def writePreface(index, preface):
filename = join('Text', 'Section%02d02.xhtml' % index)
preface.filenamePreface = filename
message = Preface.formatPreface(preface)
writeContent(filename, message)
def writeMainContent(index, story):
filename = join('Text', 'Section%02d03.xhtml' % index)
story.filenameMainStory = filename
message = MainStory.formatMainStory(story)
writeContent(filename, message)
def writeManifest():
filenames = os.listdir(join('book', 'OEBPS', 'Text'))
filenames.sort()
message = Manifest.manifest(filenames)
writeContent('content.opf', message)
def writeContent(filename, content):
f = open(join('book', 'OEBPS', filename), 'w', encoding='UTF-8')
f.write(content)
f.close()
def zipdir(path, ziph):
# ziph is zipfile handle
for root, dirs, files in os.walk(path):
for file in files:
ziph.write(os.path.join(root, file))
def makeEbook():
zipf = zipfile.ZipFile('ebook.epub', 'w', zipfile.ZIP_DEFLATED)
zipdir('book/', zipf)
zipf.close()
main()