class Object

Constants

DummyRoot

Public Instance Methods

build_tree(mm) click to toggle source

Doc -> Tree (of Text node class)

# 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) click to toggle source
# File lib/rdnode.rb, line 115
def dashes(n)
  Array.new(n, "-").join ""
end
do_build_tree(doc, id, depth, t) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

Example, — aaa => Text(3, “aaa”) – >>bbb => Link(2, “bbb”)

  • [ccc:ddd]

    > Tag(1, “ccc”, “ddd”)

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/requidef.rb, line 29
def mm2rd(txt)
  rdtree2rd( mm2rdtree(txt))
end
mm2rdtree(mm) click to toggle source
# File lib/mm2rdtree.rb, line 5
def mm2rdtree(mm)
  build_tree(mm)
end
mm2tree(mm) click to toggle source
# File lib/mm2rdtree.rb, line 9
def mm2tree(mm)
  mm2rdtree(mm)
end
nodes2tree(nodes) click to toggle source
# 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?() click to toggle source
# File lib/rd2rdnodes.rb, line 33
def parse_lenient?
  ENV["REQUIDEF_PARSE_LENIENT"] == "true"
end
parse_tag(s) click to toggle source

NOTE: Under Engineering

# 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) click to toggle source
# File lib/rd2rdnodes.rb, line 87
def parse_text(s)
  s
end
rd2lines(rd) click to toggle source
# 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) click to toggle source

rd -> [node]

# File lib/rd2rdnodes.rb, line 21
def rd2nodes(rd)
  rd2rdnodes(rd)
end
rd2rdnodes( rd) click to toggle source
# File lib/rd2rdnodes.rb, line 14
def rd2rdnodes( rd)
  lines = rd2lines(rd)
  nodes = lines2nodes(lines)
  nodes 
end
rd2rdtree( rd) click to toggle source
# File lib/rd2rdtree.rb, line 4
def rd2rdtree( rd)
  rdnodes2rdtree( rd2rdnodes(rd) ) 
end
rdnodes2rdtree(nodes) click to toggle source
# File lib/rdnodes2rdtree.rb, line 4
def rdnodes2rdtree(nodes)
  nodes2tree(nodes)
end
rdtree2rd(tree) click to toggle source
# File lib/rdtree2rd.rb, line 3
def rdtree2rd(tree)
  tree.to_rd
end
read_anchor(anchor) click to toggle source
# File lib/html2rd.rb, line 22
def read_anchor(anchor)
  anchor.scan(/<a.*?>(.*?)<\/a>/m)[0][0]
end
read_body(html) click to toggle source
# 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) click to toggle source
# File lib/html2rd.rb, line 26
def repeat(n, s)
  Array.new(n, s).join
end
root_node_of(mm) click to toggle source
# 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) click to toggle source
# File lib/generic/row_of_nodes.rb, line 3
def row_of_nodes(tree)
  RowOfNodes.new(tree).row_of_nodes
end
to_csv(txt) click to toggle source

File -> String

# File lib/requidef.rb, line 14
def to_csv(txt)
  rd2rdtree( txt ).to_csv
end
to_dot(txt) click to toggle source

File -> String

# File lib/requidef.rb, line 9
def to_dot(txt)
  rd2rdtree( txt ).to_dot
end
to_node(doc, depth) click to toggle source
# File lib/mm2rdtree.rb, line 38
def to_node(doc, depth)
  text = doc.attributes["TEXT"]
  Text.new(depth, text)
end
to_rd(txt, ext) click to toggle source
# 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) click to toggle source
# 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