-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_epub.rb
81 lines (72 loc) · 2.52 KB
/
create_epub.rb
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
# frozen_string_literal: true
require 'gepub'
load 'utils.rb'
def get_content(body, title)
'<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<link href="../Styles/style.css" rel="stylesheet" type="text/css"/>
<title>' + title + '</title>
</head>
<body class="chapter">' +
body +
'</body>
</html>'
end
def generate_epub(url, title, author, contributors, chapters)
book = GEPUB::Book.new
book.identifier = url
book.title = title
book.creator = author
contributors.each { |contributor| book.add_contributor contributor }
book.language = 'zh'
File.open 'style.css', mode: 'rb', encoding: 'utf-8' do |css|
book.add_item('Styles/style.css', content: css)
end
File.open get_file_path(title, 'png'), mode: 'rb' do |png|
book.add_item('Images/cover.png', content: png).cover_image
end
book.ordered do
book.add_item('Text/cover.xhtml', content: StringIO.new(<<~COVER.chomp)).landmark(type: 'cover', title: '封面')
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<link href="../Styles/style.css" rel="stylesheet" type="text/css"/>
<title>封面</title>
</head>
<body class="center">
<img alt="cover" src="../Images/cover.png"/>
</body>
</html>
COVER
book.add_item('Text/message.xhtml').add_content(StringIO.new(<<~MESSAGE.chomp))
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<link href="../Styles/style.css" rel="stylesheet" type="text/css"/>
<title>制作信息</title>
</head>
<body>
<p>仅供个人学习交流使用,禁作商业用途。</p>
<br>
<div>
<p>古诗文网 gushiwen.cn</p>
<p>制 作 陈 刑</p>
</div>
</body>
</html>
MESSAGE
chapters.each_with_index do |chapter, index|
item_href = "Text/#{index}.xhtml"
chapter_title = chapter[0]
content = get_content(chapter[1], chapter_title)
book.add_item(item_href).add_content(StringIO.new(content))
end
end
file = get_file_path(title, 'epub')
book.generate_epub(file)
puts "#{file} created"
end