nokogiri-streaming-reader 0.0.4 → 1.1

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: 1eb4862727aea104efe91c018f0a9e2a0028cb14
4
- data.tar.gz: fd0873746033091ef29e77e69010ef6fe79c96be
3
+ metadata.gz: 37fc22b99e9fd94795e988f57082726da98ef508
4
+ data.tar.gz: e3bb119767f3a94f69f6f9bad22c5a77c515d58d
5
5
  SHA512:
6
- metadata.gz: c339ad735fc8f065a6f73ed3eba1e9e5aadf32cf2fe07bc348c308d8945d98cfa72abd48bef9f7f5ba574446b98fc4032dbe19acd35cc30e93f9b0b126888b6c
7
- data.tar.gz: e9ab7271bfaf71a817d4fff7da753b793c197332ea74fe1a9d43405c872c5f7f1f3aa7f9518d08b7ccb1eb4665787abce484cb2669bb06d15618f6799aafbe83
6
+ metadata.gz: bb074d9be0e846dd89a034f026ba01c38224426a94f7cbba99dfd623d1a33dd804b53c52ff3a5300c2150ddbd4cc23c7b7e58f56dec73dfabb168841800fbb92
7
+ data.tar.gz: 79d289710b6dc9869e59639bfafb33806299021f0ec8fa42429cb7d7c01600b962ba9bc0682aad1950936a0ac5438c5005acab61521e7c695f1543fbc4f0e54a
@@ -34,7 +34,8 @@ module Nokogiri
34
34
  @source = IOWrapper.new(source)
35
35
  @parser = Nokogiri::XML::SAX::Parser.new(self)
36
36
  @stack = []
37
- @triggers = {}
37
+ @end_triggers = {}
38
+ @start_triggers = {}
38
39
  end
39
40
 
40
41
  def run
@@ -42,7 +43,11 @@ module Nokogiri
42
43
  end
43
44
 
44
45
  def on(path, &block)
45
- (@triggers[path] ||= []).push(block)
46
+ (@end_triggers[path] ||= []).push(block)
47
+ end
48
+
49
+ def on_start(path, &block)
50
+ (@start_triggers[path] ||= []).push(block)
46
51
  end
47
52
 
48
53
  def current_path
@@ -70,19 +75,27 @@ module Nokogiri
70
75
  def start_element(name, attrs = [])
71
76
  if @current
72
77
  element = @current.document.create_element(name)
73
- attrs.each do |name, value|
74
- element[name] = value
78
+ attrs.each do |attrname, value|
79
+ element[attrname] = value
75
80
  end
76
81
  @current.add_child(element)
77
82
  @current = element
78
- elsif @triggers[current_path + '/' + name]
83
+ elsif @end_triggers.include?(current_path + '/' + name)
79
84
  fragment = Nokogiri::XML::DocumentFragment.new(Nokogiri::XML::Document.new)
80
-
81
85
  element = fragment.document.create_element(name)
82
- attrs.each do |name, value|
83
- element[name] = value
86
+ attrs.each do |attrname, value|
87
+ element[attrname] = value
84
88
  end
85
89
  @current = element
90
+ elsif (triggers = @start_triggers[current_path + '/' + name])
91
+ fragment = Nokogiri::XML::DocumentFragment.new(Nokogiri::XML::Document.new)
92
+ element = fragment.document.create_element(name)
93
+ attrs.each do |attrname, value|
94
+ element[attrname] = value
95
+ end
96
+ triggers.each do |proc|
97
+ proc.call(element)
98
+ end
86
99
  end
87
100
  @stack.push(name)
88
101
  end
@@ -97,8 +110,7 @@ module Nokogiri
97
110
  @current = @current.parent
98
111
  end
99
112
 
100
- triggers = @triggers[path]
101
- if triggers
113
+ if (triggers = @end_triggers[path])
102
114
  triggers.each do |proc|
103
115
  proc.call(element)
104
116
  end
@@ -1,5 +1,5 @@
1
1
  module Nokogiri
2
2
  module Streaming
3
- VERSION = '0.0.4'
3
+ VERSION = '1.1'
4
4
  end
5
5
  end
@@ -6,62 +6,86 @@ describe Nokogiri::Streaming::Reader do
6
6
  Nokogiri::Streaming::Reader
7
7
  end
8
8
 
9
- it 'parses registered paths' do
10
- doc = %{
11
- <root>
12
- <fruit/>
13
- <fruit/>
14
- <vegetable id='1'><seed/></vegetable>
15
- <meat><type>beef</type></meat>
16
- </root>
17
- }
9
+ describe "#on" do
10
+ it 'parses registered paths' do
11
+ doc = StringIO.new %{
12
+ <root>
13
+ <fruit/>
14
+ <fruit/>
15
+ <vegetable id='1'><seed/></vegetable>
16
+ <meat><type>beef</type></meat>
17
+ </root>
18
+ }
18
19
 
