-
Notifications
You must be signed in to change notification settings - Fork 0
/
toepub.rb
66 lines (53 loc) · 1.73 KB
/
toepub.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
require 'gepub'
require 'erb'
require 'pry'
@manga = ARGV[0]
@chapter = ARGV[1]
@erb_file = 'epub_template/page.html.erb'
def generate_book(manga, chapter)
images = Dir.glob("mangas/#{manga}/#{chapter}/*.jpg")
base_path = "tmp/#{manga}/"
image_path = base_path + 'img'
text_path = base_path + 'text'
FileUtils.mkdir_p(image_path)
FileUtils.mkdir_p(text_path)
images.each do |image|
FileUtils.cp image, image_path
page = image.split('/').last.split('.').first
html_file = text_path + "/page_#{page}.xhtml"
@title = @manga
@image = image.split('/').last
@page = page
@text = "hello world"
renderer = ERB.new(File.read(@erb_file))
result = renderer.result()
File.open(html_file, 'w') { |f| f.write(result) }
end
end
generate_book(@manga, @chapter)
book = GEPUB::Book.new
book.set_primary_identifier('http:/example.jp/bookid_in_url', 'BookID', 'URL')
book.language = 'en'
book.add_title('test', nil, GEPUB::TITLE_TYPE::MAIN) {
|title|
title.lang = 'en'
title.file_as = 'GEPUB Sample Book'
title.display_seq = 1
title.add_alternates('en' => 'GEPUB Sample Book (Japanese)')
}
files = Dir.glob("tmp/mahou-tsukai-no-yome/text/*.xhtml")
images = Dir.glob("tmp/mahou-tsukai-no-yome/img/*.jpg")
prefix = 'tmp/mahou-tsukai-no-yome/'
# within ordered block, add_item will be added to spine.
book.ordered {
images.each do |image|
puts "adding image: #{image}"
book.add_item(image.gsub(prefix, '')).add_content(StringIO.new(File.open(image).read))
end
files.each do |file|
puts "adding file: #{file}"
book.add_item(file.gsub(prefix, '')).add_content(StringIO.new(File.open(file).read))
end
}
epubname = File.join(File.dirname(__FILE__), 'example_test.epub')
book.generate_epub(epubname)