eepub 0.3.1 → 0.3.2

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
  SHA1:
3
- metadata.gz: 025305fc54a605c35c60cfc8cfe7dda9046e7a5d
4
- data.tar.gz: 7d2ff2df5a02f29e0c6611f43993fa08b92f10cc
3
+ metadata.gz: 47898b927d644865e6bfc5acafa1e338b105cade
4
+ data.tar.gz: 8caef5facc55165930418b10b81d476fb7b91dec
5
5
  SHA512:
6
- metadata.gz: 5cb94e81780bf8eb68665fb899f40d6bac235a350012b0caca545edaa48b12d8ed329ae80aefeac78e570c4f57f94aa698e478d76486fd80ce76aa6d2ccd181d
7
- data.tar.gz: 890c918e8afb1ef46254f46597cb0cb1ff5432934a1a59bd630b3609797bf820fb6332d4f7150dd6dbbbd0582135770a85fd472738e3f2772b0827a85d7cbd6c
6
+ metadata.gz: e859a6ee9cf51421654bc57e32d1894427481fab98d9d7d31a404f23312ae22321b4afdc103a195f43b620295fb450b403f8d97481ac776101feeb3231693a18
7
+ data.tar.gz: 1a8cba1bb4e01126b8c70a24b6ee1906151a2aaaea906535cdf0b9a8fb6c478d116467081990dc962b2edda91b38c2ed8b227e2ca80e67505586caf6a2cb018e
@@ -2,8 +2,23 @@ require 'fileutils'
2
2
  require 'rexml/document'
3
3
  require 'zip'
4
4
 
5
+ using Module.new {
6
+ refine Hash do
7
+ def slice(*keys)
8
+ keys.each_with_object({}) {|k, h|
9
+ h[k] = self[k]
10
+ }
11
+ end
12
+ end
13
+ } unless {}.respond_to?(:slice)
14
+
5
15
  module Eepub
6
16
  class Epub
17
+ XMLNS = {
18
+ 'container' => 'urn:oasis:names:tc:opendocument:xmlns:container',
19
+ 'dc' => 'http://purl.org/dc/elements/1.1/',
20
+ }
21
+
7
22
  def initialize(path)
8
23
  @path = path
9
24
  end
@@ -11,10 +26,10 @@ module Eepub
11
26
  attr_reader :path
12
27
 
13
28
  def title
14
- @title ||= Zip::File.open(@path) {|zip|
15
- package_xml = REXML::Document.new(package_entry(zip).get_input_stream)
29
+ @title ||= Zip::File.open(path) {|zip|
30
+ rootfile_doc = REXML::Document.new(rootfile_entry(zip).get_input_stream)
16
31
 
17
- REXML::XPath.first(package_xml, '//dc:title', 'dc' => 'http://purl.org/dc/elements/1.1/').text
32
+ REXML::XPath.first(rootfile_doc, '//dc:title', XMLNS.slice('dc')).text
18
33
  }
19
34
  end
20
35
 
@@ -24,14 +39,13 @@ module Eepub
24
39
  FileUtils.cp path, to unless path == to
25
40
 
26
41
  Zip::File.open to do |zip|
27
- entry = package_entry(zip)
28
- xml = REXML::Document.new(entry.get_input_stream)
29
- title_node = REXML::XPath.first(xml, '//dc:title', 'dc' => 'http://purl.org/dc/elements/1.1/')
42
+ rootfile_entry = rootfile_entry(zip)
43
+ rootfile_doc = rootfile_entry.get_input_stream(&REXML::Document.method(:new))
30
44
 
31
- title_node.text = title
45
+ REXML::XPath.first(rootfile_doc, '//dc:title', XMLNS.slice('dc')).text = title
32
46
 
33
- zip.get_output_stream entry.name do |stream|
34
- stream.write xml.to_s
47
+ zip.get_output_stream rootfile_entry.name do |stream|
48
+ stream.write rootfile_doc.to_s
35
49
  end
36
50
 
37
51
  zip.commit
@@ -40,12 +54,11 @@ module Eepub
40
54
 
41
55
  private
42
56
 
43
- def package_entry(zip)
44
- container_entry = zip.find_entry('META-INF/container.xml')
45
- container_xml = REXML::Document.new(container_entry.get_input_stream)
46
- path = REXML::XPath.first(container_xml, '//container:rootfile/@full-path', 'container' => 'urn:oasis:names:tc:opendocument:xmlns:container').value
57
+ def rootfile_entry(zip)
58
+ container_doc = zip.find_entry('META-INF/container.xml').get_input_stream(&REXML::Document.method(:new))
59
+ rootfile_path = REXML::XPath.first(container_doc, '//container:rootfile/@full-path', XMLNS.slice('container')).value
47
60
 
48
- zip.find_entry(path)
61
+ zip.find_entry(rootfile_path)
49
62
  end
50
63
  end
51
64
  end
@@ -1,3 +1,3 @@
1
1
  module Eepub
2
- VERSION = '0.3.1'
2
+ VERSION = '0.3.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eepub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keita Urashima
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-21 00:00:00.000000000 Z
11
+ date: 2017-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyzip