module Iroki::Utils

Public Instance Methods

add_color_to_leaf_branch(patterns, node, exact, iroki_to_name=nil) click to toggle source
# File lib/iroki/utils/utils.rb, line 25
def add_color_to_leaf_branch patterns, node, exact, iroki_to_name=nil
  num_matches = 0
  color = nil
  already_matched = false

  if exact # treat patterns as string matching
    node_s = node.to_s
    if patterns.has_key? node_s
      color = patterns[node_s]

      return color
    elsif Iroki::Color::default_color_tag
      color = Iroki::Color::default_color_tag

      return color
    else
      return nil
    end
  else
    assert iroki_to_name, "iroki_to_name arg is nil"
    assert iroki_to_name[node.to_s],
           "iroki_to_name is missing #{node.to_s}"
    node_s = iroki_to_name[node.to_s]

    patterns.each do |pattern, this_color|
      if node_s =~ pattern
        abort_if already_matched,
                 "Non specific matching for #{node_s}. " +
                 "Previously matched pattern was " +
                 "#{already_matched.inspect}. Current pattern " +
                 "is " +
                 "#{pattern.inspect}. Color was #{this_color}."

        color = this_color
        already_matched = pattern
      end
    end

    # if there was no match and there is a default color
    if !color && Iroki::Color::default_color_tag
      color = Iroki::Color::default_color_tag
    end

    return color
  end
end
color_nodes(patterns, tree, node, exact, iroki_to_name) click to toggle source
# File lib/iroki/utils/utils.rb, line 80
def color_nodes patterns, tree, node, exact, iroki_to_name
  # # check if it needs color, if so set the color
  # color = add_color_to_leaf_branch patterns, node, exact

  # clean the name no matter what
  if node.name
    node.name = node.name #.clean_name
  else
    node.name = nil
  end

  # if its a leaf that hasnt been checked & needs color
  # TODO the node.name is there to make sure already_checked?
  # doesn't blow up. When can node.name be nil?
  if(leaf?(tree, node) &&
     node.name &&
     !node.name.already_checked?)
    # check if it needs color, if so set the color

    # NOTE: this was originally before cleaning the node name a
    # couple lines up, does it matter that it is after?
    color = add_color_to_leaf_branch patterns,
                                     node,
                                     exact,
                                     iroki_to_name

    # add color to the name
    node.name = node.name + color[:branch] if color
  elsif !leaf?(tree, node)
    children = tree.children(node) # get the children
    children_colors = []
    children.each do |child|
      # recurse to color the child if needed
      color_nodes patterns, tree, child, exact, iroki_to_name
      children_colors << get_color(child) # add color of the child
    end

    # if all the children have the same color
    if children_colors.uniq.count == 1
      # set the branch node to only the color name
      node.name = children_colors[0]
    end
  end

  return node
end
get_color(node) click to toggle source
# File lib/iroki/utils/utils.rb, line 72
def get_color node
  begin
    node.name.match(/\[&!color="#[0-9A-Fa-f]{6}"\]/)[0]
  rescue NoMethodError => e
    nil
  end
end
leaf?(tree, node) click to toggle source
# File lib/iroki/utils/utils.rb, line 21
def leaf? tree, node
  tree.children(node).empty?
end