epub-maker 0.1.3 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 13a8872f5fc2aa702e7a3b345e14bc10ddfe14c4f05715964ed887c7a5e43f1a
4
- data.tar.gz: 9bc0d73cf5aa30d7c78d4aa4afbcee93feb0d32626f2f44a016dd85733c988e8
3
+ metadata.gz: 0e50c70b73d483a46da5bb6110e0159c311a3ac2a9c1d30a02f9cb28bde5b512
4
+ data.tar.gz: be7576c82dda592b0e0296b14b648594c510f122ad125a7b3302bce5d6448d25
5
5
  SHA512:
6
- metadata.gz: d5757a93cc5a4a3fbdf8a47661fba858d8c9fd15db9a80527125745d3b0b62b4f02d7b57dcb85097fb106d3b1ee95a44df23e6356517cccb8b93fe394fda1552
7
- data.tar.gz: 883f0dd647d80283867e7e2baedb040bd0831d7d5501c6decafa0ff1ea7961c2359a078b3db800499bc2a6078322044131687e8571f639c137fac3064423ddcf
6
+ metadata.gz: 0a37e50046f9fc4dd1054b0d21e4a422548d1847a43f89bd6b49133864fa40852909f1f2555a3519cf77807eb169856fa6fe1a2b8705009bd5748b38dfd0431b
7
+ data.tar.gz: 6cbb1058962c23db192085603a4cf47865271a4d8ab3ef79cd543de90a67948b0b98c39512392bae3f4e492df05945cd823018795615c371c133e983fb88793d
data/.gitlab-ci.yml CHANGED
@@ -46,3 +46,23 @@ test:2.6:
46
46
  key: ruby:2.6
47
47
  paths:
48
48
  - deps
49
+
50
+ test:2.7:
51
+ image: ruby:2.7
52
+ script: bundle exec rake test
53
+ except:
54
+ - tags
55
+ cache:
56
+ key: ruby:2.7
57
+ paths:
58
+ - deps
59
+
60
+ test:3.0:
61
+ image: ruby:3.0-rc
62
+ script: bundle exec rake test
63
+ except:
64
+ - tags
65
+ cache:
66
+ key: ruby:3.0
67
+ paths:
68
+ - deps
data/.yardopts ADDED
@@ -0,0 +1,2 @@
1
+ -
2
+ CHANGELOG.markdown
data/CHANGELOG.markdown CHANGED
@@ -1,3 +1,25 @@
1
+ 0.1.7
2
+ -----
3
+
4
+ * Remove version specification from Nokogiri to migrate to Ruby 3.1
5
+
6
+ 0.1.6
7
+ -----
8
+
9
+ * [BUG FIX]Add `mtime` keyword argument to `OCF::PhysicalContainer::Zipruby#write`
10
+
11
+ 0.1.5
12
+ -----
13
+
14
+ * [BUG FIX]Compare path name in the same encoding when finding file in ZIP archive
15
+ * Add `PhysicalContaienr.mtime` API to set mtime of files in EPUB archive
16
+
17
+ 0.1.4
18
+ -----
19
+
20
+ * Add `Publication::Package::Metadata#modified=` method
21
+ * Rescue Errno::EXDEV on renaming temporary EPUB file
22
+
1
23
  0.1.3
2
24
  -----
3
25
 
data/README.markdown CHANGED
@@ -178,52 +178,23 @@ Todo
178
178
  Recent Changes
179
179
  --------------
180
180
 
181
- ### 0.1.3
181
+ ### 0.1.7
182
182
 
183
- * Add workaround Window file renaming problem
183
+ * Remove version specification from Nokogiri to migrate to Ruby 3.1
184
184
 
185
- ### 0.1.2
185
+ ### 0.1.6
186
186
 
187
- * Close temp file when archiving EPUB file
187
+ * [BUG FIX]Add `mtime` keyword argument to `OCF::PhysicalContainer::Zipruby#write`
188
188
 
189
- ### 0.1.1
189
+ ### 0.1.5
190
190
 
191
- * Update Nokogiri version to < 1.11
191
+ * [BUG FIX]Compare path name in the same encoding when finding file in ZIP archive
192
+ * Add `PhysicalContaienr.mtime` API to set mtime of files in EPUB archive
192
193
 
193
- ### 0.1.0
194
+ ### 0.1.4
194
195
 
