eeepub-with-cover-support 0.8.6

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ doc
5
+ pkg
6
+ *.epub
7
+ .rvmrc
8
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in eeepub.gemspec
4
+ gemspec
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,22 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ begin
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new do |t|
7
+ t.rspec_opts = ['--color']
8
+ end
9
+ rescue LoadError => e
10
+ puts "RSpec not installed"
11
+ end
12
+
13
+ task :default => :spec
14
+
15
+ begin
16
+ require 'yard'
17
+ YARD::Rake::YardocTask.new
18
+ rescue LoadError
19
+ task :yardoc do
20
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
21
+ end
22
+ end
data/eeepub.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "eeepub-with-cover-support"
5
+ s.version = "0.8.6"
6
+ s.platform = Gem::Platform::RUBY
7
+ s.authors = ["jugyo"]
8
+ s.email = ["jugyo.org@gmail.com"]
9
+ s.homepage = "http://github.com/jugyo/eeepub"
10
+ s.summary = %q{ePub generator}
11
+ s.description = %q{EeePub is a Ruby ePub generator.}
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.require_paths = ["lib"]
17
+
18
+ s.add_dependency "builder"
19
+ s.add_dependency "rubyzip"
20
+ s.add_development_dependency "rspec"
21
+ s.add_development_dependency "nokogiri"
22
+ s.add_development_dependency "rr"
23
+ s.add_development_dependency "simplecov"
24
+ end
@@ -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>
Binary file
@@ -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,22 @@
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 xml:lang='en' xmlns='http://www.w3.org/1999/xhtml'>
4
+ <head>
5
+ <meta content='application/xhtml+xml; charset=utf-8' http-equiv='Content-Type' />
6
+ <title>Table of Contents</title>
7
+ </head>
8
+ <body>
9
+ <div id='toc'>
10
+ <ul>
11
+ <li>
12
+ <a href='foo.html'>Chapter 1</a>
13
+ </li>
14
+ </ul>
15
+ <ul>
16
+ <li>
17
+ <a href='bar.html'>Chapter 2</a>
18
+ </li>
19
+ </ul>
20
+ </div>
21
+ </body>
22
+ </html>
@@ -0,0 +1,27 @@
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
+ date '2010-05-06'
12
+ uid 'BookId'
13
+ identifier 'http://example.com/book/foo', :scheme => 'URL', :id => 'BookId'
14
+
15
+ files [File.join(dir, 'foo.html'), File.join(dir, 'bar.html')]
16
+ nav [
17
+ {:label => '1. foo', :content => 'foo.html', :nav => [
18
+ {:label => '1.1 foo-1', :content => 'foo.html#foo-1'}
19
+ ]},
20
+ {:label => '1. bar', :content => 'bar.html'}
21
+ ]
22
+ cover_page File.join(dir, 'cover.jpg')
23
+ toc_page File.join(dir, 'toc.html')
24
+ end
25
+ epub.save('sample.epub')
26
+
27
+ puts "complete! => 'sample.epub'"
data/lib/eeepub.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'eeepub/container_item'
2
+ require 'eeepub/opf'
3
+ require 'eeepub/ocf'
4
+ require 'eeepub/ncx'
5
+ require 'eeepub/maker'
6
+ require 'eeepub/easy'
7
+
8
+ module EeePub
9
+ # Make ePub
10
+ #
11
+ # @param [Proc] block the block for initialize EeePub::Maker
12
+ def self.make(&block)
13
+ EeePub::Maker.new(&block)
14
+ end
15
+ end
@@ -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 /.*\.x?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
@@ -0,0 +1,100 @@
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
+ :publisher => @publishers,
61
+ :date => @dates,
62
+ :language => @languages,
63
+ :subject => @subjects,
64
+ :description => @descriptions,
65
+ :rights => @rightss,
66
+ :relation => @relations,
67
+ :manifest => @files.map{|i| File.basename(i)},
68
+ :ncx => @ncx_file
69
+ ).save(File.join(dir, @opf_file))
70
+
71
+ OCF.new(
72
+ :dir => dir,
73
+ :container => @opf_file
74
+ ).save(filename)
75
+ end
76
+ end
77
+
78
+ private
79
+
80
+ def prepare(dir)
81
+ filenames = []
82
+ sections.each_with_index do |section, index|
83
+ filename = File.join(dir, "section_#{index}.html")
84
+ File.open(filename, 'w') { |file| file.write section[1] }
85
+ filenames << filename
86
+ end
87
+
88
+ assets.each do |file|
89
+ FileUtils.cp(file, dir)
90
+ end
91
+
92
+ files(filenames + assets)
93
+ nav(
94
+ [sections, filenames].transpose.map do |section, filename|
95
+ {:label => section[0], :content => File.basename(filename)}
96
+ end
97
+ )
98
+ end
99
+ end
100
+ end