polyrex 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/polyrex.rb +29 -81
  2. metadata +12 -2
data/lib/polyrex.rb CHANGED
@@ -1,9 +1,10 @@
1
- #!/usr/bin/ruby
1
+ #/usr/bin/ruby
2
2
 
3
3
  # file: polyrex.rb
4
4
 
5
5
  require 'polyrex-schema'
6
6
  require 'backtrack-xpath'
7
+ require 'line-tree'
7
8
  require 'rexml/document'
8
9
 
9
10
  class Polyrex
@@ -26,7 +27,7 @@ class Polyrex
26
27
 
27
28
  @schema = schema
28
29
 
29
- @id = '1'
30
+ @id = '0'
30
31
  a = @schema.split('/')
31
32
 
32
33
  @rpaths = (a.length).times.inject({}) {|r| r.merge ({a.join('/').gsub(/\[[^\]]+\]/,'') => a.pop}) }
@@ -112,92 +113,40 @@ end
112
113
  end
113
114
 
114
115
  def parse(lines)
115
- a = lines.split("\n")
116
-
117
- a.map! do |x|
118
- x.match(/(\s+)?(.*)/).captures
119
- end
120
-
121
- new_a = []
122
- history = []
123
- history << new_a
124
-
125
- build_tree(a, new_a, 0, history)
126
- out = scan_link(new_a)
127
-
128
- records = Document.new("<records>%s</records>" % out)
129
- doc_records = XPath.first(@doc.root, 'records')
130
-
131
- records.each do |record|
132
- doc_records.add record
133
- end
134
-
135
- end
136
-
137
- def build_tree(a, new_a, prev_indent, history=[])
138
- if a.length > 0 then
139
- x = a.shift
140
- n = x.shift
141
- cur_indent = n ? n.length : 0
142
- indent, xr = build_branch(a, new_a, cur_indent, prev_indent, x, history )
143
-
144
- if xr then
145
- history.pop
146
- cur_indent, xr = build_branch(a, history[-1] || new_a, indent, prev_indent, xr, history )
147
- return [cur_indent, xr] if xr
148
- end
149
- end
150
- end
151
-
152
- def build_branch(a, new_a, cur_indent, prev_indent, x, history=[])
153
- if cur_indent > prev_indent then
154
- new_a = history[-1]
155
- new_inner_a = [x]
156
- new_a << new_inner_a
157
- history << new_inner_a
158
- build_tree(a, history[-2], cur_indent, history)
159
- elsif cur_indent == prev_indent then
160
- new_inner_a = [x]
161
- new_a << new_inner_a
162
- history << new_inner_a
163
- build_tree(a, new_inner_a, cur_indent, history)
164
- else
165
- # revert to the earlier new_a
166
- return [cur_indent, x]
167
- end
116
+ format_line!(@parent_node, LineTree.new(lines).to_a)
117
+ self
168
118
  end
169
119
 
170
- def scan_link(a,indent='')
171
- out = []
172
- i2 = indent.clone
173
- a.each do |x|
174
- i = indent.length / 2
175
- tag_name = @recordx[i].to_s
176
-
177
- line= x[0].reverse.map(&:strip).join
178
-
120
+ def format_line!(records, a, i=0)
121
+
122
+ a.each do |x|
123
+
124
+ tag_name = @recordx[i].to_s
125
+ line = x.shift.join
126
+
179
127
  @field_names = @format_masks[i].to_s.scan(/\[!(\w+)\]/).flatten.map(&:to_sym)
180
-
181
128
  t = @format_masks[i].to_s.gsub(/\[!(\w+)\]/, '(.*)').sub(/\[/,'\[').sub(/\]/,'\]')
182
- field_values = line.match(/#{t}/).captures
129
+ field_values = line.match(/#{t}/).captures
130
+
131
+ @id = (@id.to_i + 1).to_s
132
+ record = Element.new(tag_name)
133
+ record.add_attribute('id', @id)
134
+ summary = Element.new('summary')
183
135
 
184
- fields = @field_names.zip(field_values).map{|name, value| "<%s>%s</%s>" % [name,value,name]}.join
136
+ @field_names.zip(field_values).each do |name, value|
137
+ field = Element.new(name.to_s)
138
+ field.text = value
139
+ summary.add field
140
+ end
185
141
 
186
- @id = (@id.to_i + 1).to_s
187
- out << indent + "<#{tag_name} id='#{@id}'><summary>#{fields}</summary>"
188
- if x.length > 1 and x[0].is_a? Array then
189
- #i2 += ' '
190
- out << indent + "<records>\n"
191
- x.shift
192
- out << scan_link(x,i2 + ' ')
193
- out << indent + "</records>\n"
194
- else
195
- out << indent + "<records/>\n"
142
+ new_records = Element.new('records')
196
143
 
197
- end
198
- out << indent + "</#{tag_name}>\n"
144
+ record.add summary
145
+ record.add new_records
146
+ records.add record
147
+
148
+ format_line!(new_records, x, i+1) unless x.empty?
199
149
  end
200
- out.join
201
150
  end
202
151
 
203
152
  # -- end of full text edit methods
@@ -207,4 +156,3 @@ end
207
156
  end
208
157
 
209
158
  end
210
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polyrex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors: []
7
7
 
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-05-22 00:00:00 +01:00
12
+ date: 2010-05-23 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -32,6 +32,16 @@ dependencies:
32
32
  - !ruby/object:Gem::Version
33
33
  version: "0"
34
34
  version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: line-tree
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
35
45
  description:
36
46
  email:
37
47
  executables: []