Top Level Namespace

Includes:
REXML

Defined Under Namespace

Classes: Link, Matrix, Node, RowOfNodes, Tag, Text, Tree

Constant Summary collapse

DummyRoot =
Text.new(0, "DummyRoot")

Instance Method Summary collapse

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


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(header)
  depth = header[0].to_i
  s = read_anchor(header[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
    # NOTE: Design Issue
    # If REQUIDEF_PARSE_LENIENT flag is on,
    # This software understands every text nodes as link nodes whose dest is text.
    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
    # NOTE: The separator of csv file is decisively |
    # I/F to change the charactor is the future work.
    separator = "|"
    s += ms[ms_range].join separator
    s += "\n" unless i == matrix.m_size - 1
  end
  s
end

#mm2rd(txt) ⇒ Object



29
30
31
# File 'lib/requidef.rb', line 29

def mm2rd(txt)
  rdtree2rd( mm2rdtree(txt))
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

Returns:

  • (Boolean)


33
34
35
# File 'lib/rd2rdnodes.rb', line 33

def parse_lenient?
  ENV["REQUIDEF_PARSE_LENIENT"] == "true"
end

NOTE: Under Engineering



66
67
68
69
# File 'lib/rd2rdnodes.rb', line 66

def parse_link(s)
  # Fix: Delete only the first >> 
  s.delete ">>" 
end

#parse_tag(s) ⇒ Object

NOTE: Under Engineering



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("]]")
  # Fix: What happen if having two colons
  # In current version, if haveing two colons will cause unexpected output or runtime error.
  xs = ss.split(":")
  case xs.size
  when 1
    return [xs[0], xs[0]]
  when 2
    return xs
  end
  # In current  version, if more than two colons will raise exception.
  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

rd -> [node]



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

#rd2rdtree(rd) ⇒ Object



4
5
6
# File 'lib/rd2rdtree.rb', line 4

def rd2rdtree( rd)
  rdnodes2rdtree( rd2rdnodes(rd) ) 
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



3
4
5
# File 'lib/generic/row_of_nodes.rb', line 3

def row_of_nodes(tree)
  RowOfNodes.new(tree).row_of_nodes
end

#to_csv(txt) ⇒ Object

File -> String



14
15
16
# File 'lib/requidef.rb', line 14

def to_csv(txt)
  rd2rdtree( txt ).to_csv
end

#to_dot(txt) ⇒ Object

File -> String



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