open_xml_package 0.1.0 → 0.2.0.beta1

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: b7c6df8561b12ab2eba68c491b536858c24d1ac7
4
- data.tar.gz: 072033ceed3d330101adade0edd83311db91fbc0
3
+ metadata.gz: 4ab514d2a5048f5d9cebb1ce98e5ee05b0771c2b
4
+ data.tar.gz: ca0ea6e3605293570f36e607bd6d66a15ae6acbc
5
5
  SHA512:
6
- metadata.gz: 78ee971bfc8d827ca20a977004fe0bf89c09d4f15eab9f23fa4aebb559052a4215964cf638fff28fca8b3042d5b80d3e4a771504e728f6e2d4f4c5e8db7143ef
7
- data.tar.gz: 7ddae79c7891090e17282dd30470e2f3d315176c8b81f7b7376f234ca1d6eb57445e23fe1aae7a9362b7d3f7dbf043960fd0ad36b4f33340f02a2434bc80bfa3
6
+ metadata.gz: 20d8874515ce63b2110724b7b9e2bc4dd451931e1cdda5f4f163cdd1421fd3795211a82b28d9a2516a465567cb51ce4e851f524b2372fc61500e2aec78e95359
7
+ data.tar.gz: 642cf512e5dcc44b3c426db90df1f1002a748adc2521fc7325b3ca7f5d05a3253e03a43ee567baec3b7419373f7398e5ee6373fe49d108cdab2d4f1cdadb6261
@@ -0,0 +1,45 @@
1
+ # Constructing a large XML document (5MB) with the Ox
2
+ # gem is about 4x faster than with Nokogiri and about
3
+ # 5x fater than with Builder.
4
+ #
5
+ # This class mimics the XML Builder DSL.
6
+ require "ox"
7
+
8
+ module OpenXml
9
+ class Builder
10
+
11
+ def initialize
12
+ @document = Ox::Document.new(version: "1.0")
13
+ @current = @document
14
+ yield self if block_given?
15
+ end
16
+
17
+ def to_s
18
+ Ox.dump @document
19
+ end
20
+ alias :to_xml :to_s
21
+
22
+ def method_missing(tag_name, *args)
23
+ new_element = Ox::Element.new(tag_name)
24
+ attributes = args.extract_options!
25
+ attributes.each do |key, value|
26
+ new_element[key] = value
27
+ end
28
+
29
+ if block_given?
30
+ begin
31
+ was_current = @current
32
+ @current = new_element
33
+ yield self
34
+ ensure
35
+ @current = was_current
36
+ end
37
+ elsif value = args.first
38
+ new_element << value.to_s
39
+ end
40
+
41
+ @current << new_element
42
+ end
43
+
44
+ end
45
+ end
@@ -1,8 +1,8 @@
1
1
  module OpenXml
2
2
  module Errors
3
-
3
+
4
4
  class MissingContentTypesPart < StandardError
5
5
  end
6
-
6
+
7
7
  end
8
8
  end
@@ -16,11 +16,11 @@ module OpenXml
16
16
  def content_types_presets
17
17
  @content_types_presets ||= OpenXml::ContentTypesPresets.new
18
18
  end
19
-
19
+
20
20
  def content_types(&block)
21
21
  content_types_presets.instance_eval &block
22
22
  end
23
-
23
+
24
24
  def open(path)
25
25
  if block_given?
26
26
  Zip::File.open(path) do |zipfile|
@@ -33,7 +33,7 @@ module OpenXml
33
33
 
34
34
  def from_stream(stream)
35
35
  stream = StringIO.new(stream) if stream.is_a?(String)
36
-
36
+
37
37
  # Hack: Zip::Entry.read_c_dir_entry initializes
38
38
  # a new Zip::Entry by calling `io.path`. Zip::Entry
39
39
  # uses this to open the original zipfile; but in
@@ -41,7 +41,7 @@ module OpenXml
41
41
  def stream.path
42
42
  self
43
43
  end
44
-
44
+
45
45
  zipfile = ::Zip::File.new("", true, true)
46
46
  zipfile.read_from_stream(stream)
47
47
  new(zipfile)
@@ -53,7 +53,7 @@ module OpenXml
53
53
  def initialize(zipfile=nil)
54
54
  @zipfile = zipfile
55
55
  @parts = {}
56
-
56
+
57
57
  if zipfile
58
58
  read_zipfile!
59
59
  else
data/lib/open_xml/part.rb CHANGED
@@ -1,16 +1,16 @@
1
1
  require "nokogiri"
2
+ require "open_xml/builder"
2
3
 
3
4
  module OpenXml
4
5
  class Part
5
6
  include ::Nokogiri
6
7
 
7
8
  def build_xml
8
- XML::Builder.new(encoding: "utf-8") { |xml| yield xml }.to_xml
9
+ OpenXml::Builder.new { |xml| yield xml }.to_xml
9
10
  end
10
11
 
11
- def build_standalone_xml
12
- xml = Nokogiri::XML("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>")
13
- XML::Builder.with(xml) { |xml| yield xml }.to_xml
12
+ def build_standalone_xml(&block)
13
+ "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" + build_xml(&block)
14
14
  end
15
15
 
16
16
  def read
@@ -3,7 +3,7 @@ require "zip"
3
3
  module Zip
4
4
  class InputStream
5
5
  protected
6
-
6
+
7
7
  # The problem in RubyZip 1.1.0 is that we only call `seek`
8
8
  # when `io` is a File. We need to move the cursor to the
9
9
  # right position when `io` is a StringIO as well.
@@ -12,6 +12,6 @@ module Zip
12
12
  io.seek(offset, ::IO::SEEK_SET)
13
13
  io
14
14
  end
15
-
15
+
16
16
  end
17
17
  end
@@ -1,3 +1,3 @@
1
1
  module OpenXmlPackage
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0.beta1"
3
3
  end
@@ -20,14 +20,17 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_dependency "rubyzip", "~> 1.1.0"
22
22
  spec.add_dependency "nokogiri"
23
+ spec.add_dependency "ox"
23
24
 
24
25
  spec.add_development_dependency "bundler", "~> 1.6"
25
26
  spec.add_development_dependency "rake"
26
27
  spec.add_development_dependency "rails", ">= 3.2", "< 5.0"
27
- spec.add_development_dependency "turn"
28
+ spec.add_development_dependency "minitest"
29
+ spec.add_development_dependency "minitest-reporters"
30
+ spec.add_development_dependency "minitest-reporters-turn_reporter"
28
31
  spec.add_development_dependency "pry"
29
32
  spec.add_development_dependency "rr"
30
33
  spec.add_development_dependency "simplecov"
31
34
  spec.add_development_dependency "shoulda-context"
32
-
35
+
33
36
  end
@@ -2,33 +2,33 @@ require "test_helper"
2
2
 
3
3
  class ContentTypesTest < ActiveSupport::TestCase
4
4
  attr_reader :content_types
5
-
5
+
6
6
  WORDPROCESSING_DOCUMENT_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"
7
-
7
+
8
8
  setup do
9
9
  @content_types = OpenXml::Parts::ContentTypes.new(
10
10
  {"xml" => OpenXml::Types::XML, "rels" => OpenXml::Types::RELATIONSHIPS},
11
11
  {"word/document.xml" => WORDPROCESSING_DOCUMENT_TYPE})
12
12
  end
13
-
14
-
13
+
14
+
15
15
  context "Given a path without an override" do
16
16
  should "identify the content type from its extension" do
17
17
  assert_equal OpenXml::Types::XML, content_types.of("content/some.xml")
18
18
  end
19
19
  end
20
-
20
+
21
21
  context "Given a path with an override" do
22
22
  should "identify the content type from its path" do
23
23
  assert_equal WORDPROCESSING_DOCUMENT_TYPE, content_types.of("word/document.xml")
24
24
  end
25
25
  end
26
-
26
+
27
27
  context "Given a path with an unrecognized extension" do
28
28
  should "be nil" do
29
29
  assert_equal nil, content_types.of("img/screenshot.jpg")
30
30
  end
31
31
  end
32
-
33
-
32
+
33
+
34
34
  end
@@ -4,9 +4,9 @@ require "set"
4
4
 
5
5
  class OpenXmlPackageTest < ActiveSupport::TestCase
6
6
  attr_reader :package, :temp_file
7
-
8
-
9
-
7
+
8
+
9
+
10
10
  context "#add_part" do
11
11
  should "accept a path and a part" do
12
12
  package = OpenXml::Package.new
@@ -15,21 +15,21 @@ class OpenXmlPackageTest < ActiveSupport::TestCase
15
15
  end
16
16
  end
17
17
  end
18
-
19
-
20
-
18
+
19
+
20
+
21
21
  context "Writing" do
22
22
  setup do
23
23
  @temp_file = expand_path "../tmp/test.zip"
24
24
  FileUtils.rm temp_file, force: true
25
25
  end
26
-
26
+
27
27
  context "Given a simple part" do
28
28
  setup do
29
29
  @package = OpenXml::Package.new
30
30
  package.add_part "content/document.xml", OpenXml::Parts::UnparsedPart.new(document_content)
31
31
  end
32
-
32
+
33
33
  should "write a valid zip file with the expected parts" do
34
34
  package.write_to temp_file
35
35
  assert File.exists?(temp_file), "Expected the file #{temp_file.inspect} to have been created"
@@ -38,9 +38,9 @@ class OpenXmlPackageTest < ActiveSupport::TestCase
38
38
  end
39
39
  end
40
40
  end
41
-
42
-
43
-
41
+
42
+
43
+
44
44
  context "Reading" do
45
45
  context "Given a sample Word document" do
46
46
  setup do
@@ -61,64 +61,64 @@ class OpenXmlPackageTest < ActiveSupport::TestCase
61
61
  "word/theme/theme1.xml",
62
62
  "word/webSettings.xml" ]
