openxml-package 0.2.0 → 0.2.1

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
  SHA1:
3
- metadata.gz: 9125838f28df960214ac225121b29ecabee182e4
4
- data.tar.gz: 23eefb7c402ecf3b0c500c1241a9e5105c24a7e3
3
+ metadata.gz: 7d6a0da91c83a3de0209c39c9d29016da44e3ffc
4
+ data.tar.gz: 08f26c9b65ec985d6b784341cd288b15f58cea8d
5
5
  SHA512:
6
- metadata.gz: b22bbdd8a0064445003508a9f5c96528cf5950725f863a5c8eba95389885f3767f6c5d44ec325bb4a7e451cdfe788d84afaf2445454472d437606b182d69c93b
7
- data.tar.gz: 81aa9f271fb5f65ca9e31c5b6a03b99d8f701003d1fd534749c5c87863cc84eb12b72f717e49065d4007502cada293c6ff9b31db6f6b2637ece7c64b2196fd63
6
+ metadata.gz: da26c7fc3fa5e983c0a6f78ebb5149e3ca562991ba182e44d516a226451ac95522ee41a09ca8c4c1ccc6c328eac29d595c14bc06055d6fb3bb5030c6fcbe1032
7
+ data.tar.gz: 6965c0f5343a7328c78a7a6fba636dad620f9ed316e223e82a2ef6d153f79dcae22f63cd23b7c5d30ff2ba962981d32bd149a35c1532b97f896533d5843574d5
@@ -0,0 +1,43 @@
1
+ module OpenXml
2
+ class Builder
3
+ class Element < SimpleDelegator
4
+ attr_reader :namespace
5
+ attr_accessor :parent
6
+
7
+ def initialize(*args)
8
+ super Ox::Element.new(*args)
9
+ end
10
+
11
+ def []=(attribute, value)
12
+ namespace_def = attribute.downcase.to_s.match /^xmlns(?:\:(?<prefix>.*))?$/
13
+ add_namespace(namespace_def[:prefix], value.to_s) if namespace_def
14
+ super
15
+ end
16
+
17
+ def namespace=(ns)
18
+ @namespace = ns
19
+ tag = name.match(/^(?:\w*?\:)?(?<tag>\w*)$/i)[:tag]
20
+ self.value = "#{namespace.prefix}:#{tag}" if namespace.is_a? OpenXml::Builder::Namespace
21
+ self.value = "#{namespace}:#{tag}" if namespace.is_a? String
22
+ end
23
+
24
+ def namespaces
25
+ @namespaces ||= []
26
+ end
27
+ alias :namespace_definitions :namespaces
28
+
29
+ def ancestors
30
+ parents = [self]
31
+ parents << parent.ancestors unless parent.nil? || !parent.respond_to?(:ancestors)
32
+ parents.flatten
33
+ end
34
+
35
+ private
36
+
37
+ def add_namespace(prefix, uri)
38
+ namespaces << OpenXml::Builder::Namespace.new(prefix, uri)
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,13 @@
1
+ module OpenXml
2
+ class Builder
3
+ class Namespace
4
+ attr_accessor :prefix, :uri
5
+
6
+ def initialize(prefix, uri)
7
+ @prefix = prefix.to_s
8
+ @uri = uri
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -4,41 +4,88 @@
4
4
  #
5
5
  # This class mimics the XML Builder DSL.
6
6
  require "ox"
7
+ require "openxml/builder/element"
8
+ require "openxml/builder/namespace"
7
9
 
8
10
  module OpenXml
9
11
  class Builder
12
+ attr_reader :parent
10
13
 
11
- def initialize
12
- @document = Ox::Document.new(version: "1.0")
13
- @current = @document
14
+ def initialize(options={})
15
+ @options = {
16
+ with_xml: true,
17
+ encoding: "utf-8"
18
+ }.merge(options)
19
+ @options[:with_xml] = !!@options[:with_xml] || @options[:standalone] == :yes
20
+
21
+ @document = Ox::Document.new({version: "1.0"}.merge(@options))
22
+ @parent = @document
14
23
  yield self if block_given?
15
24
  end
16
25
 
17
26
  def to_s
18
- Ox.dump @document
27
+ Ox.dump @document, @options
19
28
  end
20
29
  alias :to_xml :to_s
21
30
 
31
+ # Adapted from Nokogiri's builder.rb
32
+ def [](ns)
33
+ if @parent != @document
34
+ @ns = @parent.namespace_definitions.find { |x| x.prefix == ns.to_s }
35
+ end
36
+ return self if @ns
37
+
38
+ @parent.ancestors.each do |a|
39
+ next if a == @document
40
+ @ns = a.namespace_definitions.find { |x| x.prefix == ns.to_s }
41
+ return self if @ns
42
+ end
43
+
44
+ @ns = { :pending => ns.to_s }
45
+ return self
46
+ end
47
+
22
48
  def method_missing(tag_name, *args)
23
- new_element = Ox::Element.new(tag_name)
24
- attributes = args.extract_options!
49
+ new_element = OpenXml::Builder::Element.new(tag_name)
50
+ new_element.parent = @parent
51
+ attributes = extract_options!(args)
25
52
  attributes.each do |key, value|
26
53
  new_element[key] = value
27
54
  end
28
55
 
56
+ # Adapted from Nokogiri's builder.rb
57
+ if @ns.is_a? OpenXml::Builder::Namespace
58
+ new_element.namespace = @ns
59
+ elsif @ns.is_a? Hash
60
+ new_element.namespace = new_element.namespace_definitions.find { |x| x.prefix == @ns[:pending] }
61
+ raise ArgumentError, "Namespace #{@ns[:pending]} has not been defined" if new_element.namespace.nil?
62
+ end
63
+
64
+ @ns = nil
65
+
29
66
  if block_given?
30
67
  begin
31
- was_current = @current
32
- @current = new_element
68
+ was_current = @parent
69
+ @parent = new_element
33
70
  yield self
34
71
  ensure
35
- @current = was_current
72
+ @parent = was_current
36
73
  end
37
74
  elsif value = args.first
38
75
  new_element << value.to_s
39
76
  end
40
77
 
41
- @current << new_element
78
+ @parent << new_element.__getobj__
79
+ end
80
+
81
+ private
82
+
83
+ def extract_options!(args)
84
+ if args.last.is_a?(Hash) && args.last.instance_of?(Hash)
85
+ args.pop
86
+ else
87
+ {}
88
+ end
42
89
  end
43
90
 
44
91
  end
data/lib/openxml/part.rb CHANGED
@@ -1,16 +1,14 @@
1
- require "nokogiri"
2
1
  require "openxml/builder"
3
2
 
4
3
  module OpenXml
5
4
  class Part
6
- include ::Nokogiri
7
5
 
8
- def build_xml
9
- OpenXml::Builder.new { |xml| yield xml }.to_xml
6
+ def build_xml(options={})
7
+ OpenXml::Builder.new(options) { |xml| yield xml }.to_xml
10
8
  end
11
9
 
12
10
  def build_standalone_xml(&block)
13
- "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" + build_xml(&block)
11
+ build_xml({ standalone: :yes }, &block)
14
12
  end
15
13
 
16
14
  def read
@@ -1,3 +1,3 @@
1
1
  module OpenXmlPackage
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/test/part_test.rb ADDED
@@ -0,0 +1,71 @@
1
+ require "test_helper"
2
+ require "fileutils"
3
+ require "set"
4
+
5
+ class OpenXmlPartTest < ActiveSupport::TestCase
6
+ attr_reader :part, :builder
7
+
8
+
9
+
10
+ context "Building" do
11
+ setup do
12
+ @part = OpenXml::Part.new
13
+ end
14
+
15
+ context "Given simple xml for one part" do
16
+ setup do
17
+ @builder = @part.build_xml do |xml|
18
+ xml.document do |xml|
19
+ 2.times { xml.child({ attribute: "value", other_attr: "other value" }) }
20
+ end
21
+ end
22
+ end
23
+
24
+ should "build the expected xml" do
25
+ assert_equal basic_xml, builder.to_s
26
+ end
27
+ end
28
+
29
+ context "Given namespaced xml for one part" do
30
+ setup do
31
+ @builder = @part.build_xml do |xml|
32
+ xml.document({ "xmlns:ns" => "some:namespace:uri" }) do
33
+ 2.times { xml["ns"].child({ attribute: "value", other_attr: "other value" }) }
34
+ end
35
+ end
36
+ end
37
+
38
+ should "build the expected xml" do
39
+ assert_equal namespaced_xml, builder.to_s
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+
46
+
47
+ private
48
+
49
+
50
+ def basic_xml
51
+ <<-STR
52
+ <?xml version="1.0" encoding="utf-8"?>
53
+ <document>
54
+ <child attribute="value" other_attr="other value"/>
55
+ <child attribute="value" other_attr="other value"/>
56
+ </document>
57
+ STR
58
+ end
59
+
60
+ def namespaced_xml
61
+ <<-STR
62
+ <?xml version="1.0" encoding="utf-8"?>
63
+ <document xmlns:ns="some:namespace:uri">
64
+ <ns:child attribute="value" other_attr="other value"/>
65
+ <ns:child attribute="value" other_attr="other value"/>
66
+ </document>
67
+ STR
68
+ end
69
+
70
+
71
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openxml-package
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
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-11-20 00:00:00.000000000 Z
11
+ date: 2015-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyzip
@@ -213,6 +213,8 @@ files:
213
213
  - lib/openxml-package.rb
214
214
  - lib/openxml-package/version.rb
215
215
  - lib/openxml/builder.rb
216
+ - lib/openxml/builder/element.rb
217
+ - lib/openxml/builder/namespace.rb
216
218
  - lib/openxml/content_types_presets.rb
217
219
  - lib/openxml/errors.rb
218
220
  - lib/openxml/package.rb
@@ -226,6 +228,7 @@ files:
226
228
  - openxml-package.gemspec
227
229
  - test/content_types_test.rb
228
230
  - test/package_test.rb
231
+ - test/part_test.rb
229
232
  - test/support/sample.docx
230
233
  - test/test_helper.rb
231
234
  homepage: https://github.com/openxml/openxml-package
@@ -255,5 +258,6 @@ summary: A Ruby implementation of OpenXmlPackage
255
258
  test_files:
256
259
  - test/content_types_test.rb
257
260
  - test/package_test.rb
261
+ - test/part_test.rb
258
262
  - test/support/sample.docx
259
263
  - test/test_helper.rb