epub-maker 0.1.0 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 79b98ceec5a5bd31ccbcea077c5c109e11b98c64acab7fa9c6d2eb4ede22b25c
4
- data.tar.gz: e1520191cd13384acd70c13e5a7d390587bb6238c535ba3971e5d8485d3c1920
3
+ metadata.gz: 4aeffa7298ad5bb22c54beaa0eaafc0f46cbcd724a950202d917ef9186345a5c
4
+ data.tar.gz: ea10fe12472421d4585636290c814df4eab87d9d4fc466a480d94d229174d3c4
5
5
  SHA512:
6
- metadata.gz: 8c6d10a5797d1ee3f66fc520875b3d569e746841f70badfce7f77cbf15e9b67121e09d9e6144ec3cde21ec92eb6afb303849db2939866a49fe61a0b65195fb79
7
- data.tar.gz: cfb54f2c6bff8808e0a846d2f94d0f5668bb1e1e267b5e4d64725ccaa0182f66f146f9c1ca963858f6e3ff5d93882c813798b34d0d024fb684d084cf1eaaf3c1
6
+ metadata.gz: 3ef569b71794f317b6a05136bd94ff9dcee19caa0993d58ab7f1f4d5b03bba2edc9836300892e5b1780ce0afe1b1498b84bfcbd884c9d6fe4ad9e82b1ef191a4
7
+ data.tar.gz: 39e9011f7bcac33805936f1842959a697bbf8b72d415b7b6cdd82b0dc782548316f369951e7aac68c299f1f50f40058239316868bd55de4841dc92da81775c23
@@ -38,7 +38,7 @@ test:2.5:
38
38
  - deps
39
39
 
40
40
  test:2.6:
41
- image: ruby:2.6-rc
41
+ image: ruby:2.6
42
42
  script: bundle exec rake test
43
43
  except:
44
44
  - tags
@@ -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
@@ -1,3 +1,30 @@
1
+ 0.1.5
2
+ -----
3
+
4
+ * [BUG FIX]Compare path name in the same encoding when finding file in ZIP archive
5
+ * Add `PhysicalContaienr.mtime` API to set mtime of files in EPUB archive
6
+
7
+ 0.1.4
8
+ -----
9
+
10
+ * Add `Publication::Package::Metadata#modified=` method
11
+ * Rescue Errno::EXDEV on renaming temporary EPUB file
12
+
13
+ 0.1.3
14
+ -----
15
+
16
+ * Add workaround Window file renaming problem
17
+
18
+ 0.1.2
19
+ ------
20
+
21
+ * Close temp file when archiving EPUB file
22
+
23
+ 0.1.1
24
+ -----
25
+
26
+ * Update Nokogiri version to < 1.11
27
+
1
28
  0.1.0
2
29
  -----
3
30
 
@@ -178,40 +178,19 @@ Todo
178
178
  Recent Changes
179
179
  --------------
180
180
 
181
- ### 0.1.0
181
+ ### 0.1.5
182
182
 
183
- * [BUG FIX]Fix the case file extension should be wrong
184
- * Add Nokogiri to runtime dependencies explicitly
183
+ * [BUG FIX]Compare path name in the same encoding when finding file in ZIP archive
184
+ * Add `PhysicalContaienr.mtime` API to set mtime of files in EPUB archive
185
185
 
186
- ### 0.0.9
186
+ ### 0.1.4
187
187
 
188
- * Remove [ruby-uuid][] gem from dependencies
189
- * [BUG FIX]Convert Set to Array before writing into XML
190
- * Follow change of EPUB Parser v0.3.6
188
+ * Add `Publication::Package::Metadata#modified=` method
189
+ * Rescue Errno::EXDEV on renaming temporary EPUB file
191
190
 
192
- [ruby-uuid]: https://github.com/shyouhei/ruby-uuid
191
+ ### 0.1.3
193
192
 
