eeepub 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ EeePub
2
+ ======
3
+
4
+ EeePub is a Ruby ePub generator.
5
+
6
+ Usage
7
+ -------
8
+
9
+ Create NCX:
10
+
11
+ EeePub::NCX.new(
12
+ :uid => 'xxxx',
13
+ :title => 'sample',
14
+ :nav => [
15
+ {:label => '1. foo', :content => 'foo.html'},
16
+ {:label => '2. bar', :content => 'bar.html'}
17
+ ]
18
+ ).save(File.join('sample', 'toc.ncx'))
19
+
20
+ Create OPF:
21
+
22
+ EeePub::OPF.new(
23
+ :title => 'sample',
24
+ :identifier => {'ISBN' => '0-0000000-0-0'},
25
+ :manifest => ['foo.html', 'bar.html'],
26
+ :ncx => 'toc.ncx'
27
+ ).save(File.join('sample', 'content.opf'))
28
+
29
+ Create OCF and ePub file:
30
+
31
+ EeePub::OCF.new(
32
+ :dir => 'sample',
33
+ :container => 'content.opf'
34
+ ).save('sample.epub')
35
+
36
+ Install
37
+ -------
38
+
39
+ gem install eeepub
40
+
41
+ Copyright
42
+ -------
43
+
44
+ Copyright (c) 2010 jugyo. See LICENSE for details.
data/Rakefile CHANGED
@@ -10,6 +10,7 @@ begin
10
10
  gem.email = "jugyo.org@gmail.com"
11
11
  gem.homepage = "http://github.com/jugyo/eeepub"
12
12
  gem.authors = ["jugyo"]
13
+ gem.add_dependency "builder"
13
14
  gem.add_development_dependency "rspec"
14
15
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
16
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
data/examples/simple.rb CHANGED
@@ -16,7 +16,10 @@ FileUtils.rm_f(epub_name)
16
16
  f << <<-HTML
17
17
  <?xml version="1.0" encoding="UTF-8"?>
18
18
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
19
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
19
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">
20
+ <head>
21
+ <title></title>
22
+ </head>
20
23
  <body>
21
24
  <h1>#{name}</h1>
22
25
  </body>
@@ -27,35 +30,24 @@ end
27
30
 
28
31
  # Create NCX
29
32
  EeePub::NCX.new(
30
- :uid => 'xxxx', :doc_title => 'simple',
31
- :nav_points => [
32
- {:id => 'nav-1', :play_order => '1', :label => '1. foo', :content => 'foo.html'},
33
- {:id => 'nav-2', :play_order => '2', :label => '2. bar', :content => 'bar.html'}
33
+ :uid => 'xxxx',
34
+ :title => 'simple',
35
+ :nav => [
36
+ {:label => '1. foo', :content => 'foo.html'},
37
+ {:label => '2. bar', :content => 'bar.html'}
34
38
  ]
35
39
  ).save(File.join(dir, 'toc.ncx'))
36
40
 
37
41
  # Create OPF
38
42
  EeePub::OPF.new(
39
43
  :title => 'simple',
40
- :language => 'ja',
41
- :identifier => 'xxxxxxx',
42
- :items => [
43
- {:id => 'ncx', :href => 'toc.ncx', :media_type => 'text/xml'},
44
- {:id => 'foo', :href => 'foo.html', :media_type => 'text/html'},
45
- {:id => 'bar', :href => 'bar.html', :media_type => 'text/html'},
46
- ],
47
- :itemrefs => [
48
- {:idref => 'foo'},
49
- {:idref => 'bar'}
50
- ]
51
- ).save(File.join(dir, 'package.opf'))
44
+ :identifier => {'ISBN' => '0-0000000-0-0'},
45
+ :manifest => ['foo.html', 'bar.html'],
46
+ :ncx => 'toc.ncx'
47
+ ).save(File.join(dir, 'content.opf'))
52
48
 
53
49
  # Create OCF
54
50
  EeePub::OCF.new(
55
51
  :dir => dir,
56
- :container => EeePub::OCF::Container.new(
57
- :rootfiles => [
58
- {:full_path => 'package.opf', :media_type => 'application/oebps-package+xml'}
59
- ]
60
- )
61
- ).make(epub_name)
52
+ :container => 'content.opf'
53
+ ).save(epub_name)
@@ -1,16 +1,22 @@
1
- require 'erb'
1
+ require 'erb' # TODO 消す
2
+ require 'builder'
2
3
 
