sudothinker-eeepub 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
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.
@@ -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.
@@ -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 = "sudothinker-eeepub"
8
+ gem.summary = %Q{ePub generator}
9
+ gem.description = %Q{EeePub is a Ruby ePub generator.}
10
+ gem.email = "jugyo.org@gmail.com"
11
+ gem.homepage = "http://github.com/jugyo/eeepub"
12
+ gem.authors = ["jugyo", "sudothinker"]
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.1
@@ -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,25 @@
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
+ identifier 'http://example.com/book/foo', :scheme => 'URL'
13
+ uid 'http://example.com/book/foo'
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
+ end
23
+ epub.save('sample.epub')
24
+
25
+ puts "complete! => 'sample.epub'"
@@ -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 /.*\.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,108 @@
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
+ # epub.urls << ["http://path/to/image.png", "image.png"]
32
+ #
33
+ # epub.save('sample.epub')
34
+ class Easy < EeePub::Maker
35
+ attr_reader :sections, :assets, :urls
36
+
37
+ # @param [Proc] block the block for initialize
38
+ def initialize(&block)
39
+ @sections = []
40
+ @assets = []
41
+ @urls = []
42
+ super
43
+ end
44
+
45
+ # Save as ePub file
46
+ #
47
+ # @param [String] filename the ePub file name to save
48
+ def save(filename)
49
+ Dir.mktmpdir do |dir|
50
+ prepare(dir)
51
+
52
+ NCX.new(
53
+ :uid => @uid,
54
+ :title => @titles[0],
55
+ :nav => @nav
56
+ ).save(File.join(dir, @ncx_file))
57
+
58
+ OPF.new(
59
+ :title => @titles,
60
+ :identifier => @identifiers,
61
+ :creator => @creators,
62
+ :publisher => @publishers,
63
+ :date => @dates,
64
+ :language => @languages,
65
+ :subject => @subjects,
66
+ :description => @descriptions,
67
+ :rights => @rightss,
68
+ :relation => @relations,
69
+ :manifest => @files.map{|i| File.basename(i)},
70
+ :ncx => @ncx_file
71
+ ).save(File.join(dir, @opf_file))
72
+
73
+ OCF.new(
74
+ :dir => dir,
75
+ :container => @opf_file
76
+ ).save(filename)
77
+ end
78
+ end
79
+
80
+ private
81
+
82
+ def prepare(dir)
83
+ filenames = []
84
+ sections.each_with_index do |section, index|
85
+ filename = File.join(dir, "section_#{index}.html")
86
+ File.open(filename, 'w') { |file| file.write section[1] }
87
+ filenames << filename
88
+ end
89
+
90
+ assets.each do |file|
91
+ FileUtils.cp(file, dir)
92
+ end
93
+
94
+ urls.each do |url|
95
+ File.open(File.join(dir, url[1]), "w") do |f|
96
+ f << open(url[0]).read
97
+ end
98
+ end
99
+
100
+ files(filenames + assets + urls.map{ |u| u[1] })
101
+ nav(
102
+ [sections, filenames].transpose.map do |section, filename|
103
+ {:label => section[0], :content => File.basename(filename)}
104
+ end
105
+ )
106
+ end
107
+ end
108
+ end