-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfb2book.py
More file actions
76 lines (70 loc) · 1.83 KB
/
fb2book.py
File metadata and controls
76 lines (70 loc) · 1.83 KB
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
class FB2book:
def __init__(self, title, author, thumbnail=None):
self.title = title
self.thumbnail = thumbnail
self.publisher = 'DALOR'
self.tags = []
self.authors = []
self.chapters = []
self.pictures = []
self.author = author
self.add_author(author)
def add_author(self, author):
self.authors.append('<first-name></first-name><last-name>{}</last-name>'.format(author))
def add_tag(self, tag):
self.tags.append(tag)
def format_chapter(self, chapter):
return '''
<section>
<title><p>{}</p></title>
{}
{}
</section>
'''.format(chapter.name, chapter.content, '\n'.join([self.format_chapter(ch) for ch in chapter.chapters]))
def add_chapter(self, chapter):
self.chapters.append(self.format_chapter(chapter))
def add_picture(self, pic):
self.pictures.append('<binary id="{}" content-type="{}">{}</binary>'.format(pic.name, pic.type, pic.content))
def result(self):
return '''<?xml version="1.0" encoding="UTF-8"?>
<FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0" xmlns:l="http://www.w3.org/1999/xlink">
<description>
<title-info>
<genre>
{}
</genre>
<author>
{}
</author>
<book-title>
{}
</book-title>
{}
</title-info>
<document-info>
<author>
<nickname>
{}
</nickname>
</author>
</document-info>
<publish-info>
<publisher>
{}
</publisher>
</publish-info>
</description>
<body>
{}
</body>
{}
</FictionBook>'''.format(
', '.join(self.tags),
'\n'.join(self.authors),
self.title,
'<coverpage><image l:href="{}" /></coverpage>'.format(self.thumbnail) if self.thumbnail else '',
self.author,
self.publisher,
'\n'.join(self.chapters),
'\n'.join(self.pictures)
).encode()