3
4
  module EeePub
4
5
  class ContainerItem
5
6
  include ERB::Util
6
7
 
7
- TEMPLATE_DIR = File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
8
-
9
8
  class << self
10
- attr_reader :template_name
9
+ def default_value(name, default)
10
+ instance_variable_name = "@#{name}"
11
+ define_method(name) do
12
+ self.instance_variable_get(instance_variable_name) ||
13
+ self.instance_variable_set(instance_variable_name, default)
14
+ end
15
+ end
11
16
 
12
- def template(name)
13
- @template_name = name
17
+ def attr_alias(name, src)
18
+ alias_method name, src
19
+ alias_method :"#{name}=", :"#{src}="
14
20
  end
15
21
  end
16
22
 
@@ -25,7 +31,11 @@ module EeePub
25
31
  end
26
32
 
27
33
  def to_xml
28
- erb(self.class.template_name).result(binding)
34
+ out = ""
35
+ builder = Builder::XmlMarkup.new(:target => out, :indent => 2)
36
+ builder.instruct!
37
+ build_xml(builder)
38
+ out
29
39
  end
30
40
 
31
41
  def erb(filename)
@@ -41,5 +51,35 @@ module EeePub
41
51
  file << self.to_xml
42
52
  end
43
53
  end
54
+
55
+ def guess_media_type(filename)
56
+ case filename
57
+ when /.*\.html?$/i
58
+ 'application/xhtml+xml'
59
+ when /.*\.css$/i
60
+ 'text/css'
61
+ when /.*\.(jpeg|jpg)$/
62
+ 'image/jpeg'
63
+ when /.*\.png$/i
64
+ 'image/png'
65
+ when /.*\.gif$/i
66
+ 'image/gif'
67
+ when /.*\.svg$/i
68
+ 'image/svg+xml'
69
+ when /.*\.ncx$/i
70
+ 'application/x-dtbncx+xml'
71
+ when /.*\.opf$/i
72
+ 'application/oebps-package+xml'
73
+ end
74
+ end
75
+
76
+ def create_build_option(hash)
77
+ result = {}
78
+ hash.each do |k, v|
79
+ key = k.to_s.gsub('_', '-').to_sym
80
+ result[key] = v
81
+ end
82
+ result
83
+ end
44
84
  end
45
85
  end
data/lib/eeepub/ncx.rb CHANGED
@@ -1,18 +1,68 @@
1
1
  module EeePub
2
2
  class NCX < ContainerItem
3
- template 'ncx.erb'
4
3
  attr_accessor :uid,
5
4
  :depth,
6
5
  :total_page_count,
7
6
  :max_page_number,
8
7
  :doc_title,
9
- :nav_points
8
+ :nav_map
10
9
 
11
- def set_values(values)
12
- super
13
- @depth ||= 1
14
- @total_page_count ||= 0
15
- @max_page_number ||= 0
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
16
66
  end
17
67
  end
18
68
  end
data/lib/eeepub/ocf.rb CHANGED
@@ -1,8 +1,30 @@
1
1
  module EeePub
2
2
  class OCF
3
3
  class Container < ContainerItem
4
- template 'container.xml.erb'
5
4
  attr_accessor :rootfiles
5
+
6
+ def set_values(arg)
7
+ case arg
8
+ when String
9
+ super(
10
+ :rootfiles => [
11
+ {:full_path => arg, :media_type => guess_media_type(arg)}
12
+ ]
13
+ )
14
+ else
15
+ super
16
+ end
17
+ end
18
+
19
+ def build_xml(builder)
20
+ builder.container :xmlns => "urn:oasis:names:tc:opendocument:xmlns:container", :version => "1.0" do
21
+ builder.rootfiles do
22
+ rootfiles.each do |i|
23
+ builder.rootfile create_build_option(i)
24
+ end
25
+ end
26
+ end
27
+ end
6
28
  end
7
29
 
8
30
  attr_accessor :dir, :container
@@ -13,7 +35,16 @@ module EeePub
13
35
  end
14
36
  end
15
37
 