63
63
  end
64
-
64
+
65
65
  context ".open" do
66
66
  setup do
67
67
  @package = OpenXml::Package.open(temp_file)
68
68
  end
69
-
69
+
70
70
  teardown do
71
71
  package.close
72
72
  end
73
-
73
+
74
74
  should "discover the expected parts" do
75
75
  assert_equal @expected_contents, package.parts.keys.to_set
76
76
  end
77
-
77
+
78
78
  should "read their content on-demand" do
79
79
  assert_equal web_settings_content, package.get_part("word/webSettings.xml").content
80
80
  end
81
81
  end
82
-
82
+
83
83
  context ".from_stream" do
84
84
  setup do
85
85
  @package = OpenXml::Package.from_stream(File.open(temp_file, "rb", &:read))
86
86
  end
87
-
87
+
88
88
  should "also discover the expected parts" do
89
89
  assert_equal @expected_contents, package.parts.keys.to_set
90
90
  end
91
-
91
+
92
92
  should "read their content" do
93
93
  assert_equal web_settings_content, package.get_part("word/webSettings.xml").content
94
94
  end
95
95
  end
96
-
96
+
97
97
  context "ContentTypes" do
98
98
  setup do
99
99
  @package = OpenXml::Package.open(temp_file)
100
100
  end
101
-
101
+
102
102
  teardown do
103
103
  package.close
104
104
  end
105
-
105
+
106
106
  should "be parsed" do
107
107
  assert_equal %w{jpeg png rels xml}, package.content_types.defaults.keys.sort
108
108
  assert_equal "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml",
109
109
  package.content_types.overrides["/word/document.xml"]
110
110
  end
111
111
  end
112
-
112
+
113
113
  context "Rels" do
114
114
  setup do
115
115
  @package = OpenXml::Package.open(temp_file)
116
116
  end
117
-
117
+
118
118
  teardown do
119
119
  package.close
120
120
  end
121
-
121
+
122
122
  should "be parsed" do
123
123
  assert_equal %w{docProps/core.xml docProps/app.xml word/document.xml docProps/thumbnail.jpeg},
124
124
  package.rels.map(&:target)
@@ -126,11 +126,11 @@ class OpenXmlPackageTest < ActiveSupport::TestCase
126
126
  end
127
127
  end
128
128
  end
129
-
130
-
131
-
129
+
130
+
131
+
132
132
  private
133
-
133
+
134
134
  def document_content
135
135
  <<-STR
136
136
  <document>
@@ -138,13 +138,13 @@ private
138
138
  </document>
139
139
  STR
140
140
  end
141
-
141
+
142
142
  def web_settings_content
143
143
  "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n<w:webSettings xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\" mc:Ignorable=\"w14\"><w:allowPNG/><w:doNotSaveAsSingleFile/></w:webSettings>"
144
144
  end
145
-
145
+
146
146
  def expand_path(path)
147
147
  File.expand_path(File.join(File.dirname(__FILE__), path))
148
148
  end
149
-
149
+
150
150
  end
data/test/test_helper.rb CHANGED
@@ -7,9 +7,10 @@ end
7
7
 
8
8
  require "rails"
9
9
  require "rails/test_help"
10
- require "turn"
11
10
  require "pry"
12
11
  require "rr"
13
12
  require "shoulda/context"
13
+ require "minitest/reporters/turn_reporter"
14
+ MiniTest::Reporters.use! Minitest::Reporters::TurnReporter.new
14
15
 
15
16
  require "open_xml_package"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_xml_package
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bob Lail
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-20 00:00:00.000000000 Z
11
+ date: 2015-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyzip
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ox
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -87,7 +101,35 @@ dependencies:
87
101
  - !ruby/object:Gem::Version
88
102
  version: '5.0'
89
103
  - !ruby/object:Gem::Dependency
90
- name: turn
104
+ name: minitest
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: minitest-reporters
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: minitest-reporters-turn_reporter
91
133
  requirement: !ruby/object:Gem::Requirement
92
134
  requirements:
93
135
  - - ">="
@@ -168,6 +210,7 @@ files:
168
210
  - LICENSE.txt
169
211
  - README.md
170
212
  - Rakefile
213
+ - lib/open_xml/builder.rb
171
214
  - lib/open_xml/content_types_presets.rb
172
215
  - lib/open_xml/errors.rb
173
216
  - lib/open_xml/package.rb
@@ -200,12 +243,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
200
243
  version: '0'
201
244
  required_rubygems_version: !ruby/object:Gem::Requirement
202
245
  requirements:
203
- - - ">="
246
+ - - ">"
204
247
  - !ruby/object:Gem::Version
205
- version: '0'
248
+ version: 1.3.1
206
249
  requirements: []
207
250
  rubyforge_project:
208
- rubygems_version: 2.2.2
251
+ rubygems_version: 2.4.8
209
252
  signing_key:
210
253
  specification_version: 4
211
254
  summary: A Ruby implementation of OpenXmlPackage