19
- fruits = []
20
- vegetables = []
21
- meats = []
20
+ fruits = []
21
+ vegetables = []
22
+ meats = []
22
23
 
23
- reader = subject.new(doc)
24
- reader.on('/root/fruit') do |e|
25
- fruits.push(e.to_xml(indent: 0))
26
- end
27
- reader.on('/root/vegetable') do |e|
28
- vegetables.push(e.to_xml(indent: 0))
29
- end
30
- reader.on('/root/meat/type') do |e|
31
- meats.push(e.to_xml(indent: 0))
24
+ reader = subject.new(doc)
25
+ reader.on('/root/fruit') do |e|
26
+ fruits.push(e.to_xml(indent: 0))
27
+ end
28
+ reader.on('/root/vegetable') do |e|
29
+ vegetables.push(e.to_xml(indent: 0))
30
+ end
31
+ reader.on('/root/meat/type') do |e|
32
+ meats.push(e.to_xml(indent: 0))
33
+ end
34
+ reader.run
35
+
36
+ expect(fruits).to eq ['<fruit/>', '<fruit/>']
37
+ expect(vegetables).to eq ["<vegetable id=\"1\">\n<seed/>\n</vegetable>"]
38
+ expect(meats).to eq ["<type>beef</type>"]
32
39
  end
33
- reader.run
34
40
 
35
- expect(fruits).to eq ['<fruit/>', '<fruit/>']
36
- expect(vegetables).to eq ["<vegetable id=\"1\">\n<seed/>\n</vegetable>"]
37
- expect(meats).to eq ["<type>beef</type>"]
38
- end
41
+ it 'preserves CDATA nodes' do
42
+ doc = StringIO.new %{
43
+ <root><![CDATA[banana]]></root>
44
+ }
45
+
46
+ reader = subject.new(doc)
47
+ reader.on('/root') do |e|
48
+ expect(e.children.length).to eq 1
49
+ expect(e.children.first.cdata?).to be true
50
+ end
51
+ reader.run
52
+ end
39
53
 
40
- it 'preserves CDATA nodes' do
41
- doc = %{
42
- <root><![CDATA[banana]]></root>
43
- }
54
+ it 'preserves text nodes' do
55
+ doc = StringIO.new %{
56
+ <root>banana</root>
57
+ }
44
58
 
45
- reader = subject.new(doc)
46
- reader.on('/root') do |e|
47
- expect(e.children.length).to eq 1
48
- expect(e.children.first.cdata?).to be true
59
+ reader = subject.new(doc)
60
+ reader.on('/root') do |e|
61
+ expect(e.children.length).to eq 1
62
+ expect(e.children.first.text?).to be true
63
+ expect(e.children.first.cdata?).to be false
64
+ end
65
+ reader.run
49
66
  end
50
- reader.run
51
67
  end
52
68
 
53
- it 'preserves text nodes' do
54
- doc = %{
55
- <root>banana</root>
56
- }
69
+ describe "#on_start" do
70
+ it 'parses registered paths' do
71
+ doc = StringIO.new %{
72
+ <root>
73
+ <fruit name="apple"/>
74
+ <fruit/>
75
+ <fruit>something</fruit>
76
+ <vegetable id='1'><seed/></vegetable>
77
+ <meat><type>beef</type></meat>
78
+ </root>
79
+ }
80
+ fruits = []
81
+ reader = subject.new(doc)
82
+ reader.on_start('/root/fruit') do |e|
83
+ fruits.push(e.to_xml(indent: 0))
84
+ end
85
+ reader.run
57
86
 
58
- reader = subject.new(doc)
59
- reader.on('/root') do |e|
60
- expect(e.children.length).to eq 1
61
- expect(e.children.first.text?).to be true
62
- expect(e.children.first.cdata?).to be false
87
+ expect(fruits).to eq ['<fruit name="apple"/>', '<fruit/>', '<fruit/>']
63
88
  end
64
- reader.run
65
89
  end
66
90
 
67
- end
91
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nokogiri-streaming-reader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: '1.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Staubo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-10 00:00:00.000000000 Z
11
+ date: 2017-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -103,11 +103,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  version: '0'
104
104
  requirements: []
105
105
  rubyforge_project:
106
- rubygems_version: 2.2.5
106
+ rubygems_version: 2.5.2
107
107
  signing_key:
108
108
  specification_version: 4
109
109
  summary: Simple streaming reader for Nokogiri.
110
110
  test_files:
111
111
  - spec/reader_spec.rb
112
112
  - spec/spec_helper.rb
113
- has_rdoc: