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
|
# File 'lib/asciibook/builders/epub_builder.rb', line 10
def build
FileUtils.mkdir_p @dest_dir
layout = Liquid::Template.parse(File.read(File.join(@theme_dir, 'layout.html')))
@book.pages.each do |page|
page.path = page.path.gsub(/.html$/, '.xhtml')
end
epub = GEPUB::Book.new do |book|
book.title = @book.title
book.identifier = @book.doc.attributes['identifier'] || 'undefined'
book.language = @book.doc.attributes['language'] || 'en'
id_pool = GEPUB::Package::IDPool.new
@book.assets.each do |path|
book.add_item path, content: File.open(File.join(@book.base_dir, path)), id: id_pool.generate_key(prefix: 'asset_')
end
if @book.cover_image_path
book.add_item(@book.cover_image_path, content: File.open(File.join(@book.base_dir, @book.cover_image_path)), id: 'cover_image').cover_image
end
Dir.glob('**/*.{jpb,png,gif,svg,css,js,eot,ttf,woff,woff2}', File::FNM_CASEFOLD, base: @theme_share_dir).each do |path|
book.add_item path, content: File.open(File.join(@theme_share_dir, path)), id: id_pool.generate_key(prefix: 'theme_asset_')
end
Dir.glob('**/*.{jpb,png,gif,svg,css,js,eot,ttf,woff,woff2}', File::FNM_CASEFOLD, base: @theme_dir).each do |path|
book.add_item path, content: File.open(File.join(@theme_dir, path)), id: id_pool.generate_key(prefix: 'theme_asset_')
end
book.ordered do
@book.pages.each do |page|
html = layout.render(
'book' => @book.to_hash,
'page' => page.to_hash
)
book.add_item page.path, content: StringIO.new(html), id: id_pool.generate_key(prefix: 'page_')
end
end
book.add_tocdata tocdata
end
epub.generate_epub(File.join(@dest_dir, "#{@book.basename}.epub"))
@book.pages.each do |page|
page.path = page.path.gsub(/.xhtml$/, '.html')
end
end
|