16
- def make(output_path)
38
+ def container=(arg)
39
+ case arg
40
+ when String
41
+ @container = EeePub::OCF::Container.new(arg)
42
+ else
43
+ @container = arg
44
+ end
45
+ end
46
+
47
+ def save(output_path)
17
48
  output_path = File.expand_path(output_path)
18
49
 
19
50
  FileUtils.chdir(dir) do
data/lib/eeepub/opf.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module EeePub
2
2
  class OPF < ContainerItem
3
- template 'opf.erb'
4
- attr_accessor :title,
3
+ attr_accessor :unique_identifier,
4
+ :title,
5
5
  :language,
6
6
  :identifier,
7
7
  :date,
@@ -11,7 +11,112 @@ module EeePub
11
11
  :creator,
12
12
  :publisher,
13
13
  :rights,
14
- :items,
15
- :itemrefs
14
+ :manifest,
15
+ :spine,
16
+ :ncx,
17
+ :toc
18
+
19
+ default_value :toc, 'ncx'
20
+ default_value :unique_identifier, 'BookId'
21
+ default_value :title, 'Untitled'
22
+ default_value :language, 'en'
23
+
24
+ attr_alias :files, :manifest
25
+
26
+ def identifier
27
+ case @identifier
28
+ when Array
29
+ @identifier
30
+ when String
31
+ [{:value => @identifier, :id => unique_identifier}]
32
+ when Hash
33
+ if @identifier.size == 1
34
+ key = @identifier.keys[0]
35
+ [{:scheme => key, :value => @identifier[key], :id => unique_identifier}]
36
+ else
37
+ @identifier[:id] = unique_identifier
38
+ [@identifier]
39
+ end
40
+ else
41
+ @identifier
42
+ end
43
+ end
44
+
45
+ def spine
46
+ unless @spine
47
+ items = manifest.map do |i|
48
+ case i
49
+ when String
50
+ id = i
51
+ media_type = guess_media_type(i)
52
+ when Hash
53
+ id = i[:id]
54
+ media_type = i[:media_type] || guess_media_type(i[:href])
55
+ end
56
+ media_type == 'application/xhtml+xml' ? id : nil
57
+ end
58
+ @spine = items.compact
59
+ end
60
+ @spine
61
+ end
62
+
63
+ def build_xml(builder)
64
+ builder.package :xmlns => "http://www.idpf.org/2007/opf",
65
+ 'unique-identifier' => unique_identifier,
66
+ 'version' => "2.0" do
67
+
68
+ build_metadata(builder)
69
+ build_manifest(builder)
70
+ build_spine(builder)
71
+ end
72
+ end
73
+
74
+ def build_metadata(builder)
75
+ builder.metadata 'xmlns:dc' => "http://purl.org/dc/elements/1.1/",
76
+ 'xmlns:dcterms' => "http://purl.org/dc/terms/",
77
+ 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
78
+ 'xmlns:opf' => "http://www.idpf.org/2007/opf" do
79
+
80
+ identifier.each do |i|
81
+ attrs = {}
82
+ attrs['opf:scheme'] = i[:scheme] if i[:scheme]
83
+ attrs[:id] = i[:id] if i[:id]
84
+ builder.dc :identifier, i[:value], attrs
85
+ end
86
+
87
+ [:title, :language, :subject, :description, :relation, :creator, :publisher, :date, :rights].each do |i|
88
+ value = self.send(i)
89
+ if value
90
+ [value].flatten.each do |v|
91
+ builder.dc i, v
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+
98
+ def build_manifest(builder)
99
+ items = manifest + [{:id => 'ncx', :href => ncx}]
100
+ builder.manifest do
101
+ items.each do |i|
102
+ case i
103
+ when Hash
104
+ builder.item :id => i[:id], :href => i[:href], 'media-type' => i[:media_type] || guess_media_type(i[:href])
105
+ when String
106
+ builder.item :id => i, :href => i, 'media-type' => guess_media_type(i)
107
+ else
108
+ raise 'manifest item must be Hash or String'
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ def build_spine(builder)
115
+ builder.spine :toc => toc do
116
+ spine.each do |i|
117
+ builder.itemref :idref => i
118
+ end
119
+ end
120
+ end
16
121
  end
17
122
  end
@@ -6,10 +6,10 @@ require 'fileutils'
6
6
  describe "EeePub::NCX" do
7
7
  before do
8
8
  @ncx = EeePub::NCX.new(
9
- :uid => 'uid', :doc_title => 'title',
10
- :nav_points => [
11
- {:id => 'nav-1', :play_order => '1', :label => 'foo', :content => 'foo.html'},
12
- {:id => 'nav-2', :play_order => '2', :label => 'bar', :content => 'bar.html'}
9
+ :uid => 'uid',
10
+ :nav_map => [
11
+ {:label => 'foo', :content => 'foo.html'},
12
+ {:label => 'bar', :content => 'bar.html'}
13
13
  ]
14
14
  )
15
15
  end
@@ -18,6 +18,7 @@ describe "EeePub::NCX" do
18
18
  @ncx.depth.should == 1
19
19
  @ncx.total_page_count.should == 0
20
20
  @ncx.max_page_number.should == 0
21
+ @ncx.doc_title.should == 'Untitled'
21
22
  end
22
23
 
23
24
  it 'should make xml' do
@@ -25,20 +26,55 @@ describe "EeePub::NCX" do
25
26
  head = doc.at('head')
26
27
  head.should_not be_nil
27
28
 
28
- head.at("//xmlns:meta[@name='dtb:uid']")['content'].should == 'uid'
29
- head.at("//xmlns:meta[@name='dtb:depth']")['content'].should == '1'
30
- head.at("//xmlns:meta[@name='dtb:totalPageCount']")['content'].should == '0'
31
- head.at("//xmlns:meta[@name='dtb:maxPageNumber']")['content'].should == '0'
32
- head.at("//xmlns:docTitle/xmlns:text").inner_text.should == 'title'
29
+ head.at("//xmlns:meta[@name='dtb:uid']")['content'].should == @ncx.uid
30
+ head.at("//xmlns:meta[@name='dtb:depth']")['content'].should == @ncx.depth.to_s
31
+ head.at("//xmlns:meta[@name='dtb:totalPageCount']")['content'].should == @ncx.total_page_count.to_s
32
+ head.at("//xmlns:meta[@name='dtb:maxPageNumber']")['content'].should == @ncx.max_page_number.to_s
33
+ head.at("//xmlns:docTitle/xmlns:text").inner_text.should == @ncx.doc_title
33
34
 
34
35
  nav_map = doc.at('navMap')
35
36
  nav_map.should_not be_nil
36
37
  nav_map.search('navPoint').each_with_index do |nav_point, index|
37
- expect = @ncx.nav_points[index]
38
- nav_point.attribute('id').value.should == expect[:id]
39
- nav_point.attribute('playOrder').value.should == expect[:play_order]
38
+ expect = @ncx.nav_map[index]
39
+ nav_point.attribute('id').value.should == "navPoint-#{index + 1}"
40
+ nav_point.attribute('playOrder').value.should == (index + 1).to_s
40
41
  nav_point.at('navLabel').at('text').inner_text.should == expect[:label]
41
42
  nav_point.at('content').attribute('src').value.should == expect[:content]
42
43
  end
43
44
  end
45
+
46
+ context 'nested nav_map' do
47
+ before do
48
+ @ncx.nav = [
49
+ {:label => 'foo', :content => 'foo.html',
50
+ :nav => [
51
+ {:label => 'foo-1', :content => 'foo-1.html'},
52
+ {:label => 'foo-2', :content => 'foo-2.html'}
53
+ ],
54
+ },
55
+ {:label => 'bar', :content => 'bar.html'}
56
+ ]
57
+ end
58
+
59
+ it 'should make xml' do
60
+ doc = Nokogiri::XML(@ncx.to_xml)
61
+ nav_map = doc.at('navMap')
62
+
63
+ nav_map.search('navMap/navPoint').each_with_index do |nav_point, index|
64
+ expect = @ncx.nav_map[index]
65
+ nav_point.attribute('id').value.should == "navPoint-#{index + 1}"
66
+ nav_point.attribute('playOrder').value.should == (index + 1).to_s
67
+ nav_point.at('navLabel').at('text').inner_text.should == expect[:label]
68
+ nav_point.at('content').attribute('src').value.should == expect[:content]
69
+ end
70
+
71
+ nav_map.search('navPoint/navPoint').each_with_index do |nav_point, index|
72
+ expect = @ncx.nav[0][:nav][index]
73
+ nav_point.attribute('id').value.should == "navPoint-#{index + 2}"
74
+ nav_point.attribute('playOrder').value.should == (index + 2).to_s
75
+ nav_point.at('navLabel').at('text').inner_text.should == expect[:label]
76
+ nav_point.at('content').attribute('src').value.should == expect[:content]
77
+ end
78
+ end
79
+ end
44
80
  end
@@ -7,9 +7,7 @@ describe "EeePub::OCF" do
7
7
  before do
8
8
  @tmpdir = File.join(Dir.tmpdir, 'eeepub_test')
9
9
  FileUtils.mkdir_p(@tmpdir)
10
- @container = EeePub::OCF::Container.new(
11
- :rootfiles => [{:full_path => 'foo.opf', :media_type => 'application/oebps-package+xml'}]
12
- )
10
+ @container = EeePub::OCF::Container.new('foo.opf')
13
11
  @ocf = EeePub::OCF.new(:dir => @tmpdir, :container => @container)
14
12
  end
15
13
 
@@ -17,6 +15,15 @@ describe "EeePub::OCF" do
17
15
  FileUtils.rm_rf(@tmpdir)
18
16
  end
19
17
 
18
+ it 'should return rootfiles' do
19
+ @container.rootfiles.should == [{:full_path => 'foo.opf', :media_type => 'application/oebps-package+xml'}]
20
+ end
21
+
22
+ it 'should specify container as String' do
23
+ ocf = EeePub::OCF.new(:dir => @tmpdir, :container => 'foo.opf')
24
+ ocf.container.rootfiles == [{:full_path => 'foo.opf', :media_type => 'application/oebps-package+xml'}]
25
+ end
26
+
20
27
  it 'should make xml' do
21
28
  doc = Nokogiri::XML(@container.to_xml)
22
29
  rootfiles = doc.at('rootfiles')
@@ -29,8 +36,8 @@ describe "EeePub::OCF" do
29
36
  end
30
37
 
31
38
  it 'should make epub' do
32
- output_path = File.join(Dir.tmpdir, 'eeepub_test.epub')
33
- @ocf.make(output_path)
39
+ output_path = File.join('eeepub_test.epub')
40
+ @ocf.save(output_path)
34
41
  File.exists?(output_path)
35
42
  end
36
43
  end
@@ -1,31 +1,60 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'date'
2
3
 
3
4
  describe "EeePub::OPF" do
4
5
  before do
5
6
  @opf = EeePub::OPF.new(
6
- :title => 'title',
7
- :language => 'ja',
8
- :identifier => 'id',
9
- :items => [
10
- {:id => 'foo', :href => 'foo.html', :media_type => 'text/html'},
11
- {:id => 'bar', :href => 'bar.html', :media_type => 'text/html'},
12
- {:id => 'picture', :href => 'picture.png', :media_type => 'image/png'}
13
- ],
14
- :itemrefs => [
15
- {:idref => 'foo'},
16
- {:idref => 'bar'}
17
- ]
7
+ :identifier => {'ISBN' => '978-4-00-310101-8'},
8
+ :files => ['foo.html', 'bar.html', 'picture.png'],
9
+ :ncx => 'toc.ncx'
18
10
  )
19
11
  end
20
12
 
13
+ it 'should set default value' do
14
+ @opf.toc.should == 'ncx'
15
+ @opf.unique_identifier.should == 'BookId'
16
+ @opf.title.should == 'Untitled'
17
+ @opf.language.should == 'en'
18
+ end
19
+
20
+ describe 'spec of identifier' do
21
+ context 'specify as Array' do
22
+ before { @opf.identifier = [{:scheme => 'ISBN', :value => '978-4-00-310101-8'}] }
23
+ it 'should return value' do
24
+ @opf.identifier.should == [{:scheme => 'ISBN', :value => '978-4-00-310101-8'}]
25
+ end
26
+ end
27
+
28
+ context 'specify as Hash' do
29
+ before { @opf.identifier = {:scheme => 'ISBN', :value => '978-4-00-310101-8'} }
30
+ it 'should return value' do
31
+ @opf.identifier.should == [{:scheme => 'ISBN', :value => '978-4-00-310101-8', :id => @opf.unique_identifier}]
32
+ end
33
+ end
34
+
35
+ context 'specify as Hash(scheme => value)' do
36
+ before { @opf.identifier = {'ISBN' => '978-4-00-310101-8'} }
37
+ it 'should return value' do
38
+ @opf.identifier.should == [{:scheme => 'ISBN', :value => '978-4-00-310101-8', :id => @opf.unique_identifier}]
39
+ end
40
+ end
41
+
42
+ context 'specify as String' do
43
+ before { @opf.identifier = '978-4-00-310101-8' }
44
+ it 'should return value' do
45
+ @opf.identifier.should == [{:value => '978-4-00-310101-8', :id => @opf.unique_identifier}]
46
+ end
47
+ end
48
+ end
49
+
21
50
  it 'should export as xml' do
22
51
  doc = Nokogiri::XML(@opf.to_xml)
52
+ doc.at('package').attribute('unique-identifier').value.should == @opf.unique_identifier
23
53
  metadata = doc.at('metadata')
24
54
  metadata.should_not be_nil
25
55
  [
26
56
  ['dc:title', @opf.title],
27
57
  ['dc:language', @opf.language],
28
- ['dc:identifier', @opf.identifier],
29
58
  ['dc:date', ''],
30
59
  ['dc:subject', ''],
31
60
  ['dc:description', ''],
@@ -37,25 +66,32 @@ describe "EeePub::OPF" do
37
66
  metadata.xpath(xpath,
38
67
  'xmlns:dc' => "http://purl.org/dc/elements/1.1/").inner_text.should == expect
39
68
  end
69
+ identifier = metadata.xpath('dc:identifier', 'xmlns:dc' => "http://purl.org/dc/elements/1.1/")[0]
70
+ identifier.attribute('id').value.should == @opf.unique_identifier
71
+ identifier.attribute('scheme').value.should == @opf.identifier[0][:scheme]
72
+ identifier.inner_text.should == @opf.identifier[0][:value]
40
73
 
41
74
  manifest = doc.at('manifest')
42
75
  manifest.should_not be_nil
43
- items = manifest.search('item')
44
- items.size.should == 3
45
- items.each_with_index do |item, index|
46
- expect = @opf.items[index]
47
- item.attribute('id').value.should == expect[:id]
48
- item.attribute('href').value.should == expect[:href]
49
- item.attribute('media-type').value.should == expect[:media_type]
76
+ manifest = manifest.search('item')
77
+ manifest.size.should == 4
78
+ manifest[0..2].each_with_index do |item, index|
79
+ expect = @opf.manifest[index]
80
+ item.attribute('id').value.should == expect
81
+ item.attribute('href').value.should == expect
82
+ item.attribute('media-type').value.should == @opf.guess_media_type(expect)
50
83
  end
84
+ manifest[3].attribute('id').value.should == 'ncx'
85
+ manifest[3].attribute('href').value.should == @opf.ncx
86
+ manifest[3].attribute('media-type').value.should == @opf.guess_media_type(@opf.ncx)
51
87
 
52
88
  spine = doc.at('spine')
53
89
  spine.should_not be_nil
54
- itemrefs = spine.search('itemref')
55
- itemrefs.size.should == 2
56
- itemrefs.each_with_index do |itemref, index|
57
- expect = @opf.itemrefs[index]
58
- itemref.attribute('idref').value.should == expect[:idref]
90
+ spine = spine.search('itemref')
91
+ spine.size.should == 2
92
+ spine.each_with_index do |itemref, index|
93
+ expect = @opf.spine[index]
94
+ itemref.attribute('idref').value.should == expect
59
95
  end
60
96
  end
61
97
 
@@ -79,7 +115,6 @@ describe "EeePub::OPF" do
79
115
  [
80
116
  ['dc:title', @opf.title],
81
117
  ['dc:language', @opf.language],
82
- ['dc:identifier', @opf.identifier],
83
118
  ['dc:date', @opf.date.to_s],
84
119
  ['dc:subject', 'subject'],
85
120
  ['dc:description', 'description'],
@@ -91,6 +126,91 @@ describe "EeePub::OPF" do
91
126
  metadata.xpath(xpath,
92
127
  'xmlns:dc' => "http://purl.org/dc/elements/1.1/").inner_text.should == expect
93
128
  end
129
+ identifier = metadata.xpath('dc:identifier', 'xmlns:dc' => "http://purl.org/dc/elements/1.1/")[0]
130
+ identifier.attribute('id').value.should == @opf.unique_identifier
131
+ identifier.attribute('scheme').value.should == @opf.identifier[0][:scheme]
132
+ identifier.inner_text.should == @opf.identifier[0][:value]
133
+ end
134
+ end
135
+
136
+ context 'plural identifiers' do
137
+ before do
138
+ @opf.identifier = [
139
+ {:id => 'BookId', :scheme => 'ISBN', :value => '978-4-00-310101-8'},
140
+ {:id => 'BookURL', :scheme => 'URL', :value => 'http://example.com/books/foo'}
141
+ ]
142
+ end
143
+
144
+ it 'should export as xml' do
145
+ doc = Nokogiri::XML(@opf.to_xml)
146
+ elements = doc.xpath('//dc:identifier', 'xmlns:dc' => "http://purl.org/dc/elements/1.1/")
147
+ elements.size.should == 2
148
+ elements.each_with_index do |element, index|
149
+ expect = @opf.identifier[index]
150
+ element.attribute('id').value.should == expect[:id]
151
+ element.attribute('scheme').value.should == expect[:scheme]
152
+ element.inner_text.should == expect[:value]
153
+ end
154
+ end
155
+ end
156
+
157
+ context 'plural languages' do
158
+ before do
159
+ @opf.language = ['ja', 'en']
160
+ end
161
+
162
+ it 'should export as xml' do
163
+ doc = Nokogiri::XML(@opf.to_xml)
164
+ elements = doc.xpath('//dc:language', 'xmlns:dc' => "http://purl.org/dc/elements/1.1/")
165
+ elements.size.should == 2
166
+ elements.each_with_index do |element, index|
167
+ element.inner_text.should == @opf.language[index]
168
+ end
169
+ end
170
+ end
171
+
172
+ context 'specify spine' do
173
+ before do
174
+ @opf.spine = ['a', 'b']
175
+ end
176
+
177
+ it 'should export as xml' do
178
+ doc = Nokogiri::XML(@opf.to_xml)
179
+ spine = doc.at('spine')
180
+ spine.should_not be_nil
181
+ spine = spine.search('itemref')
182
+ spine.size.should == 2
183
+ spine.each_with_index do |itemref, index|
184
+ expect = @opf.spine[index]
185
+ itemref.attribute('idref').value.should == expect
186
+ end
187
+ end
188
+ end
189
+
190
+ context 'specify manifest as Hash' do
191
+ before do
192
+ @opf.manifest = [
193
+ {:id => 'foo', :href => 'foo.html', :media_type => 'application/xhtml+xml'},
194
+ {:id => 'bar', :href => 'bar.html', :media_type => 'application/xhtml+xml'},
195
+ {:id => 'picture', :href => 'picture.png', :media_type => 'image/png'}
196
+ ]
197
+ end
198
+
199
+ it 'should export as xml' do
200
+ doc = Nokogiri::XML(@opf.to_xml)
201
+ manifest = doc.at('manifest')
202
+ manifest.should_not be_nil
203
+ manifest = manifest.search('item')
204
+ manifest.size.should == 4
205
+ manifest[0..2].each_with_index do |item, index|
206
+ expect = @opf.manifest[index]
207
+ item.attribute('id').value.should == expect[:id]
208
+ item.attribute('href').value.should == expect[:href]
209
+ item.attribute('media-type').value.should == expect[:media_type]
210
+ end
211
+ manifest[3].attribute('id').value.should == 'ncx'
212
+ manifest[3].attribute('href').value.should == @opf.ncx
213
+ manifest[3].attribute('media-type').value.should == @opf.guess_media_type(@opf.ncx)
94
214
  end
95
215
  end
96
216
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - jugyo
@@ -14,11 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-02 00:00:00 +09:00
17
+ date: 2010-05-05 00:00:00 +09:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: rspec
21
+ name: builder
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  requirements:
@@ -27,8 +27,20 @@ dependencies:
27
27
  segments:
28
28
  - 0
29
29
  version: "0"
30
- type: :development
30
+ type: :runtime
31
31
  version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: rspec
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :development
43
+ version_requirements: *id002
32
44
  description: EeePub is a Ruby ePub generator.
33
45
  email: jugyo.org@gmail.com
34
46
  executables: []
@@ -37,10 +49,10 @@ extensions: []
37
49
 
38
50
  extra_rdoc_files:
39
51
  - LICENSE
40
- - README.rdoc
52
+ - README.md
41
53
  files:
42
54
  - LICENSE
43
- - README.rdoc
55
+ - README.md
44
56
  - Rakefile
45
57
  - VERSION
46
58
  - examples/simple.rb
@@ -49,9 +61,6 @@ files:
49
61
  - lib/eeepub/ncx.rb
50
62
  - lib/eeepub/ocf.rb
51
63
  - lib/eeepub/opf.rb
52
- - lib/eeepub/templates/container.xml.erb
53
- - lib/eeepub/templates/ncx.erb
54
- - lib/eeepub/templates/opf.erb
55
64
  - spec/eeepub/ncx_spec.rb
56
65
  - spec/eeepub/ocf_spec.rb
57
66
  - spec/eeepub/opf_spec.rb
data/README.rdoc DELETED
@@ -1,15 +0,0 @@
1
- = EeePub
2
-
3
- EeePub is a Ruby ePub generator.
4
-
5
- == Install
6
-
7
- gem install eeepub
8
-
9
- == Usage
10
-
11
- Show examples: http://github.com/jugyo/EeePub/tree/master/examples/
12
-
13
- == Copyright
14
-
15
- Copyright (c) 2010 jugyo. See LICENSE for details.
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
3
- <rootfiles>
4
- <%- rootfiles.each do |rootfile| -%>
5
- <rootfile full-path="<%=h rootfile[:full_path] %>" media-type="<%=h rootfile[:media_type] %>"/>
6
- <%- end -%>
7
- </rootfiles>
8
- </container>
@@ -1,22 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">
3
- <head>
4
- <meta name="dtb:uid" content="<%=h uid %>"/>
5
- <meta name="dtb:depth" content="<%=h depth %>"/>
6
- <meta name="dtb:totalPageCount" content="<%=h total_page_count %>"/>
7
- <meta name="dtb:maxPageNumber" content="<%=h max_page_number %>"/>
8
- </head>
9
- <docTitle>
10
- <text><%=h doc_title %></text>
11
- </docTitle>
12
- <navMap>
13
- <%- nav_points.each do |nav| -%>
14
- <navPoint id="<%=h nav[:id] %>" playOrder="<%=h nav[:play_order] %>">
15
- <navLabel>
16
- <text><%=h nav[:label] %></text>
17
- </navLabel>
18
- <content src="<%=h nav[:content] %>"/>
19
- </navPoint>
20
- <%- end -%>
21
- </navMap>
22
- </ncx>
@@ -1,39 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <package version="2.0" xmlns="http://www.idpf.org/2007/opf" unique-identifier="BookId">
3
- <metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
4
- <dc:title><%=h title %></dc:title>
5
- <dc:language><%=h language %></dc:language>
6
- <dc:identifier id="BookId"><%=h identifier %></dc:identifier>
7
- <%- if date -%>
8
- <dc:date><%=h date.to_s %></dc:data>
9
- <%- end -%>
10
- <%- if subject -%>
11
- <dc:subject><%=h subject %></dc:subject>
12
- <%- end -%>
13
- <%- if description -%>
14
- <dc:description><%=h description %></dc:description>
15
- <%- end -%>
16
- <%- if relation -%>
17
- <dc:relation><%=h relation %></dc:relation>
18
- <%- end -%>
19
- <%- if creator -%>
20
- <dc:creator opf:role="aut"><%=h creator %></dc:creator>
21
- <%- end -%>
22
- <%- if publisher -%>
23
- <dc:publisher><%=h publisher %></dc:publisher>
24
- <%- end -%>
25
- <%- if rights -%>
26
- <dc:rights><%=h rights %></dc:rights>
27
- <%- end -%>
28
- </metadata>
29
- <manifest>
30
- <%- items.each do |item| -%>
31
- <item id="<%=h item[:id] %>" href="<%=h item[:href] %>" media-type="<%=h item[:media_type] %>" />
32
- <%- end -%>
33
- </manifest>
34
- <spine toc="ncx">
35
- <%- itemrefs.each do |itemref| -%>
36
- <itemref idref="<%=h itemref[:idref] %>" />
37
- <%- end -%>
38
- </spine>
39
- </package>