Permalink
Browse files

Added DOTScriptData, moved responsibilities from lib/graphviz

  • Loading branch information...
1 parent 7504cb2 commit d378784fc32067b73b29a58a3400daf7db30fecc @markus1189 markus1189 committed Jul 7, 2012
Showing with 74 additions and 52 deletions.
  1. +33 −51 lib/graphviz.rb
  2. +41 −1 lib/graphviz/dot_script.rb
View
@@ -425,61 +425,14 @@ def each_attribut(&b)
#
def output( hOpts = {} )
xDOTScript = DOTScript.new
- xLastType = nil
- xSeparator = ""
- xData = ""
lNotHugly = []
- @elements_order.each { |kElement|
- unless xLastType and xLastType == kElement["type"]
- xDOTScript.add_type(xLastType, xData) unless xData.empty?
-
- xSeparator = ""
- xData = ""
- end
-
- xLastType = kElement["type"]
-
- #Modified by
- #Brandon Coleman
- #verify value is NOT NULL
- if kElement["value"] == nil then
- raise ArgumentError, "#{kElement["name"]} has a nil value!"
- end
-
- case kElement["type"]
- when "graph_attr"
- xData << xSeparator + kElement["name"] + " = " + kElement["value"].to_gv
- xSeparator = "; "
-
- when "node_attr"
- xData << xSeparator + kElement["name"] + " = " + kElement["value"].to_gv
- xSeparator = ", "
-
- when "edge_attr"
- xData << xSeparator + kElement["name"] + " = " + kElement["value"].to_gv
- xSeparator = ", "
-
- when "node"
- xDOTScript << " " + kElement["value"].output()
-
- when "edge"
- xDOTScript << " " + kElement["value"].output( @oGraphType )
-
- when "graph"
- xDOTScript << kElement["value"].output()
-
- else
- raise ArgumentError, "Don't know what to do with element type '#{kElement['type']}'"
- end
- }
-
- xDOTScript.add_type(xLastType, xData) unless xData.empty?
+ append_attributes_and_types(xDOTScript)
xDOTScript << "}"
- if @oParentGraph
- xDOTScript.prepend("subgraph #{GraphViz.escape(@name, :unquote_empty => true)} {")
+ if has_parent_graph?
+ xDOTScript.make_subgraph("#{GraphViz.escape(@name, :unquote_empty => true)}")
else
hOutput = {}
@@ -650,9 +603,34 @@ def output( hOpts = {} )
end
end
end
-
alias :save :output
+ def append_attributes_and_types(script)
+ xLastType = nil
+ xData = DOTScriptData.new
+
+ @elements_order.each { |kElement|
+ is_new_type = xLastType != kElement["type"]
+ xData = DOTScriptData.new if is_new_type
+ xLastType = kElement["type"]
+
+ # Modified by Brandon Coleman verify value is NOT NULL
+ kElement["value"] or raise ArgumentError, "#{kElement["name"]} is nil!"
+
+ case xLastType
+ when "graph_attr", "node_attr", "edge_attr"
+ xData.add_attribute(xLastType,kElement["name"],kElement["value"].to_gv)
+ when "node", "graph"
+ script << kElement["value"].output()
+ when "edge"
+ script << " " + kElement["value"].output( @oGraphType )
+ else
+ raise ArgumentError,
+ "Don't know what to do with element type '#{kElement['type']}'"
+ end
+ }
+ end
+
def to_s
self.output(:none => String)
end
@@ -788,6 +766,10 @@ def complete!
def directed?
not (/digraph/ =~ "bla digraph bla").nil?
end
+
+ def has_parent_graph?
+ @oParentGraph
+ end
## ----------------------------------------------------------------------------
private
View
@@ -1,5 +1,41 @@
require "forwardable"
class GraphViz
+
+ class DOTScriptData
+ def initialize
+ @data = []
+ end
+
+ def append(data)
+ @data << data
+ end
+ alias :<< :append
+
+ def add_attribute(type,name,value)
+ @data << @separator << name << " = " << value
+ @separator = determine_separator(type)
+ end
+
+ def to_str
+ @data.join(" ")
+ end
+
+ def empty?
+ @data.empty?
+ end
+
+ private
+
+ def determine_separator(str)
+ case str
+ when "graph_attr" then ";"
+ when "node_attr", "edge_attr" then ","
+ else raise ArgumentError, "Unknown type: #{str}."
+ end
+ end
+
+ end
+
class DOTScript
extend Forwardable
@@ -22,6 +58,10 @@ def prepend(line)
self
end
+ def make_subgraph(name)
+ prepend(assure_ends_with("subgraph #{name}"," {"))
+ end
+
def add_type(type, data)
return self if data.empty?
@@ -49,7 +89,7 @@ def to_str
private
def assure_ends_with(str,ending="\n")
- str.end_with?("\n") ? str : str + "\n"
+ str.to_s.end_with?("\n") ? str : str + ending
end
def append_statement(statement)

1 comment on commit d378784

qqshfox commented on d378784 Sep 5, 2012

All common graph/node options are lost after this commit.

Such as:

g["rankdir"] = "LR"
g.node["shape"] = "box"
Please sign in to comment.