-
Notifications
You must be signed in to change notification settings - Fork 0
/
style.py
141 lines (131 loc) · 4.75 KB
/
style.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from epub import *
def add_simple_cover(manifest, title, author, description=None, css_file=None, filename='cover.xhtml'):
writer = SimpleXMLWriter()
writer.write_xhtml_dtd_and_documentelement(lang='ja')
writer.start('head')
writer.element('title', text=title)
writer.write_link_stylesheet(css_file)
writer.end()
writer.start('body')
writer.element('h1', atts={'class':'cover title'}, text=title)
writer.element('h2', atts={'class':'cover author'}, text='作者:')
writer.element('p', text=author)
if description is not None:
writer.element('h2', atts={'class':'cover outline'}, text='あらすじ:')
writer.element('p', text=description)
manifest.add_item(filename, str(writer).encode('UTF-8'))
def create_simple_page(title, title_tagname, css_file, iterator):
writer = SimpleXMLWriter()
writer.write_xhtml_dtd_and_documentelement(lang='ja')
writer.start('head')
writer.element('title', text=title)
writer.write_link_stylesheet(css_file)
writer.end()
writer.start('body')
if title_tagname is not None:
writer.element(title_tagname, text=title)
for line in iterator:
writer.element('p', text=line.strip())
writer.end()
return str(writer)
def create_simple_page_from_html(package, filename, title, title_tagname, css_file, novel_body_element):
writer = SimpleXMLWriter()
writer.write_xhtml_dtd_and_documentelement(lang='ja')
writer.start('head')
writer.element('title', text=title)
writer.write_link_stylesheet(css_file)
writer.end()
writer.start('body')
if title_tagname is not None:
writer.element(title_tagname, text=title)
prev_is_empty_p = False
paragraph_open = False
for n in novel_body_element.iter():
if n.tag in ('ruby', 'img'):
prev_is_empty_p = False
if not paragraph_open:
paragraph_open = True
writer.start('p')
if n.tag == 'ruby':
writer.start('ruby')
for ruby_child in n.iter():
if ruby_child.text is None: continue
if ruby_child.tag in ('rt', 'rp'):
writer.element(ruby_child.tag, text=ruby_child.text)
else:
writer.text(ruby_child.text)
writer.end()
elif n.tag == 'img':
if 'src' in n.attrib:
(imgfilename, mime, data) = self.__process_image(n.attrib['src'])
if mime is not None and data is not None and imgfilename is not None:
package.manifest.add_item(imgfilename, data, media_type=mime)
writer.start('img', atts={'src':imgfilename})
if 'alt' in n.attrib:
writer.att('alt', n.attrib['alt'])
writer.end()
if n.tail is not None:
writer.text(n.tail.strip())
continue
# ignore
if n.tag in ('rb', 'rt', 'rp'):
continue
if paragraph_open and n.tag == 'br':
writer.end()
paragraph_open = False
text = n.text
if text is None: text = n.tail
if text is None and prev_is_empty_p: continue
if not paragraph_open:
writer.start('p')
paragraph_open = True
if text is not None:
writer.text(text.strip())
prev_is_empty_p = False
else:
prev_is_empty_p = True
writer.end()
package.manifest.add_item(filename, str(writer))
class StylesheetMap:
def __init__(self, default_css, cover_css = None, toc_css = None, page_css = None):
self.default = default_css
self.cover = cover_css if cover_css is not None else self.default
self.toc = toc_css if toc_css is not None else self.default
self.page = page_css if page_css is not None else self.default
def default_css(self): return self.default[0]
def cover_css(self): return self.cover[0]
def toc_css(self): return self.toc[0]
def page_css(self): return self.page[0]
def output(self, manifest):
css_list = (self.default, self.cover, self.toc, self.page)
wrote_set = set()
for css in css_list:
if css[0] in wrote_set: continue
wrote_set.add(css[0])
manifest.add_item(css[0], css[1].encode('UTF-8'))
SimpleVerticalWritingStyle = '''html {
-epub-writing-mode: vertical-rl;
direction: ltr;
unicode-bidi:bidi-override;
}
ol {
list-style-type: none;
padding-top: 0.5em;
padding-left: 1em;
}
ol ol {
list-style-type: none;
padding-top: 1em;
}
h2, h3, h4, h5, h6 {
font-size: medium;
}
p {
margin: 0;
line-height: 140%;
}
body {
margin: 0;
padding: 0;
}
'''