xmlscan 0.3.0preb → 0.3.0prec
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/xmlscan/processor.rb +21 -33
- metadata +11 -11
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.0prec
|
data/lib/xmlscan/processor.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
require 'xmlscan/parser'
|
3
3
|
require 'xmlscan/visitor'
|
4
|
+
require 'stringio'
|
4
5
|
|
5
6
|
module XMLScan
|
6
7
|
module ElementProcessor
|
@@ -14,20 +15,20 @@ module XMLScan
|
|
14
15
|
|
15
16
|
def initialize(opts={}, mod=nil)
|
16
17
|
raise "No module" unless mod
|
17
|
-
STDERR << "init Element Processer #{mod}\n"
|
18
18
|
(MY_METHODS - mod.instance_methods).each do |i|
|
19
|
-
self.class.class_eval %{def #{i}(d, *a) d&&(
|
19
|
+
self.class.class_eval %{def #{i}(d, *a) d&&(self << d) end}, __FILE__, __LINE__
|
20
20
|
end
|
21
21
|
self.class.send :include, mod
|
22
22
|
|
23
23
|
@element = opts[:element] || raise("need an element")
|
24
24
|
@key = opts[:key] || raise("need a key")
|
25
25
|
@extras = (ex = opts[:extras]) ? ex.map(&:to_sym) : []
|
26
|
+
@tmpl = opts[:substitute] || "{{:key}}"
|
26
27
|
|
27
|
-
@pairs =
|
28
|
-
@context = ''
|
29
|
-
@stack = []
|
30
|
-
@out = []
|
28
|
+
@pairs = {} # output name=> [content, context, extra_values] * 1 or more
|
29
|
+
@context = '' # current key(name) of the element (card)
|
30
|
+
@stack = [] # stack of containing context cards
|
31
|
+
@out = [] # current output for name(card)
|
31
32
|
@parser = XMLScan::XMLParser.new(self)
|
32
33
|
self
|
33
34
|
end
|
@@ -41,8 +42,8 @@ module XMLScan
|
|
41
42
|
mod ||= ElementProcessing
|
42
43
|
STDERR << "process #{io.inspect}, #{opts.inspect}\n"
|
43
44
|
io = case io
|
44
|
-
when String; open(io)
|
45
45
|
when IO, StringIO; io
|
46
|
+
when String; open(io)
|
46
47
|
else raise "bad type file input #{io.inspect}"
|
47
48
|
end
|
48
49
|
|
@@ -54,36 +55,31 @@ module XMLScan
|
|
54
55
|
|
55
56
|
|
56
57
|
module ElementProcessing
|
57
|
-
def
|
58
|
+
def <<(s) @out << s end
|
59
|
+
def on_chardata(s) self << s end
|
58
60
|
def on_stag_end(name, s, h, *a)
|
59
61
|
if name.to_sym == @element
|
60
62
|
# starting a new context, first output our substitute string
|
61
63
|
key= h&&h[@key.to_s]||'*no-name*'
|
62
|
-
@tmpl
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
@tmpl.split('|').find {|x| !(/:\w[\w\d]*/ =~ x) ||
|
67
|
-
h[$&[1..-1].to_s] }.gsub(/:\w[\w\d]*/) {|m|
|
68
|
-
STDERR << "templ sub match #{m.inspect}, #{h[m[1..-1]]}\n"
|
69
|
-
h[m[1..-1]] }
|
70
|
-
#sub = h['transclude'] || "{{#{key}}}"
|
71
|
-
@out << sub
|
64
|
+
self << @tmpl.split('|').find {
|
65
|
+
|x| !(/:\w[\w\d]*/ =~ x) || h[$&[1..-1].to_s] }.gsub(/:\w[\w\d]*/) {
|
66
|
+
|m| h[m[1..-1]]
|
67
|
+
}
|
72
68
|
# then push the current context and initialize this one
|
73
69
|
@stack.push([@context, @out, *@ex])
|
74
|
-
@
|
75
|
-
@ex = @extras.map {|e| h[e]}
|
76
|
-
else
|
70
|
+
@pairs[key] = nil # insert it when first seen
|
71
|
+
@context = key; @out = []; @ex = @extras.map {|e| h[e.to_s]}
|
72
|
+
else self << s end # pass through tags we aren't processing
|
77
73
|
end
|
78
74
|
|
79
75
|
def on_etag(name, s=nil)
|
80
76
|
if name.to_sym == @element
|
81
77
|
# output a card (name, content, type)
|
82
|
-
@pairs
|
78
|
+
@pairs[@context] = [@out, @stack[-1][0], *@ex]
|
83
79
|
# restore previous context from stack
|
84
80
|
last = @stack.pop
|
85
81
|
@context, @out, @ex = last.shift, last.shift, *last
|
86
|
-
else
|
82
|
+
else self << s end
|
87
83
|
end
|
88
84
|
|
89
85
|
def on_stag_empty_end(name, s=nil, h={}, *a)
|
@@ -91,19 +87,11 @@ module XMLScan
|
|
91
87
|
|
92
88
|
key= h&&h[@key.to_s]||'*no-name*'
|
93
89
|
ex = @extras.map {|e| h[e]}
|
94
|
-
@pairs
|
95
|
-
else
|
90
|
+
@pairs[key] = [[], @context, *ex]
|
91
|
+
else self << s end
|
96
92
|
end
|
97
93
|
|
98
94
|
attr_reader :pairs, :parser
|
99
95
|
end
|
100
96
|
|
101
97
|
end
|
102
|
-
ARGV.each do |a|
|
103
|
-
pairs = XMLScan::XMLProcessor.process(a, {:key=>:name, :element=>:card, :extras=>[:type]}, XMLScan::ElementProcessing)
|
104
|
-
STDOUT << "Result\n"
|
105
|
-
STDOUT << pairs.map do |p| n,o,c,t = p
|
106
|
-
"#{c&&c.size>0&&"#{c}::"||''}#{n}#{t&&"[#{t}]"}=>#{o*''}"
|
107
|
-
end * "\n"
|
108
|
-
STDOUT << "\nDone\n"
|
109
|
-
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xmlscan
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.0prec
|
5
5
|
prerelease: 5
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &9706320 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 2.8.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *9706320
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rdoc
|
27
|
-
requirement: &
|
27
|
+
requirement: &9705800 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '3.12'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *9705800
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: bundler
|
38
|
-
requirement: &
|
38
|
+
requirement: &9705220 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 1.0.0
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *9705220
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: jeweler
|
49
|
-
requirement: &
|
49
|
+
requirement: &9704580 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: 1.8.3
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *9704580
|
58
58
|
description: The fastest XML parser written in 100% pure Ruby.
|
59
59
|
email: gerryg@inbox.com
|
60
60
|
executables: []
|
@@ -96,7 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
96
96
|
version: '0'
|
97
97
|
segments:
|
98
98
|
- 0
|
99
|
-
hash:
|
99
|
+
hash: 4206592949743860129
|
100
100
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
101
|
none: false
|
102
102
|
requirements:
|