195
- * [BUG FIX]Fix the case file extension should be wrong
196
- * Add Nokogiri to runtime dependencies explicitly
197
-
198
- ### 0.0.9
199
-
200
- * Remove [ruby-uuid][] gem from dependencies
201
- * [BUG FIX]Convert Set to Array before writing into XML
202
- * Follow change of EPUB Parser v0.3.6
203
-
204
- [ruby-uuid]: https://github.com/shyouhei/ruby-uuid
205
-
206
- ### 0.0.8
207
-
208
- * Use default temporary directory for `EPUB::Maker.archive`
209
-
210
- ### 0.0.7
211
-
212
- * Change temporary directory used by `EPUB::Maker.archive`
213
-
214
- ### 0.0.6
215
-
216
- * Add `epub-archive` command
217
- * Add `EPUB::Maker.archive` method
218
-
219
- ### 0.0.5
220
-
221
- * Fix bug to modify `dc:rights` to `dc:right`
222
-
223
- ### 0.0.4
224
- * API change: #save -> #write for PhysicalContainer classes
225
- * Bump required EPUB Parser version: 0.2.0 -> 0.2.6
226
- * Deprecate `EPUB::OCF::PhysicalContainer.save`
196
+ * Add `Publication::Package::Metadata#modified=` method
197
+ * Rescue Errno::EXDEV on renaming temporary EPUB file
227
198
 
228
199
  Contributing
229
200
  ------------
data/Rakefile CHANGED
@@ -2,11 +2,17 @@ require 'rake/testtask'
2
2
  require 'rake/clean'
3
3
  require 'yard'
4
4
  require "rubygems/tasks"
5
+ require "open-uri"
5
6
 
6
7
  task :default => :test
7
8
 
8
9
  CLEAN.include 'README.html'
9
10
 
11
+ VALID_EPUB = "test/fixtures/accessible_epub_3.epub"
12
+ file VALID_EPUB do |t|
13
+ File.write t.name, URI("https://github.com/IDPF/epub3-samples/releases/download/20170606/accessible_epub_3.epub").read
14
+ end
10
15
  Rake::TestTask.new
16
+ task test: VALID_EPUB
11
17
  YARD::Rake::YardocTask.new
12
18
  Gem::Tasks.new
data/epub-maker.gemspec CHANGED
@@ -25,7 +25,8 @@ Gem::Specification.new do |gem|
25
25
  gem.add_runtime_dependency 'archive-zip'
26
26
  gem.add_runtime_dependency 'rake'
27
27
  gem.add_runtime_dependency 'addressable', '>= 2.3.5'
28
- gem.add_runtime_dependency "nokogiri", ">= 1.6.0", "< 1.11"
28
+ gem.add_runtime_dependency 'rexml'
29
+ gem.add_runtime_dependency "nokogiri"
29
30
 
30
31
  gem.add_development_dependency 'zipruby'
31
32
  gem.add_development_dependency 'test-unit'
@@ -5,7 +5,7 @@ module EPUB
5
5
  class PhysicalContainer
6
6
  class ArchiveZip < self
7
7
  # @todo Write multiple files at once
8
- def write(path_name, content)
8
+ def write(path_name, content, mtime: nil)
9
9
  ::Dir.mktmpdir do |dir|
10
10
  tmp_archive_path = ::File.join(dir, ::File.basename(@container_path) + '.tmp')
11
11
  ::File.open @container_path do |archive_in|
@@ -15,15 +15,21 @@ module EPUB
15
15
  Archive::Zip.open archive_out, :w do |z_out|
16
16
  updated = false
17
17
  z_in.each do |entry|
18
- if entry.zip_path == path_name
18
+ if entry.zip_path == path_name.force_encoding('ASCII-8BIT')
19
19
  entry.file_data = StringIO.new(content)
20
20
  updated = true
21
21
  end
22
+ if mtime
23
+ entry.mtime = mtime
24
+ end
22
25
  z_out << entry
23
26
  end
24
27
  unless updated
25
28
  entry = Archive::Zip::Entry::File.new(path_name)
26
29
  entry.file_data = StringIO.new(content)
30
+ if mtime
31
+ entry.mtime = mtime
32
+ end
27
33
  z_out << entry
28
34
  end
29
35
  end
@@ -33,7 +39,7 @@ module EPUB
33
39
  begin
34
40
  ::File.chmod 0666 & ~::File.umask, tmp_archive_path
35
41
  ::File.rename tmp_archive_path, @container_path
36
- rescue Errno::EACCES
42
+ rescue Errno::EACCES, Errno::EXDEV
37
43
  # In some cases on Windows, we fail to rename the file
38
44
  # but succeed to copy although I don't know why.
39
45
  # Race condition? I don't know. But no time to dig deeper.
@@ -4,7 +4,7 @@ module EPUB
4
4
  class OCF
5
5
  class PhysicalContainer
6
6
  class Zipruby < self
7
- def write(path_name, content)
7
+ def write(path_name, content, mtime: nil)
8
8
  if @archive
9
9
  @archive.add_or_replace_buffer path_name, content
10
10
  else
@@ -13,15 +13,34 @@ module EPUB
13
13
  class OCF
14
14
  class PhysicalContainer
15
15
  class << self
