walterdavis-eeepub 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.md +76 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/examples/files/bar.html +42 -0
- data/examples/files/foo.html +42 -0
- data/examples/simple_epub.rb +26 -0
- data/lib/eeepub/container_item.rb +108 -0
- data/lib/eeepub/easy.rb +101 -0
- data/lib/eeepub/maker.rb +127 -0
- data/lib/eeepub/ncx.rb +68 -0
- data/lib/eeepub/ocf.rb +107 -0
- data/lib/eeepub/opf.rb +149 -0
- data/lib/eeepub.rb +15 -0
- data/spec/eeepub/easy_spec.rb +67 -0
- data/spec/eeepub/maker_spec.rb +129 -0
- data/spec/eeepub/ncx_spec.rb +80 -0
- data/spec/eeepub/ocf_spec.rb +43 -0
- data/spec/eeepub/opf_spec.rb +265 -0
- data/spec/eeepub_spec.rb +4 -0
- data/spec/spec_helper.rb +12 -0
- metadata +137 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 jugyo
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
EeePub
|
2
|
+
======
|
3
|
+
|
4
|
+
EeePub is a Ruby ePub generator.
|
5
|
+
|
6
|
+
Usage
|
7
|
+
-------
|
8
|
+
|
9
|
+
epub = EeePub.make do
|
10
|
+
title 'sample'
|
11
|
+
creator 'jugyo'
|
12
|
+
publisher 'jugyo.org'
|
13
|
+
date '2010-05-06'
|
14
|
+
identifier 'http://example.com/book/foo', :scheme => 'URL'
|
15
|
+
uid 'http://example.com/book/foo'
|
16
|
+
|
17
|
+
files ['/path/to/foo.html', '/path/to/bar.html'] # or files [{'/path/to/foo.html' => 'dest/dir'}, {'/path/to/bar.html' => 'dest/dir'}]
|
18
|
+
nav [
|
19
|
+
{:label => '1. foo', :content => 'foo.html', :nav => [
|
20
|
+
{:label => '1.1 foo-1', :content => 'foo.html#foo-1'}
|
21
|
+
]},
|
22
|
+
{:label => '1. bar', :content => 'bar.html'}
|
23
|
+
]
|
24
|
+
end
|
25
|
+
epub.save('sample.epub')
|
26
|
+
|
27
|
+
### Low Level API
|
28
|
+
|
29
|
+
Create NCX:
|
30
|
+
|
31
|
+
EeePub::NCX.new(
|
32
|
+
:uid => 'xxxx',
|
33
|
+
:title => 'sample',
|
34
|
+
:nav => [
|
35
|
+
{:label => '1. foo', :content => 'foo.html'},
|
36
|
+
{:label => '2. bar', :content => 'bar.html'}
|
37
|
+
]
|
38
|
+
).save(File.join('sample', 'toc.ncx'))
|
39
|
+
|
40
|
+
Create OPF:
|
41
|
+
|
42
|
+
EeePub::OPF.new(
|
43
|
+
:title => 'sample',
|
44
|
+
:identifier => {:value => '0-0000000-0-0', :scheme => 'ISBN'},
|
45
|
+
:manifest => ['foo.html', 'bar.html'],
|
46
|
+
:ncx => 'toc.ncx'
|
47
|
+
).save(File.join('sample', 'content.opf'))
|
48
|
+
|
49
|
+
Create OCF and ePub file:
|
50
|
+
|
51
|
+
EeePub::OCF.new(
|
52
|
+
:dir => 'sample',
|
53
|
+
:container => 'content.opf'
|
54
|
+
).save('sample.epub')
|
55
|
+
|
56
|
+
Install
|
57
|
+
-------
|
58
|
+
|
59
|
+
gem install eeepub
|
60
|
+
|
61
|
+
Requirements
|
62
|
+
-------
|
63
|
+
|
64
|
+
* builder
|
65
|
+
* eBook Reader :)
|
66
|
+
|
67
|
+
Links
|
68
|
+
-------
|
69
|
+
|
70
|
+
* Documentation: [http://yardoc.org/docs/jugyo-eeepub](http://yardoc.org/docs/jugyo-eeepub)
|
71
|
+
* Source code: [http://github.com/jugyo/eeepub](http://github.com/jugyo/eeepub)
|
72
|
+
|
73
|
+
Copyright
|
74
|
+
-------
|
75
|
+
|
76
|
+
Copyright (c) 2010 jugyo. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "walterdavis-eeepub"
|
8
|
+
gem.summary = %Q{ePub generator}
|
9
|
+
gem.description = %Q{EeePub is a Ruby ePub generator. This version is forked from the original https://github.com/jugyo/eeepub}
|
10
|
+
gem.email = "waltd@wdstudio.com"
|
11
|
+
gem.homepage = "http://github.com/walterdavis/eeepub"
|
12
|
+
gem.authors = ["jugyo", "walterdavis"]
|
13
|
+
gem.add_dependency "builder"
|
14
|
+
gem.add_development_dependency "rspec"
|
15
|
+
gem.add_development_dependency "rr"
|
16
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
17
|
+
end
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
begin
|
39
|
+
require 'yard'
|
40
|
+
YARD::Rake::YardocTask.new
|
41
|
+
rescue LoadError
|
42
|
+
task :yardoc do
|
43
|
+
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
|
44
|
+
end
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.6.2
|
@@ -0,0 +1,42 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
3
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">
|
4
|
+
<head>
|
5
|
+
<title></title>
|
6
|
+
</head>
|
7
|
+
<body>
|
8
|
+
<h1>bar</h1>
|
9
|
+
|
10
|
+
<p>foo foo foo foo</p>
|
11
|
+
<p>bar bar bar bar</p>
|
12
|
+
<p>foo foo foo foo</p>
|
13
|
+
<p>bar bar bar bar</p>
|
14
|
+
<p>foo foo foo foo</p>
|
15
|
+
<p>bar bar bar bar</p>
|
16
|
+
<p>foo foo foo foo</p>
|
17
|
+
<p>bar bar bar bar</p>
|
18
|
+
<p>foo foo foo foo</p>
|
19
|
+
<p>bar bar bar bar</p>
|
20
|
+
<p>foo foo foo foo</p>
|
21
|
+
<p>bar bar bar bar</p>
|
22
|
+
<p>foo foo foo foo</p>
|
23
|
+
<p>bar bar bar bar</p>
|
24
|
+
|
25
|
+
<h2 id="bar-1">bar-1</h2>
|
26
|
+
|
27
|
+
<p>foo foo foo foo</p>
|
28
|
+
<p>bar bar bar bar</p>
|
29
|
+
<p>foo foo foo foo</p>
|
30
|
+
<p>bar bar bar bar</p>
|
31
|
+
<p>foo foo foo foo</p>
|
32
|
+
<p>bar bar bar bar</p>
|
33
|
+
<p>foo foo foo foo</p>
|
34
|
+
<p>bar bar bar bar</p>
|
35
|
+
<p>foo foo foo foo</p>
|
36
|
+
<p>bar bar bar bar</p>
|
37
|
+
<p>foo foo foo foo</p>
|
38
|
+
<p>bar bar bar bar</p>
|
39
|
+
<p>foo foo foo foo</p>
|
40
|
+
<p>bar bar bar bar</p>
|
41
|
+
</body>
|
42
|
+
</html>
|
@@ -0,0 +1,42 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
3
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">
|
4
|
+
<head>
|
5
|
+
<title></title>
|
6
|
+
</head>
|
7
|
+
<body>
|
8
|
+
<h1>foo</h1>
|
9
|
+
|
10
|
+
<p>foo foo foo foo</p>
|
11
|
+
<p>bar bar bar bar</p>
|
12
|
+
<p>foo foo foo foo</p>
|
13
|
+
<p>bar bar bar bar</p>
|
14
|
+
<p>foo foo foo foo</p>
|
15
|
+
<p>bar bar bar bar</p>
|
16
|
+
<p>foo foo foo foo</p>
|
17
|
+
<p>bar bar bar bar</p>
|
18
|
+
<p>foo foo foo foo</p>
|
19
|
+
<p>bar bar bar bar</p>
|
20
|
+
<p>foo foo foo foo</p>
|
21
|
+
<p>bar bar bar bar</p>
|
22
|
+
<p>foo foo foo foo</p>
|
23
|
+
<p>bar bar bar bar</p>
|
24
|
+
|
25
|
+
<h2 id="foo-1">foo-1</h2>
|
26
|
+
|
27
|
+
<p>foo foo foo foo</p>
|
28
|
+
<p>bar bar bar bar</p>
|
29
|
+
<p>foo foo foo foo</p>
|
30
|
+
<p>bar bar bar bar</p>
|
31
|
+
<p>foo foo foo foo</p>
|
32
|
+
<p>bar bar bar bar</p>
|
33
|
+
<p>foo foo foo foo</p>
|
34
|
+
<p>bar bar bar bar</p>
|
35
|
+
<p>foo foo foo foo</p>
|
36
|
+
<p>bar bar bar bar</p>
|
37
|
+
<p>foo foo foo foo</p>
|
38
|
+
<p>bar bar bar bar</p>
|
39
|
+
<p>foo foo foo foo</p>
|
40
|
+
<p>bar bar bar bar</p>
|
41
|
+
</body>
|
42
|
+
</html>
|
@@ -0,0 +1,26 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
2
|
+
require 'rubygems'
|
3
|
+
require 'eeepub'
|
4
|
+
|
5
|
+
dir = File.join(File.dirname(__FILE__), 'files')
|
6
|
+
|
7
|
+
epub = EeePub.make do
|
8
|
+
title 'sample'
|
9
|
+
creator 'jugyo'
|
10
|
+
publisher 'jugyo.org'
|
11
|
+
author 'foo bar'
|
12
|
+
date '2010-05-06'
|
13
|
+
identifier 'http://example.com/book/foo', :scheme => 'URL'
|
14
|
+
uid 'http://example.com/book/foo'
|
15
|
+
|
16
|
+
files [File.join(dir, 'foo.html'), File.join(dir, 'bar.html')]
|
17
|
+
nav [
|
18
|
+
{:label => '1. foo', :content => 'foo.html', :nav => [
|
19
|
+
{:label => '1.1 foo-1', :content => 'foo.html#foo-1'}
|
20
|
+
]},
|
21
|
+
{:label => '1. bar', :content => 'bar.html'}
|
22
|
+
]
|
23
|
+
end
|
24
|
+
epub.save('sample.epub')
|
25
|
+
|
26
|
+
puts "complete! => 'sample.epub'"
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'builder'
|
2
|
+
|
3
|
+
module EeePub
|
4
|
+
# Abstract base class for container item of ePub. Provides some helper methods.
|
5
|
+
#
|
6
|
+
# @abstract
|
7
|
+
class ContainerItem
|
8
|
+
class << self
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
# Set default value to attribute
|
13
|
+
#
|
14
|
+
# @param [Symbol] name the attribute name
|
15
|
+
# @param [Object] default the default value
|
16
|
+
def default_value(name, default)
|
17
|
+
instance_variable_name = "@#{name}"
|
18
|
+
define_method(name) do
|
19
|
+
self.instance_variable_get(instance_variable_name) ||
|
20
|
+
self.instance_variable_set(instance_variable_name, default)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Define alias of attribute accessor
|
25
|
+
#
|
26
|
+
# @param [Symbol] name the attribute name as alias
|
27
|
+
# @param [Symbol] name the attribute name as source
|
28
|
+
def attr_alias(name, src)
|
29
|
+
alias_method name, src
|
30
|
+
alias_method :"#{name}=", :"#{src}="
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# @param [Hash<Symbol, Object>] values the hash of symbols and objects for attributes
|
35
|
+
def initialize(values)
|
36
|
+
set_values(values)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Set values for attributes
|
40
|
+
#
|
41
|
+
# @param [Hash<Symbol, Object>] values the hash of symbols and objects for attributes
|
42
|
+
def set_values(values)
|
43
|
+
values.each do |k, v|
|
44
|
+
self.send(:"#{k}=", v)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Convert to xml of container item
|
49
|
+
#
|
50
|
+
# @return [String] the xml of container item
|
51
|
+
def to_xml
|
52
|
+
out = ""
|
53
|
+
builder = Builder::XmlMarkup.new(:target => out, :indent => 2)
|
54
|
+
builder.instruct!
|
55
|
+
build_xml(builder)
|
56
|
+
out
|
57
|
+
end
|
58
|
+
|
59
|
+
# Save as container item
|
60
|
+
#
|
61
|
+
# @param [String] filepath the file path for container item
|
62
|
+
def save(filepath)
|
63
|
+
File.open(filepath, 'w') do |file|
|
64
|
+
file << self.to_xml
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
# Guess media type from file name
|
71
|
+
#
|
72
|
+
# @param [String] filename the file name
|
73
|
+
# @return [String] the media-type
|
74
|
+
def guess_media_type(filename)
|
75
|
+
case filename
|
76
|
+
when /.*\.html?$/i
|
77
|
+
'application/xhtml+xml'
|
78
|
+
when /.*\.css$/i
|
79
|
+
'text/css'
|
80
|
+
when /.*\.(jpeg|jpg)$/
|
81
|
+
'image/jpeg'
|
82
|
+
when /.*\.png$/i
|
83
|
+
'image/png'
|
84
|
+
when /.*\.gif$/i
|
85
|
+
'image/gif'
|
86
|
+
when /.*\.svg$/i
|
87
|
+
'image/svg+xml'
|
88
|
+
when /.*\.ncx$/i
|
89
|
+
'application/x-dtbncx+xml'
|
90
|
+
when /.*\.opf$/i
|
91
|
+
'application/oebps-package+xml'
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# Convert options for xml attributes
|
96
|
+
#
|
97
|
+
# @param [Hash<Symbol, Object>] hash the hash of symbols and objects for xml attributes
|
98
|
+
# @return [Hash<String, Object>] the options for xml attributes
|
99
|
+
def convert_to_xml_attributes(hash)
|
100
|
+
result = {}
|
101
|
+
hash.each do |k, v|
|
102
|
+
key = k.to_s.gsub('_', '-').to_sym
|
103
|
+
result[key] = v
|
104
|
+
end
|
105
|
+
result
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
data/lib/eeepub/easy.rb
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'tmpdir'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
module EeePub
|
5
|
+
# The class to make ePub more easily
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# epub = EeePub::Easy.new do
|
9
|
+
# title 'sample'
|
10
|
+
# creator 'jugyo'
|
11
|
+
# identifier 'http://example.com/book/foo', :scheme => 'URL'
|
12
|
+
# uid 'http://example.com/book/foo'
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# epub.sections << ['1. foo', <<HTML]
|
16
|
+
# <?xml version="1.0" encoding="UTF-8"?>
|
17
|
+
# <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
18
|
+
# <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">
|
19
|
+
# <head>
|
20
|
+
# <title>foo</title>
|
21
|
+
# </head>
|
22
|
+
# <body>
|
23
|
+
# <p>
|
24
|
+
# foo foo foo foo foo foo
|
25
|
+
# </p>
|
26
|
+
# </body>
|
27
|
+
# </html>
|
28
|
+
# HTML
|
29
|
+
#
|
30
|
+
# epub.assets << 'image.png'
|
31
|
+
#
|
32
|
+
# epub.save('sample.epub')
|
33
|
+
class Easy < EeePub::Maker
|
34
|
+
attr_reader :sections, :assets
|
35
|
+
|
36
|
+
# @param [Proc] block the block for initialize
|
37
|
+
def initialize(&block)
|
38
|
+
@sections = []
|
39
|
+
@assets = []
|
40
|
+
super
|
41
|
+
end
|
42
|
+
|
43
|
+
# Save as ePub file
|
44
|
+
#
|
45
|
+
# @param [String] filename the ePub file name to save
|
46
|
+
def save(filename)
|
47
|
+
Dir.mktmpdir do |dir|
|
48
|
+
prepare(dir)
|
49
|
+
|
50
|
+
NCX.new(
|
51
|
+
:uid => @uid,
|
52
|
+
:title => @titles[0],
|
53
|
+
:nav => @nav
|
54
|
+
).save(File.join(dir, @ncx_file))
|
55
|
+
|
56
|
+
OPF.new(
|
57
|
+
:title => @titles,
|
58
|
+
:identifier => @identifiers,
|
59
|
+
:creator => @creators,
|
60
|
+
:author => @authors,
|
61
|
+
:publisher => @publishers,
|
62
|
+
:date => @dates,
|
63
|
+
:language => @languages,
|
64
|
+
:subject => @subjects,
|
65
|
+
:description => @descriptions,
|
66
|
+
:rights => @rightss,
|
67
|
+
:relation => @relations,
|
68
|
+
:manifest => @files.map{|i| File.basename(i)},
|
69
|
+
:ncx => @ncx_file
|
70
|
+
).save(File.join(dir, @opf_file))
|
71
|
+
|
72
|
+
OCF.new(
|
73
|
+
:dir => dir,
|
74
|
+
:container => @opf_file
|
75
|
+
).save(filename)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def prepare(dir)
|
82
|
+
filenames = []
|
83
|
+
sections.each_with_index do |section, index|
|
84
|
+
filename = File.join(dir, "section_#{index}.html")
|
85
|
+
File.open(filename, 'w') { |file| file.write section[1] }
|
86
|
+
filenames << filename
|
87
|
+
end
|
88
|
+
|
89
|
+
assets.each do |file|
|
90
|
+
FileUtils.cp(file, dir)
|
91
|
+
end
|
92
|
+
|
93
|
+
files(filenames + assets)
|
94
|
+
nav(
|
95
|
+
[sections, filenames].transpose.map do |section, filename|
|
96
|
+
{:label => section[0], :content => File.basename(filename)}
|
97
|
+
end
|
98
|
+
)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
data/lib/eeepub/maker.rb
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
require 'tmpdir'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
module EeePub
|
5
|
+
# The class to make ePub easily
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# epub = EeePub.make do
|
9
|
+
# title 'sample'
|
10
|
+
# creator 'jugyo'
|
11
|
+
# publisher 'jugyo.org'
|
12
|
+
# date '2010-05-06'
|
13
|
+
# identifier 'http://example.com/book/foo', :scheme => 'URL'
|
14
|
+
# uid 'http://example.com/book/foo'
|
15
|
+
#
|
16
|
+
# files ['/path/to/foo.html', '/path/to/bar.html']
|
17
|
+
# nav [
|
18
|
+
# {:label => '1. foo', :content => 'foo.html', :nav => [
|
19
|
+
# {:label => '1.1 foo-1', :content => 'foo.html#foo-1'}
|
20
|
+
# ]},
|
21
|
+
# {:label => '1. bar', :content => 'bar.html'}
|
22
|
+
# ]
|
23
|
+
# end
|
24
|
+
# epub.save('sample.epub')
|
25
|
+
class Maker
|
26
|
+
[
|
27
|
+
:title,
|
28
|
+
:creator,
|
29
|
+
:author,
|
30
|
+
:publisher,
|
31
|
+
:date,
|
32
|
+
:language,
|
33
|
+
:subject,
|
34
|
+
:description,
|
35
|
+
:rights,
|
36
|
+
:relation
|
37
|
+
].each do |name|
|
38
|
+
class_eval <<-DELIM
|
39
|
+
def #{name}(value)
|
40
|
+
@#{name}s ||= []
|
41
|
+
@#{name}s << value
|
42
|
+
end
|
43
|
+
DELIM
|
44
|
+
end
|
45
|
+
|
46
|
+
[
|
47
|
+
:uid,
|
48
|
+
:files,
|
49
|
+
:nav,
|
50
|
+
:ncx_file,
|
51
|
+
:opf_file
|
52
|
+
].each do |name|
|
53
|
+
define_method(name) do |arg|
|
54
|
+
instance_variable_set("@#{name}", arg)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def identifier(id, options)
|
59
|
+
@identifiers ||= []
|
60
|
+
@identifiers << {:value => id, :scheme => options[:scheme]}
|
61
|
+
end
|
62
|
+
|
63
|
+
# @param [Proc] block the block for initialize
|
64
|
+
def initialize(&block)
|
65
|
+
@files ||= []
|
66
|
+
@nav ||= []
|
67
|
+
@ncx_file ||= 'toc.ncx'
|
68
|
+
@opf_file ||= 'content.opf'
|
69
|
+
|
70
|
+
instance_eval(&block) if block_given?
|
71
|
+
end
|
72
|
+
|
73
|
+
# Save as ePub file
|
74
|
+
#
|
75
|
+
# @param [String] filename the ePub file name to save
|
76
|
+
def save(filename)
|
77
|
+
Dir.mktmpdir do |dir|
|
78
|
+
@files.each do |file|
|
79
|
+
case file
|
80
|
+
when String
|
81
|
+
FileUtils.cp(file, dir)
|
82
|
+
when Hash
|
83
|
+
file_path, dir_path = *file.first
|
84
|
+
dest_dir = File.join(dir, dir_path)
|
85
|
+
FileUtils.mkdir_p(dest_dir)
|
86
|
+
FileUtils.cp(file_path, dest_dir)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
NCX.new(
|
91
|
+
:uid => @uid,
|
92
|
+
:title => @titles[0],
|
93
|
+
:nav => @nav
|
94
|
+
).save(File.join(dir, @ncx_file))
|
95
|
+
|
96
|
+
OPF.new(
|
97
|
+
:title => @titles,
|
98
|
+
:identifier => @identifiers,
|
99
|
+
:creator => @creators,
|
100
|
+
:author => @authors,
|
101
|
+
:publisher => @publishers,
|
102
|
+
:date => @dates,
|
103
|
+
:language => @languages,
|
104
|
+
:subject => @subjects,
|
105
|
+
:description => @descriptions,
|
106
|
+
:rights => @rightss,
|
107
|
+
:relation => @relations,
|
108
|
+
:manifest => @files.map{|file|
|
109
|
+
case file
|
110
|
+
when String
|
111
|
+
File.basename(file)
|
112
|
+
when Hash
|
113
|
+
file_path, dir_path = *file.first
|
114
|
+
File.join(dir_path, File.basename(file_path))
|
115
|
+
end
|
116
|
+
},
|
117
|
+
:ncx => @ncx_file
|
118
|
+
).save(File.join(dir, @opf_file))
|
119
|
+
|
120
|
+
OCF.new(
|
121
|
+
:dir => dir,
|
122
|
+
:container => @opf_file
|
123
|
+
).save(filename)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
data/lib/eeepub/ncx.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
module EeePub
|
2
|
+
class NCX < ContainerItem
|
3
|
+
attr_accessor :uid,
|
4
|
+
:depth,
|
5
|
+
:total_page_count,
|
6
|
+
:max_page_number,
|
7
|
+
:doc_title,
|
8
|
+
:nav_map
|
9
|
+
|
10
|
+
attr_alias :title, :doc_title
|
11
|
+
attr_alias :nav, :nav_map
|
12
|
+
|
13
|
+
default_value :depth, 1
|
14
|
+
default_value :total_page_count, 0
|
15
|
+
default_value :max_page_number, 0
|
16
|
+
default_value :doc_title, 'Untitled'
|
17
|
+
|
18
|
+
def build_xml(builder)
|
19
|
+
builder.declare! :DOCTYPE, :ncx, :PUBLIC, "-//NISO//DTD ncx 2005-1//EN", "http://www.daisy.org/z3986/2005/ncx-2005-1.dtd"
|
20
|
+
builder.ncx :xmlns => "http://www.daisy.org/z3986/2005/ncx/", :version => "2005-1" do
|
21
|
+
build_head(builder)
|
22
|
+
builder.docTitle { builder.text doc_title }
|
23
|
+
build_nav_map(builder)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def build_head(builder)
|
28
|
+
builder.head do
|
29
|
+
{
|
30
|
+
:uid => uid,
|
31
|
+
:depth => depth,
|
32
|
+
:totalPageCount => total_page_count,
|
33
|
+
:maxPageNumber => max_page_number
|
34
|
+
}.each do |k, v|
|
35
|
+
builder.meta :name => "dtb:#{k}", :content => v
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def build_nav_map(builder)
|
41
|
+
builder.navMap do
|
42
|
+
builder_nav_point(builder, nav_map)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def builder_nav_point(builder, nav_point, play_order = 1)
|
47
|
+
case nav_point
|
48
|
+
when Array
|
49
|
+
nav_point.each do |point|
|
50
|
+
play_order = builder_nav_point(builder, point, play_order)
|
51
|
+
end
|
52
|
+
when Hash
|
53
|
+
id = nav_point[:id] || "navPoint-#{play_order}"
|
54
|
+
builder.navPoint :id => id, :playOrder => play_order do
|
55
|
+
builder.navLabel { builder.text nav_point[:label] }
|
56
|
+
builder.content :src => nav_point[:content]
|
57
|
+
play_order += 1
|
58
|
+
if nav_point[:nav]
|
59
|
+
play_order = builder_nav_point(builder, nav_point[:nav], play_order)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
else
|
63
|
+
raise "nav_point must be Array or Hash"
|
64
|
+
end
|
65
|
+
play_order
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|