line-tree 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/line-tree.rb +54 -0
- metadata +55 -0
data/lib/line-tree.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
class LineTree
|
2
|
+
|
3
|
+
def initialize(lines)
|
4
|
+
a = lines.split(/\r?\n|\r(?!\n)/)
|
5
|
+
pattern = %r((\s+)?(((\/|.)[^\s]+)\s)?([^$]+))
|
6
|
+
a.map!{|x| x.match(pattern).captures.values_at(0,2,4)}
|
7
|
+
|
8
|
+
new_a, history = [], []
|
9
|
+
history << new_a
|
10
|
+
build_tree(a, new_a, 0, history)
|
11
|
+
@a = new_a
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_a()
|
15
|
+
@a
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def build_tree(a, new_a, prev_indent, history=[])
|
21
|
+
if a.length > 0 then
|
22
|
+
x = a.shift
|
23
|
+
n = x.shift
|
24
|
+
cur_indent = n ? n.length : 0
|
25
|
+
indent, xr = build_branch(a, new_a, cur_indent, prev_indent, x, history )
|
26
|
+
|
27
|
+
if xr then
|
28
|
+
history.pop
|
29
|
+
cur_indent, xr = build_branch(a, history[-1] || new_a, indent, prev_indent, xr, history )
|
30
|
+
return [cur_indent, xr] if xr
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def build_branch(a, new_a, cur_indent, prev_indent, x, history=[])
|
36
|
+
if cur_indent > prev_indent then
|
37
|
+
new_a = history[-1]
|
38
|
+
new_inner_a = [x]
|
39
|
+
new_a << new_inner_a
|
40
|
+
history << new_inner_a
|
41
|
+
build_tree(a, history[-2], cur_indent, history)
|
42
|
+
elsif cur_indent == prev_indent then
|
43
|
+
history.pop
|
44
|
+
new_a = history[-1] if history[-1]
|
45
|
+
new_inner_a = [x]
|
46
|
+
new_a << new_inner_a
|
47
|
+
history << new_inner_a
|
48
|
+
build_tree(a, new_inner_a, cur_indent, history)
|
49
|
+
else
|
50
|
+
# revert to the earlier new_a
|
51
|
+
return [cur_indent, x]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: line-tree
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors: []
|
7
|
+
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-04-04 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Line-tree parses indented lines of text and returns an array representing a tree structure.
|
17
|
+
email:
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/line-tree.rb
|
26
|
+
has_rdoc: true
|
27
|
+
homepage:
|
28
|
+
licenses: []
|
29
|
+
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: "0"
|
40
|
+
version:
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.3.5
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: line-tree
|
54
|
+
test_files: []
|
55
|
+
|