16
+ @@mtime = nil
17
+
18
+ def mtime
19
+ @@mtime
20
+ end
21
+
22
+ # Sets +mtime+, which is used when setting mtime of file in EPUB(ZIP) archive.
23
+ # Currently supported for only {ArchiveZip} adapter
24
+ #
25
+ # @example
26
+ # EPUB::OCF::PhysicalCotainer.adapter = :ArchiveZip
27
+ # EPUB::OCF::PhysicalCotainer.mtime = Time.new(2020, 1, 1)
28
+ #
29
+ # @param time [Time] mtime to set
30
+ # @return [Time]
31
+ def mtime=(time)
32
+ @@mtime = time
33
+ end
34
+
16
35
  def write(container_path, path_name, content)
17
36
  open(container_path) {|container|
18
- container.write(path_name, content)
37
+ container.write(path_name, content, mtime: mtime)
19
38
  }
20
39
  end
21
40
 
22
41
  def save(container_path, path_name, content)
23
42
  warn "EPUB::OCF::PhysicalContainer.#{__method__} is deprecated. Use .write instead"
24
- write(container_path, path_name, content)
43
+ write(container_path, path_name, content, mtime: mtime)
25
44
  end
26
45
  end
27
46
  end
@@ -1,6 +1,7 @@
1
1
  require 'securerandom'
2
2
  require 'epub/publication/package'
3
3
  require "nokogiri"
4
+ require "mimemagic"
4
5
 
5
6
  module EPUB
6
7
  module Publication
@@ -183,6 +184,21 @@ module EPUB
183
184
  name
184
185
  end
185
186
 
187
+ # Shortcut to set modified date time
188
+ # @param datetime [String, #to_time]
189
+ def modified=(datetime)
190
+ modified = self.modified
191
+ unless modified
192
+ modified = Meta.new
193
+ modified.property = "dcterms:modified"
194
+ self.metas << modified
195
+ end
196
+ modified.content = datetime.respond_to?(:to_time) ?
197
+ datetime.to_time.utc.xmlschema :
198
+ datetime
199
+ modified
200
+ end
201
+
186
202
  class Meta
187
203
  def valid?
188
204
  property
@@ -235,6 +251,15 @@ module EPUB
235
251
  end
236
252
  item.manifest = self
237
253
  yield item if block_given?
254
+ unless item.media_type
255
+ path = item.href.to_s
256
+ item.media_type =
257
+ if path.end_with? ".html"
258
+ "application/xhtml+xml"
259
+ else
260
+ MimeMagic.by_path(item.href.to_s).type
261
+ end
262
+ end
238
263
  self << item
239
264
  item
240
265
  end
@@ -263,7 +288,11 @@ module EPUB
263
288
  elsif content_file
264
289
  File.read(content_file)
265
290
  else
266
- raise 'no content nor content_file'
291
+ begin
292
+ read
293
+ rescue OCF::PhysicalContainer::NoEntry
294
+ raise 'no content nor content_file'
295
+ end
267
296
  end
268
297
  book = manifest.package.book
269
298
  book.container_adapter.write book.epub_file, entry_name, content_to_save
@@ -1,5 +1,5 @@
1
1
  module EPUB
2
2
  module Maker
3
- VERSION = "0.1.3"
3
+ VERSION = "0.1.7"
4
4
  end
5
5
  end
data/test/helper.rb CHANGED
@@ -11,6 +11,12 @@ class Test::Unit::TestCase
11
11
  def assert_valid_epub(file)
12
12
  assert_true Epubcheck::Ruby::CLI.new.execute(file)
13
13
  end
14
+
15
+ private
16
+
17
+ def valid_epub
18
+ Pathname.new("test/fixtures/accessible_epub_3.epub")
19
+ end
14
20
  end
15
21
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
16
22
  require 'epub'
@@ -1,13 +1,15 @@
1
1
  require_relative 'helper'
2
2
  require 'epub/maker'
3
3
 
4
- class TestImplaceEditing < Test::Unit::TestCase
4
+ class TestInplaceEditing < Test::Unit::TestCase
5
5
  def setup
6
6
  @assets_dir = Pathname(__dir__)/'fixtures'/'book'
7
7
  @dir = Pathname.mktmpdir('epub-maker-test')
8
8
  @file = @dir/'book.epub'
9
9
  EPUB::Maker.archive @assets_dir, @file
10
10
  @book = EPUB::Parser.parse(@file)
11
+ @valid_epub = @dir/valid_epub.basename
12
+ FileUtils.cp valid_epub, @valid_epub
11
13
  end
12
14
 
13
15
  def teardown
@@ -57,4 +59,25 @@ class TestImplaceEditing < Test::Unit::TestCase
57
59
 
58
60
  assert_match '<title>Edited Title</title>', item.read
59
61
  end
62
+
63
+ def test_edit_without_change
64
+ epub = EPUB::Parser.parse(@valid_epub)
65
+ epub.save
66
+ assert_equal epub.release_identifier, EPUB::Parser.parse(@valid_epub).release_identifier
67
+ end
68
+
69
+ def test_specify_mtime
70
+ # Currently, only ArchiveZip supports this API
71
+ EPUB::OCF::PhysicalContainer.adapter = :ArchiveZip
72
+ mtime = EPUB::OCF::PhysicalContainer.mtime = Time.new(2020, 1, 1)
73
+ epub = EPUB::Parser.parse(@valid_epub)
74
+ epub.metadata.unique_identifier.content = "new-unique-identifier"
75
+ epub.package.save
76
+
77
+ Archive::Zip.open @valid_epub.to_path do |z|
78
+ z.each do |entry|
79
+ assert_equal mtime, entry.mtime
80
+ end
81
+ end
82
+ end
60
83
  end
data/test/test_maker.rb CHANGED
@@ -73,7 +73,6 @@ class TestMaker < Test::Unit::TestCase
73
73
  error_message = error.message
74
74
  end
75
75
  dirname = error_message.match(/\[EPUB::Maker\].*:\s*(.+)\Z/m).captures.last
76
-
77
76
  assert_path_exist dirname
78
77
  end
79
78
 
@@ -2,6 +2,7 @@
2
2
  require_relative 'helper'
3
3
  require 'epub/parser'
4
4
  require 'epub/maker/publication'
5
+ require "date"
5
6
 
6
7
  class TestMakerPublication < Test::Unit::TestCase
7
8
  def setup
@@ -37,4 +38,25 @@ class TestMakerPublication < Test::Unit::TestCase
37
38
  media_type = doc.xpath('/opf:package/opf:bindings/opf:mediaType', EPUB::NAMESPACES).first
38
39
  assert_equal 'application/x-demo-slideshow', media_type['media-type']
39
40
  end
41
+
42
+ def test_modified=
43
+ metadata = EPUB::Publication::Package::Metadata.new
44
+ metadata.modified = "2011-01-01T12:00:00Z"
45
+
46
+ assert_equal 1, metadata.metas.length
47
+ assert_equal "2011-01-01T12:00:00Z", metadata.modified.content
48
+
49
+ metadata.modified = Time.new(2020, 2, 1, 0, 0, 0, "+09:00")
50
+ assert_equal 1, metadata.metas.length
51
+ assert_equal "2020-01-31T15:00:00Z", metadata.modified.content
52
+
53
+ metadata.modified = Date.new(1993, 2, 24)
54
+ assert_equal 1, metadata.metas.length
55
+ expected = Time.new(1993, 2, 24, 0, 0, 0, Time.now.utc_offset)
56
+ assert_equal expected.utc.xmlschema, metadata.modified.content
57
+
58
+ metadata.modified = DateTime.new(1993, 2, 24, 0, 0, 0, "+00:00")
59
+ assert_equal 1, metadata.metas.length
60
+ assert_equal "1993-02-24T00:00:00Z", metadata.modified.content
61
+ end
40
62
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epub-maker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - KITAITI Makoto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-05 00:00:00.000000000 Z
11
+ date: 2021-12-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: epub-parser
@@ -81,25 +81,33 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: 2.3.5
83
83
  - !ruby/object:Gem::Dependency
84
- name: nokogiri
84
+ name: rexml
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: 1.6.0
90
- - - "<"
91
- - !ruby/object:Gem::Version
92
- version: '1.11'
89
+ version: '0'
93
90
  type: :runtime
94
91
  prerelease: false
95
92
  version_requirements: !ruby/object:Gem::Requirement
96
93
  requirements:
97
94
  - - ">="
98
95
  - !ruby/object:Gem::Version
99
- version: 1.6.0
100
- - - "<"
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: nokogiri
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
101
102
  - !ruby/object:Gem::Version
102
- version: '1.11'
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
103
111
  - !ruby/object:Gem::Dependency
104
112
  name: zipruby
105
113
  requirement: !ruby/object:Gem::Requirement
@@ -236,6 +244,7 @@ extra_rdoc_files: []
236
244
  files:
237
245
  - ".gitignore"
238
246
  - ".gitlab-ci.yml"
247
+ - ".yardopts"
239
248
  - CHANGELOG.markdown
240
249
  - Gemfile
241
250
  - LICENSE.txt
@@ -288,7 +297,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
288
297
  - !ruby/object:Gem::Version
289
298
  version: '0'
290
299
  requirements: []
291
- rubygems_version: 3.1.2
300
+ rubygems_version: 3.3.3
292
301
  signing_key:
293
302
  specification_version: 4
294
303
  summary: EPUB Maker