epub-maker 0.1.2 → 0.1.6

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: 4119c4878b2777ccee127c12fbd897dc771308b306172ca887ff962e07d9bfd1
4
- data.tar.gz: 8393a6d8d9aa4fa369f4ebf8f3247e1e9a5dbdbf96022f90a9b137f88173d4c1
3
+ metadata.gz: cb15b719ce1526ed7cc33d12ca8da5fbd58664a3baedfd72b8f8363795584825
4
+ data.tar.gz: 3c493a4f38d30554736e8d752bdbd43414d261081bfe99717004664b50b94d1e
5
5
  SHA512:
6
- metadata.gz: 5c728e1af722eb6e102b56e2df6b91b0003c7513a5036ff8a68ab26a7adbd6a9a4391205f58a0328718531fb714ae3dac6c570626afceea66d9951874b8b3234
7
- data.tar.gz: 5589187d2a35be80c16a4127ff1f442deb2defd756f7f2feae85aa44da26144fe2c5a03d7ee13f7205eda4b7df5ea6177ab98419a0f56c0239263fd96f640c7a
6
+ metadata.gz: d9386a7a043b34f1dcd91e1bffeef3463b7c0bbb106b133619b18a51fbf053c65285817fb5deb3c0f07e25c5074fbff401fbcb51458b9531c8a6f0b6e65b43cd
7
+ data.tar.gz: bc385ddb0e1a3115c1a5e3039d6638fee5924345a0a3d13e34ae8935ba62c39d4f5b77528816f9c9be7690e47fd2582194019f6f4b9f1e8f43b434375b834a11
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.6
2
+ -----
3
+
4
+ * [BUG FIX]Add `mtime` keyword argument to `OCF::PhysicalContainer::Zipruby#write`
5
+
6
+ 0.1.5
7
+ -----
8
+
9
+ * [BUG FIX]Compare path name in the same encoding when finding file in ZIP archive
10
+ * Add `PhysicalContaienr.mtime` API to set mtime of files in EPUB archive
11
+
12
+ 0.1.4
13
+ -----
14
+
15
+ * Add `Publication::Package::Metadata#modified=` method
16
+ * Rescue Errno::EXDEV on renaming temporary EPUB file
17
+
18
+ 0.1.3
19
+ -----
20
+
21
+ * Add workaround Window file renaming problem
22
+
1
23
  0.1.2
2
24
  ------
3
25
 
data/README.markdown CHANGED
@@ -178,48 +178,19 @@ Todo
178
178
  Recent Changes
179
179
  --------------
180
180
 
181
- ### 0.1.2
181
+ ### 0.1.6
182
182
 
183
- * Close temp file when archiving EPUB file
183
+ * [BUG FIX]Add `mtime` keyword argument to `OCF::PhysicalContainer::Zipruby#write`
184
184
 
185
- ### 0.1.1
185
+ ### 0.1.5
186
186
 
187
- * Update Nokogiri version to < 1.11
187
+ * [BUG FIX]Compare path name in the same encoding when finding file in ZIP archive
188
+ * Add `PhysicalContaienr.mtime` API to set mtime of files in EPUB archive
188
189
 
189
- ### 0.1.0
190
+ ### 0.1.4
190
191
 
191
- * [BUG FIX]Fix the case file extension should be wrong
192
- * Add Nokogiri to runtime dependencies explicitly
193
-
194
- ### 0.0.9
195
-
196
- * Remove [ruby-uuid][] gem from dependencies
197
- * [BUG FIX]Convert Set to Array before writing into XML
198
- * Follow change of EPUB Parser v0.3.6
199
-
200
- [ruby-uuid]: https://github.com/shyouhei/ruby-uuid
201
-
202
- ### 0.0.8
203
-
204
- * Use default temporary directory for `EPUB::Maker.archive`
205
-
206
- ### 0.0.7
207
-
208
- * Change temporary directory used by `EPUB::Maker.archive`
209
-
210
- ### 0.0.6
211
-
212
- * Add `epub-archive` command
213
- * Add `EPUB::Maker.archive` method
214
-
215
- ### 0.0.5
216
-
217
- * Fix bug to modify `dc:rights` to `dc:right`
218
-
219
- ### 0.0.4
220
- * API change: #save -> #write for PhysicalContainer classes
221
- * Bump required EPUB Parser version: 0.2.0 -> 0.2.6
222
- * Deprecate `EPUB::OCF::PhysicalContainer.save`
192
+ * Add `Publication::Package::Metadata#modified=` method
193
+ * Rescue Errno::EXDEV on renaming temporary EPUB file
223
194
 
224
195
  Contributing
225
196
  ------------
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,6 +25,7 @@ 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 'rexml'
28
29
  gem.add_runtime_dependency "nokogiri", ">= 1.6.0", "< 1.11"
29
30
 
30
31
  gem.add_development_dependency 'zipruby'
@@ -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,23 +15,36 @@ 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
30
36
  end
31
37
  end
32
38
  end
33
- ::File.chmod 0666 & ~::File.umask, tmp_archive_path
34
- ::File.rename tmp_archive_path, @container_path
39
+ begin
40
+ ::File.chmod 0666 & ~::File.umask, tmp_archive_path
41
+ ::File.rename tmp_archive_path, @container_path
42
+ rescue Errno::EACCES, Errno::EXDEV
43
+ # In some cases on Windows, we fail to rename the file
44
+ # but succeed to copy although I don't know why.
45
+ # Race condition? I don't know. But no time to dig deeper.
46
+ ::FileUtils.copy tmp_archive_path, @container_path
47
+ end
35
48
  end
36
49
  end
37
50
  end
@@ -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.2"
3
+ VERSION = "0.1.6"
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.2
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - KITAITI Makoto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-31 00:00:00.000000000 Z
11
+ date: 2021-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: epub-parser
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: 2.3.5
83
+ - !ruby/object:Gem::Dependency
84
+ name: rexml
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: nokogiri
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -236,6 +250,7 @@ extra_rdoc_files: []
236
250
  files:
237
251
  - ".gitignore"
238
252
  - ".gitlab-ci.yml"
253
+ - ".yardopts"
239
254
  - CHANGELOG.markdown
240
255
  - Gemfile
241
256
  - LICENSE.txt
@@ -288,7 +303,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
288
303
  - !ruby/object:Gem::Version
289
304
  version: '0'
290
305
  requirements: []
291
- rubygems_version: 3.1.2
306
+ rubygems_version: 3.2.22
292
307
  signing_key:
293
308
  specification_version: 4
294
309
  summary: EPUB Maker