-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmake-ebook
executable file
·82 lines (63 loc) · 2.54 KB
/
make-ebook
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
#!/usr/bin/env python
import html
import json
import os
import textwrap
from ebooklib import epub
AUTHOR = '_9MOTHER9HORSE9EYES9'
TITLE = 'The Interface Series'
LANGUAGE = 'en'
FRONT_COVER_FILE_NAME = 'front-cover.jpg'
BACK_COVER_FILE_NAME = 'back-cover.jpg'
# There's no real rhyme or reason to this, I just tried to find a value which looks good on my e-book reader
MAX_TITLE_LENGTH = 56
def main():
spine = {}
for username in os.listdir('parts'):
print('-', username)
print(' - Loading spine...')
spine_path = os.path.join('parts', username, 'spine.json')
with open(spine_path) as file:
user_spine = json.load(file)
spine.update({id_: (username, created) for id_, created in user_spine.items()})
book = epub.EpubBook()
book.set_title(TITLE)
book.add_author(AUTHOR)
book.set_language(LANGUAGE)
# Front cover
with open(FRONT_COVER_FILE_NAME, 'rb') as file:
book.set_cover(FRONT_COVER_FILE_NAME, file.read())
book.get_item_with_id('cover').is_linear = True
# Back cover image
back_cover_image = epub.EpubCover('back-cover-image', BACK_COVER_FILE_NAME)
with open(BACK_COVER_FILE_NAME, 'rb') as file:
back_cover_image.content = file.read()
book.add_item(back_cover_image)
# Back cover html
back_cover = epub.EpubCoverHtml('back-cover', 'back-cover.xhtml', BACK_COVER_FILE_NAME)
back_cover.is_linear = True
book.add_item(back_cover)
print('- Loading parts...')
chapters = []
for id_, (username, created) in sorted(spine.items(), key=lambda item: item[1][1]):
with open(os.path.join('parts', username, id_ + '.html')) as file:
html_content = file.read()
with open(os.path.join('parts', username, id_ + '.txt')) as file:
text = file.read()
file_name = id_ + '.xhtml'
chapter = epub.EpubHtml(file_name=file_name)
chapter.content = html_content
book.add_item(chapter)
chapters.append(chapter)
# Have to unescape HTML entities in the text content
title = textwrap.shorten('%s: %s' % (username, html.unescape(text)), MAX_TITLE_LENGTH, placeholder='...')
book.toc.append(epub.Link(file_name, title, id_))
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())
book.spine = ['cover', 'nav', *chapters, 'back-cover']
file_name = '%s #%d.epub' % (book.title, len(chapters))
print('- Saving: %s...' % file_name)
epub.write_epub(file_name, book)
print('Finished!')
if __name__ == '__main__':
main()