epub-parser 0.0.6.1 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +1,36 @@
1
- require 'epub/parser'
2
1
  require 'tmpdir'
2
+ require 'optparse'
3
+ require 'epub/parser'
4
+
5
+ Version = EPUB::Parser::VERSION
3
6
 
4
- # Use optparse or slop or Thor
5
- # Validat ARGV
7
+ options = {:format => :line}
8
+ opt = OptionParser.new do |opt|
9
+ opt.banner = 'Usage: epubinfo [options] EPUBFILE'
10
+ opt.on '-f', '--format=FORMAT', [:line, :json, :yaml], 'Format of output(line, json or yaml), defaults to line(for console)' do |format|
11
+ options[:format] = format
12
+ end
13
+ end
14
+ opt.parse!(ARGV)
6
15
 
7
16
  file = ARGV[0]
8
- dir = Dir.mktmpdir('epub-parser') do |dir|
17
+ unless file
18
+ $stdout.puts opt.help
19
+ exit 1
20
+ end
9
21
 
22
+ Dir.mktmpdir('epub-parser') do |dir|
10
23
  book = EPUB::Parser.parse file, dir
11
- puts book.package.metadata.to_json
12
-
24
+ data = book.package.metadata.to_hash
25
+ data['Unique identifier'] = [book.package.metadata.unique_identifier]
26
+ data['EPUB Version'] = [book.package.version]
27
+ if options[:format] == :line
28
+ key_width = data.keys.map {|k| k.length}.max + 3
29
+ data.each_pair do |k, v|
30
+ puts (k.to_s.capitalize + ':').ljust(key_width) + v.join(', ')
31
+ end
32
+ else
33
+ require options[:format].to_s
34
+ puts data.__send__("to_#{options[:format]}")
35
+ end
13
36
  end
@@ -19,6 +19,7 @@ module EPUB
19
19
  end
20
20
 
21
21
  def parse
22
+ parse_package
22
23
  parse_metadata
23
24
  parse_manifest
24
25
  parse_spine
@@ -28,6 +29,14 @@ module EPUB
28
29
  @package
29
30
  end
30
31
 
32
+ def parse_package
33
+ elem = @doc.root
34
+ @package.version = elem['version']
35
+ @package.unique_identifier = elem['unique-identifier']
36
+
37
+ @package
38
+ end
39
+
31
40
  def parse_metadata
32
41
  metadata = @package.metadata = EPUB::Publication::Package::Metadata.new
33
42
  elem = @doc.xpath('/opf:package/opf:metadata', EPUB::NAMESPACES).first
@@ -40,13 +49,8 @@ module EPUB
40
49
  identifier
41
50
  end
42
51
 
43
- metadata.titles = elem.xpath('./dc:title', EPUB::NAMESPACES).collect do |e|
44
- title = EPUB::Publication::Package::Metadata::Title.new
52
+ metadata.titles = collect_dcmes(elem, './dc:title') do |title, e|
45
53
  title.content = e.content
46
- %w[ id lang dir ].each do |attr|
47
- title.__send__("#{attr}=", e[attr])
48
- end
49
- title
50
54
  end
51
55
 
52
56
  metadata.languages = elem.xpath('./dc:language', EPUB::NAMESPACES).collect do |e|
@@ -54,25 +58,11 @@ module EPUB
54
58
  end
55
59
 
56
60
  %w[ contributor coverage creator date description format publisher relation source subject type ].each do |dcmes|
57
- metadata.__send__ "#{dcmes}s=", elem.xpath("./dc:#{dcmes}", EPUB::NAMESPACES).collect { |e|
58
- md = EPUB::Publication::Package::Metadata::DCMES.new
59
- md.content = e.content
60
- %w[ id lang dir ].each do |attr|
61
- md.__send__("#{attr}=", e[attr])
62
- end
63
- md
64
- }
65
- end
66
- metadata.rights = elem.xpath('./dc:rights', EPUB::NAMESPACES).collect do |e|
67
- md = EPUB::Publication::Package::Metadata::DCMES.new
68
- md.content = e.content
69
- %w[ id lang dir ].each do |attr|
70
- md.__send__("#{attr}=", e[attr])
71
- end
72
- md
61
+ metadata.__send__ "#{dcmes}s=", collect_dcmes(elem, "./dc:#{dcmes}")
73
62
  end
63
+ metadata.rights = collect_dcmes(elem, './dc:rights')
74
64
 
