summaryrefslogtreecommitdiff
path: root/src/GF/Visualization/Graphviz.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/GF/Visualization/Graphviz.hs')
-rw-r--r--src/GF/Visualization/Graphviz.hs68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/GF/Visualization/Graphviz.hs b/src/GF/Visualization/Graphviz.hs
new file mode 100644
index 000000000..fe2dd0b82
--- /dev/null
+++ b/src/GF/Visualization/Graphviz.hs
@@ -0,0 +1,68 @@
+----------------------------------------------------------------------
+-- |
+-- Module : Graphviz
+-- Maintainer : BB
+-- Stability : (stable)
+-- Portability : (portable)
+--
+-- > CVS $Date: 2005/09/14 15:17:30 $
+-- > CVS $Author: bringert $
+-- > CVS $Revision: 1.1 $
+--
+-- Graphviz DOT format representation and printing.
+-----------------------------------------------------------------------------
+
+module GF.Visualization.Graphviz (
+ Graph(..), GraphType(..),
+ Node(..), Edge(..),
+ Attr,
+ prGraphviz
+ ) where
+
+import GF.Data.Utilities
+
+data Graph = Graph GraphType [Attr] [Node] [Edge]
+ deriving (Show)
+
+data GraphType = Directed | Undirected
+ deriving (Show)
+
+data Node = Node String [Attr]
+ deriving Show
+
+data Edge = Edge String String [Attr]
+ deriving Show
+
+type Attr = (String,String)
+
+prGraphviz :: Graph -> String
+prGraphviz (Graph t at ns es) =
+ unlines $ [graphtype t ++ " {"]
+ ++ map (++";") (map prAttr at
+ ++ map prNode ns
+ ++ map (prEdge t) es)
+ ++ ["}\n"]
+
+graphtype :: GraphType -> String
+graphtype Directed = "digraph"
+graphtype Undirected = "graph"
+
+prNode :: Node -> String
+prNode (Node n at) = esc n ++ " " ++ prAttrList at
+
+prEdge :: GraphType -> Edge -> String
+prEdge t (Edge x y at) = esc x ++ " " ++ edgeop t ++ " " ++ prAttrList at
+
+edgeop :: GraphType -> String
+edgeop Directed = "->"
+edgeop Undirected = "--"
+
+prAttrList :: [Attr] -> String
+prAttrList = join "," . map prAttr
+
+prAttr :: Attr -> String
+prAttr (n,v) = esc n ++ " = " ++ esc v
+
+esc :: String -> String
+esc s = "\"" ++ concat [ if shouldEsc c then ['\\',c] else [c] | c <- s ] ++ "\""
+ where shouldEsc = (`elem` ['"', '\\']) \ No newline at end of file