Top Level Namespace
Defined Under Namespace
Classes: Link, Matrix, Node, RowOfNodes, Tag, Text, Tree
Constant Summary
collapse
- DummyRoot =
Text.new(0, "DummyRoot")
Instance Method Summary
collapse
-
#build_tree(mm) ⇒ Object
Doc -> Tree (of Text node class).
-
#dashes(n) ⇒ Object
-
#depth2link(nodes) ⇒ Object
-
#do_build_tree(doc, id, depth, t) ⇒ Object
-
#html2rd(html) ⇒ Object
-
#hx2rd(header) ⇒ Object
-
#line2node(line) ⇒ Object
Example, — aaa => Text(3, “aaa”) – >>bbb => Link(2, “bbb”) - [[ccc:ddd]] => Tag(1, “ccc”, “ddd”).
-
#lines2nodes(lines) ⇒ Object
-
#matrix2csv(matrix, range = [0...matrix.m_size, 0...matrix.n_size], &writer) ⇒ Object
-
#mm2rd(txt) ⇒ Object
-
#mm2rdtree(mm) ⇒ Object
-
#mm2tree(mm) ⇒ Object
-
#nodes2tree(nodes) ⇒ Object
-
#parse_lenient? ⇒ Boolean
-
#parse_link(s) ⇒ Object
-
#parse_tag(s) ⇒ Object
-
#parse_text(s) ⇒ Object
-
#rd2lines(rd) ⇒ Object
-
#rd2nodes(rd) ⇒ Object
-
#rd2rdnodes(rd) ⇒ Object
-
#rd2rdtree(rd) ⇒ Object
-
#rdnodes2rdtree(nodes) ⇒ Object
-
#rdtree2rd(tree) ⇒ Object
-
#read_anchor(anchor) ⇒ Object
-
#read_body(html) ⇒ Object
-
#repeat(n, s) ⇒ Object
-
#root_node_of(mm) ⇒ Object
-
#row_of_nodes(tree) ⇒ Object
-
#to_csv(txt) ⇒ Object
-
#to_dot(txt) ⇒ Object
-
#to_node(doc, depth) ⇒ Object
-
#to_rd(txt, ext) ⇒ Object
-
#tree2matrix(tree) ⇒ Object
Instance Method Details
#build_tree(mm) ⇒ Object
Doc -> Tree (of Text node class)
14
15
16
17
18
19
20
21
|
# File 'lib/mm2rdtree.rb', line 14
def build_tree(mm)
t = Tree.new
doc_root_id = 1
t.add_node(0, DummyRoot)
t.add_edge(0, doc_root_id)
do_build_tree(root_node_of(mm), doc_root_id, 1, t)
t
end
|
#dashes(n) ⇒ Object
115
116
117
|
# File 'lib/rdnode.rb', line 115
def dashes(n)
Array.new(n, "-").join ""
end
|
#depth2link(nodes) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/rdnodes2rdtree.rb', line 23
def depth2link(nodes)
nodess = [DummyRoot] + nodes
nodess.each_with_index do |n, i|
n.id = i
end
current = [nodess[0]]
for i in 1...nodess.size
n = nodess[i]
x = n.depth - 1
n.tmp[:parent_id] = current[x].id
current[n.depth] = n
end
nodess
end
|
#do_build_tree(doc, id, depth, t) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/mm2rdtree.rb', line 23
def do_build_tree(doc, id, depth, t)
t.add_node( id, to_node(doc, depth) )
n = doc.elements.size
for i in 1...n+1
child_id = id+i
child_depth = depth + 1
child_doc = doc.elements[i]
child_value = to_node( child_doc, child_depth )
t.add_node( child_id, child_value )
t.add_edge( id, child_id )
do_build_tree( child_doc, child_id, child_depth, t )
end
end
|
#html2rd(html) ⇒ Object
8
9
10
11
12
13
14
|
# File 'lib/html2rd.rb', line 8
def html2rd(html)
body = read_body(html)
regex = /<h(\d).*?>(.*?)<\/h\d>/m
body.scan(regex).map do |e|
hx2rd(e)
end.join("\n")
end
|
#hx2rd(header) ⇒ Object
16
17
18
19
20
|
# File 'lib/html2rd.rb', line 16
def hx2rd()
depth = [0].to_i
s = read_anchor([1].strip)
"#{repeat(depth, "-")} #{s}"
end
|
#line2node(line) ⇒ Object
Example, — aaa => Text(3, “aaa”) – >>bbb => Link(2, “bbb”)
- [ccc:ddd]
-
> Tag(1, “ccc”, “ddd”)
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/rd2rdnodes.rb', line 41
def line2node(line)
splits = line.split(" ")
dashes = splits[0]
depth = dashes.split("").size
rest = splits[1...splits.size].join " "
if rest.start_with?(">>")
ss = parse_link(rest)
return Link.new(depth, ss)
elsif rest.start_with?("[[")
ss = parse_tag(rest)
return Tag.new(depth, ss[0], ss[1])
else
if parse_lenient?
ss = parse_text(rest)
return Link.new(depth, ss)
end
ss = parse_text(rest)
return Text.new(depth, ss)
end
end
|
#lines2nodes(lines) ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/rd2rdnodes.rb', line 25
def lines2nodes(lines)
xs = []
lines.each do |line|
xs << line2node(line)
end
xs
end
|
#matrix2csv(matrix, range = [0...matrix.m_size, 0...matrix.n_size], &writer) ⇒ Object
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/generic/matrix2csv.rb', line 3
def matrix2csv(matrix, range = [0...matrix.m_size, 0...matrix.n_size], &writer)
s = ""
for i in range[0]
ms = []
ms_range = range[1]
for j in ms_range
ms[j] = writer.call matrix.get([i,j])
end
separator = "|"
s += ms[ms_range].join separator
s += "\n" unless i == matrix.m_size - 1
end
s
end
|
#mm2rdtree(mm) ⇒ Object
5
6
7
|
# File 'lib/mm2rdtree.rb', line 5
def mm2rdtree(mm)
build_tree(mm)
end
|
#mm2tree(mm) ⇒ Object
9
10
11
|
# File 'lib/mm2rdtree.rb', line 9
def mm2tree(mm)
mm2rdtree(mm)
end
|
#nodes2tree(nodes) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/rdnodes2rdtree.rb', line 8
def nodes2tree(nodes)
nodess = depth2link(nodes)
t = Tree.new
nodess.each do |n|
n.add_on_tree(n.id, t)
end
for i in 1...nodess.size
n = nodess[i]
from = n.tmp[:parent_id]
to = n.id
t.add_edge(from, to)
end
t
end
|
#parse_lenient? ⇒ Boolean
33
34
35
|
# File 'lib/rd2rdnodes.rb', line 33
def parse_lenient?
ENV["REQUIDEF_PARSE_LENIENT"] == "true"
end
|
#parse_link(s) ⇒ Object
66
67
68
69
|
# File 'lib/rd2rdnodes.rb', line 66
def parse_link(s)
s.delete ">>"
end
|
#parse_tag(s) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/rd2rdnodes.rb', line 72
def parse_tag(s)
ss = s.delete("[[").delete("]]")
xs = ss.split(":")
case xs.size
when 1
return [xs[0], xs[0]]
when 2
return xs
end
raise "Parse Erorr. The line #{s} contains more than two colons"
end
|
#parse_text(s) ⇒ Object
87
88
89
|
# File 'lib/rd2rdnodes.rb', line 87
def parse_text(s)
s
end
|
#rd2lines(rd) ⇒ Object
3
4
5
6
7
8
9
10
11
12
|
# File 'lib/rd2rdnodes.rb', line 3
def rd2lines(rd)
all_lines = rd.split("\n")
all_lines
.delete_if do |line|
line.empty?
end
.delete_if do |line|
line.start_with? "//"
end
end
|
#rd2nodes(rd) ⇒ Object
21
22
23
|
# File 'lib/rd2rdnodes.rb', line 21
def rd2nodes(rd)
rd2rdnodes(rd)
end
|
#rd2rdnodes(rd) ⇒ Object
14
15
16
17
18
|
# File 'lib/rd2rdnodes.rb', line 14
def rd2rdnodes( rd)
lines = rd2lines(rd)
nodes = lines2nodes(lines)
nodes
end
|
#rdnodes2rdtree(nodes) ⇒ Object
4
5
6
|
# File 'lib/rdnodes2rdtree.rb', line 4
def rdnodes2rdtree(nodes)
nodes2tree(nodes)
end
|
#rdtree2rd(tree) ⇒ Object
3
4
5
|
# File 'lib/rdtree2rd.rb', line 3
def rdtree2rd(tree)
tree.to_rd
end
|
#read_anchor(anchor) ⇒ Object
22
23
24
|
# File 'lib/html2rd.rb', line 22
def read_anchor(anchor)
anchor.scan(/<a.*?>(.*?)<\/a>/m)[0][0]
end
|
#read_body(html) ⇒ Object
30
31
32
33
34
35
36
37
|
# File 'lib/html2rd.rb', line 30
def read_body(html)
doc = Hpricot( html )
x = ""
(doc/"body").each do |e|
x += e.to_html
end
x
end
|
#repeat(n, s) ⇒ Object
26
27
28
|
# File 'lib/html2rd.rb', line 26
def repeat(n, s)
Array.new(n, s).join
end
|
#root_node_of(mm) ⇒ Object
43
44
45
46
|
# File 'lib/mm2rdtree.rb', line 43
def root_node_of(mm)
doc = REXML::Document.new( mm )
doc.root.elements[1]
end
|
#row_of_nodes(tree) ⇒ Object
#to_csv(txt) ⇒ Object
14
15
16
|
# File 'lib/requidef.rb', line 14
def to_csv(txt)
rd2rdtree( txt ).to_csv
end
|
#to_dot(txt) ⇒ Object
9
10
11
|
# File 'lib/requidef.rb', line 9
def to_dot(txt)
rd2rdtree( txt ).to_dot
end
|
#to_node(doc, depth) ⇒ Object
38
39
40
41
|
# File 'lib/mm2rdtree.rb', line 38
def to_node(doc, depth)
text = doc.attributes["TEXT"]
Text.new(depth, text)
end
|
#to_rd(txt, ext) ⇒ Object
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/requidef.rb', line 18
def to_rd(txt, ext)
case ext
when "mm"
return mm2rd(txt)
when "html"
return html2rd(txt)
else
raise "the input stream assumed type can not translate into rd."
end
end
|
#tree2matrix(tree) ⇒ Object
4
5
6
7
8
9
10
11
|
# File 'lib/generic/tree2matrix.rb', line 4
def tree2matrix(tree)
rows = row_of_nodes(tree)
m = Matrix.new
for i in 0...tree.size
m.set( [rows[i], tree.depth(i)], tree.value(i) )
end
m
end
|