line-tree 0.2.7 → 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/line-tree.rb +15 -68
  2. metadata +2 -2
data/lib/line-tree.rb CHANGED
@@ -1,84 +1,31 @@
1
- require 'rexml/document'
1
+ #!/usr/bin/ruby
2
2
 
3
- class LineTree
4
- include REXML
3
+ # file: line-tree.rb
5
4
 
6
- def initialize(lines)
7
- @lines = lines
8
- a = lines.split(/\r?\n|\r(?!\n)/)
9
- pattern = %r((\s+)?(.*))
10
- a.map!{|x| x.match(pattern).captures}
5
+ class LineTree
11
6
 
12
- new_a, history = [], []
13
- history << new_a
14
- build_tree(a, new_a, 0, history)
15
- @a = new_a
7
+ def initialize(lines)
8
+ @a = scan_shift(lines)
16
9
  end
17
10
 
18
11
  def to_a()
19
12
  @a
20
13
  end
21
14
 
22
- def to_xml()
23
-
24
- a = @lines.split(/\r?\n|\r(?!\n)/)
25
- pattern = %r((\s+)?(((\/|.)[^\s]+)\s)?([^$]+))
26
- a.map!{|x| x.match(pattern).captures.values_at(0,2,4)}
27
-
28
- new_a, history = [], []
29
- history << new_a
30
- build_tree(a, new_a, 0, history)
31
-
32
- doc = Document.new '<root/>'
33
- scan_line doc.root, new_a
34
- doc.to_s
35
- end
36
-
37
15
  private
38
16
 
39
- def build_tree(a, new_a, prev_indent, history=[])
40
- if a.length > 0 then
41
- x = a.shift
42
- n = x.shift
43
- cur_indent = n ? n.length : 0
44
- indent, xr = build_branch(a, new_a, cur_indent, prev_indent, x, history )
45
-
46
- if xr then
47
- cur_indent, xr = build_branch(a, history[-1] || new_a, indent, prev_indent, xr, history )
48
- return [cur_indent, xr] if xr
17
+ def scan_shift(lines)
18
+ a = lines.split(/^\b/)
19
+ a.map do |x|
20
+ rlines = x.split(/\n/)
21
+ label = [rlines.shift]
22
+ new_lines = rlines.map{|x| x[2..-1]}
23
+ if new_lines.length > 1 then
24
+ label + scan_shift(new_lines.join("\n"))
25
+ else
26
+ new_lines.length > 0 ? label + [new_lines] : label
49
27
  end
50
28
  end
51
29
  end
52
30
 
53
- def build_branch(a, new_a, cur_indent, prev_indent, x, history=[])
54
- if cur_indent > prev_indent then
55
- new_a = history[-1]
56
- new_inner_a = [x]
57
- new_a << new_inner_a
58
- history << new_inner_a
59
- build_tree(a, history[-2], cur_indent, history)
60
- elsif cur_indent == prev_indent then
61
- history.pop unless history.length <= 1
62
- new_a = history[-1] if history[-1]
63
- new_inner_a = [x]
64
- new_a << new_inner_a
65
- history << new_inner_a
66
- build_tree(a, new_inner_a, cur_indent, history)
67
- else
68
- # revert to the earlier new_a
69
- history.pop unless history.length <= 1
70
- return [cur_indent, x]
71
- end
72
- end
73
-
74
- def scan_line(node, a)
75
- a.each do |x|
76
- name, text = x.shift.compact
77
- element = Element.new(name.gsub(/^[^a-zA-Z][^a-zA-Z0-9]?/,''))
78
- element.add_text text
79
- scan_line(element, x) unless x.empty?
80
- node.add element
81
- end
82
- end
83
-
84
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: line-tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
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-24 00:00:00 +01:00
12
+ date: 2010-06-06 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15