75
- # handle meta elements
65
+ # To do: handle <meta> elements
76
66
 
77
67
  metadata
78
68
  end
@@ -138,6 +128,18 @@ module EPUB
138
128
  def parse_bindings
139
129
  raise 'still not implemented'
140
130
  end
131
+
132
+ def collect_dcmes(elem, selector)
133
+ elem.xpath(selector, EPUB::NAMESPACES).collect do |e|
134
+ md = EPUB::Publication::Package::Metadata::DCMES.new
135
+ md.content = e.content
136
+ %w[ id lang dir ].each do |attr|
137
+ md.__send__("#{attr}=", e[attr])
138
+ end
139
+ yield(md, e) if block_given?
140
+ md
141
+ end
142
+ end
141
143
  end
142
144
  end
143
145
  end
@@ -1,5 +1,5 @@
1
1
  module EPUB
2
2
  class Parser
3
- VERSION = "0.0.6.1"
3
+ VERSION = "0.0.7"
4
4
  end
5
5
  end
@@ -1,5 +1,3 @@
1
- require 'json'
2
-
3
1
  module EPUB
4
2
  module Publication
5
3
  class Package
@@ -7,7 +5,7 @@ module EPUB
7
5
  ELEMS = [:identifiers, :titles, :languages] +
8
6
  [:contributors, :coverages, :creators, :dates, :descriptions, :formats, :publishers,
9
7
  :relations, :rights, :sources, :subjects, :types]
10
- attr_accessor :package,
8
+ attr_accessor :package, :unique_identifier,
11
9
  *(ELEMS.collect {|elem| "dc_#{elem}"})
12
10
  ELEMS.each do |elem|
13
11
  alias_method elem, "dc_#{elem}"
@@ -15,12 +13,11 @@ module EPUB
15
13
  end
16
14
  attr_accessor :links
17
15
 
18
- def to_json(pretty = false)
19
- obj = {}
20
- ELEMS.each do |elem|
21
- obj[elem] = __send__(elem)
16
+ def to_hash
17
+ ELEMS.inject({}) do |hsh, elem|
18
+ hsh[elem] = __send__(elem)
19
+ hsh
22
20
  end
23
- obj.to_json
24
21
  end
25
22
 
26
23
  class Identifier
@@ -5,6 +5,11 @@ require 'epub/parser/publication'
5
5
  class TestParserPublication < Test::Unit::TestCase
6
6
  def setup
7
7
  @parser = EPUB::Parser::Publication.new 'test/fixtures/book/OPS/ルートファイル.opf'
8
+ @package = @parser.parse_package
9
+ end
10
+
11
+ def test_parse_package
12
+ assert_equal '3.0', @package.version
8
13
  end
9
14
 
10
15
  class TestParseMetadata < TestParserPublication
@@ -17,6 +22,10 @@ class TestParserPublication < Test::Unit::TestCase
17
22
  assert_equal 'da265185-8da8-462d-a146-17dd388f61fc', @metadata.identifiers.first.content
18
23
  end
19
24
 
25
+ def test_has_unique_identifier
26
+ assert_equal 'da265185-8da8-462d-a146-17dd388f61fc', @metadata.unique_identifier.to_s
27
+ end
28
+
20
29
  def test_has_five_titles
21
30
  assert_equal 5, @metadata.titles.length
22
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epub-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6.1
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-19 00:00:00.000000000Z
12
+ date: 2012-02-21 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rubygems-test
16
- requirement: &21388280 !ruby/object:Gem::Requirement
16
+ requirement: &12346600 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *21388280
24
+ version_requirements: *12346600
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &21387200 !ruby/object:Gem::Requirement
27
+ requirement: &12342420 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *21387200
35
+ version_requirements: *12342420
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: pry
38
- requirement: &21515840 !ruby/object:Gem::Requirement
38
+ requirement: &12360020 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *21515840
46
+ version_requirements: *12360020
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: pry-doc
49
- requirement: &21513220 !ruby/object:Gem::Requirement
49
+ requirement: &12359300 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *21513220
57
+ version_requirements: *12359300
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: test-unit
60
- requirement: &21511460 !ruby/object:Gem::Requirement
60
+ requirement: &12358560 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '2'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *21511460
68
+ version_requirements: *12358560
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: simplecov
71
- requirement: &21510360 !ruby/object:Gem::Requirement
71
+ requirement: &12358000 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *21510360
79
+ version_requirements: *12358000
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: thin
82
- requirement: &21509080 !ruby/object:Gem::Requirement
82
+ requirement: &12357120 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *21509080
90
+ version_requirements: *12357120
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: yard
93
- requirement: &21468300 !ruby/object:Gem::Requirement
93
+ requirement: &12356360 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *21468300
101
+ version_requirements: *12356360
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: epzip
104
- requirement: &21466200 !ruby/object:Gem::Requirement
104
+ requirement: &12355640 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *21466200
112
+ version_requirements: *12355640
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: epubcheck
115
- requirement: &21465040 !ruby/object:Gem::Requirement
115
+ requirement: &12354540 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,10 +120,10 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *21465040
123
+ version_requirements: *12354540
124
124
  - !ruby/object:Gem::Dependency
125
125
  name: epub_validator
126
- requirement: &21464320 !ruby/object:Gem::Requirement
126
+ requirement: &12353020 !ruby/object:Gem::Requirement
127
127
  none: false
128
128
  requirements:
129
129
  - - ! '>='
@@ -131,10 +131,10 @@ dependencies:
131
131
  version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
- version_requirements: *21464320
134
+ version_requirements: *12353020
135
135
  - !ruby/object:Gem::Dependency
136
136
  name: zipruby
137
- requirement: &21462160 !ruby/object:Gem::Requirement
137
+ requirement: &12370280 !ruby/object:Gem::Requirement
138
138
  none: false
139
139
  requirements:
140
140
  - - ! '>='
@@ -142,10 +142,10 @@ dependencies:
142
142
  version: '0'
143
143
  type: :development
144
144
  prerelease: false
145
- version_requirements: *21462160
145
+ version_requirements: *12370280
146
146
  - !ruby/object:Gem::Dependency
147
147
  name: rubyzip
148
- requirement: &21854220 !ruby/object:Gem::Requirement
148
+ requirement: &12369680 !ruby/object:Gem::Requirement
149
149
  none: false
150
150
  requirements:
151
151
  - - ! '>='
@@ -153,10 +153,10 @@ dependencies:
153
153
  version: '0'
154
154
  type: :development
155
155
  prerelease: false
156
- version_requirements: *21854220
156
+ version_requirements: *12369680
157
157
  - !ruby/object:Gem::Dependency
158
158
  name: enumerabler
159
- requirement: &21852560 !ruby/object:Gem::Requirement
159
+ requirement: &12368540 !ruby/object:Gem::Requirement
160
160
  none: false
161
161
  requirements:
162
162
  - - ! '>='
@@ -164,10 +164,10 @@ dependencies:
164
164
  version: '0'
165
165
  type: :runtime
166
166
  prerelease: false
167
- version_requirements: *21852560
167
+ version_requirements: *12368540
168
168
  - !ruby/object:Gem::Dependency
169
169
  name: nokogiri
170
- requirement: &21851880 !ruby/object:Gem::Requirement
170
+ requirement: &12367280 !ruby/object:Gem::Requirement
171
171
  none: false
172
172
  requirements:
173
173
  - - ! '>='
@@ -175,10 +175,10 @@ dependencies:
175
175
  version: '0'
176
176
  type: :runtime
177
177
  prerelease: false
178
- version_requirements: *21851880
178
+ version_requirements: *12367280
179
179
  - !ruby/object:Gem::Dependency
180
180
  name: addressable
181
- requirement: &21851320 !ruby/object:Gem::Requirement
181
+ requirement: &12365820 !ruby/object:Gem::Requirement
182
182
  none: false
183
183
  requirements:
184
184
  - - ! '>='
@@ -186,7 +186,7 @@ dependencies:
186
186
  version: '0'
187
187
  type: :runtime
188
188
  prerelease: false
189
- version_requirements: *21851320
189
+ version_requirements: *12365820
190
190
  description: Parse EPUB 3 book loosely
191
191
  email:
192
192
  - KitaitiMakoto@gmail.com
@@ -256,7 +256,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
256
256
  version: '0'
257
257
  segments:
258
258
  - 0
259
- hash: -1758789986162453430
259
+ hash: -4537999334465122186
260
260
  required_rubygems_version: !ruby/object:Gem::Requirement
261
261
  none: false
262
262
  requirements:
@@ -265,7 +265,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
265
265
  version: '0'
266
266
  segments:
267
267
  - 0
268
- hash: -1758789986162453430
268
+ hash: -4537999334465122186
269
269
  requirements: []
270
270
  rubyforge_project:
271
271
  rubygems_version: 1.8.8