194
- ### 0.0.8
195
-
196
- * Use default temporary directory for `EPUB::Maker.archive`
197
-
198
- ### 0.0.7
199
-
200
- * Change temporary directory used by `EPUB::Maker.archive`
201
-
202
- ### 0.0.6
203
-
204
- * Add `epub-archive` command
205
- * Add `EPUB::Maker.archive` method
206
-
207
- ### 0.0.5
208
-
209
- * Fix bug to modify `dc:rights` to `dc:right`
210
-
211
- ### 0.0.4
212
- * API change: #save -> #write for PhysicalContainer classes
213
- * Bump required EPUB Parser version: 0.2.0 -> 0.2.6
214
- * Deprecate `EPUB::OCF::PhysicalContainer.save`
193
+ * Add workaround Window file renaming problem
215
194
 
216
195
  Contributing
217
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
@@ -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.9"
28
+ gem.add_runtime_dependency 'rexml'
29
+ gem.add_runtime_dependency "nokogiri", ">= 1.6.0", "< 1.11"
29
30
 
30
31
  gem.add_development_dependency 'zipruby'
31
32
  gem.add_development_dependency 'test-unit'
@@ -76,7 +76,9 @@ module EPUB
76
76
  raise "source directory #{source_dir} not exist" unless source_dir.exist?
77
77
 
78
78
  epub_file = Pathname(epub_file)
79
- temp_dest = Pathname(Tempfile.create(epub_file.basename.to_path, epub_file.dirname.to_path))
79
+ temp_dest_file = Tempfile.create(epub_file.basename.to_path, epub_file.dirname.to_path)
80
+ temp_dest_file.close
81
+ temp_dest = Pathname(temp_dest_file)
80
82
  Pathname.mktmpdir "epub-maker" do |dir|
81
83
  temp_container = dir/source_dir.basename
82
84
 
@@ -13,15 +13,33 @@ 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
+ # @return [Time]
30
+ def mtime=(time)
31
+ @@mtime = time
32
+ end
33
+
16
34
  def write(container_path, path_name, content)
17
35
  open(container_path) {|container|
18
- container.write(path_name, content)
36
+ container.write(path_name, content, mtime: mtime)
19
37
  }
20
38
  end
21
39
 
22
40
  def save(container_path, path_name, content)
23
41
  warn "EPUB::OCF::PhysicalContainer.#{__method__} is deprecated. Use .write instead"
24
- write(container_path, path_name, content)
42
+ write(container_path, path_name, content, mtime: mtime)
25
43
  end
26
44
  end
27
45
  end
@@ -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
@@ -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.0"
3
+ VERSION = "0.1.5"
4
4
  end
5
5
  end
@@ -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
@@ -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.0
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - KITAITI Makoto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-29 00:00:00.000000000 Z
11
+ date: 2020-10-31 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
@@ -89,7 +103,7 @@ dependencies:
89
103
  version: 1.6.0
90
104
  - - "<"
91
105
  - !ruby/object:Gem::Version
92
- version: '1.9'
106
+ version: '1.11'
93
107
  type: :runtime
94
108
  prerelease: false
95
109
  version_requirements: !ruby/object:Gem::Requirement
@@ -99,7 +113,7 @@ dependencies:
99
113
  version: 1.6.0
100
114
  - - "<"
101
115
  - !ruby/object:Gem::Version
102
- version: '1.9'
116
+ version: '1.11'
103
117
  - !ruby/object:Gem::Dependency
104
118
  name: zipruby
105
119
  requirement: !ruby/object:Gem::Requirement
@@ -288,8 +302,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
288
302
  - !ruby/object:Gem::Version
289
303
  version: '0'
290
304
  requirements: []
291
- rubyforge_project:
292
- rubygems_version: 2.7.6
305
+ rubygems_version: 3.1.4
293
306
  signing_key:
294
307
  specification_version: 4
295
308
  summary: EPUB Maker