summaryrefslogtreecommitdiff
path: root/Dot.hs
blob: 0507c638ce1f821ba7cae292bf1abf7c12b9e50e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{- a simple graphviz / dot(1) digraph description generator library
 -
 - Copyright 2010 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Dot where -- import qualified

{- generates a graph description from a list of lines -}
graph :: [String] -> String
graph s = unlines $ [header] ++ map formatLine s ++ [footer]
	where
		header = "digraph map {"
		footer= "}"

{- a node in the graph -}
graphNode :: String -> String -> String
graphNode nodeid desc = label desc $ quote nodeid

{- an edge between two nodes -}
graphEdge :: String -> String -> Maybe String -> String
graphEdge fromid toid desc =
	case desc of
		Nothing -> edge
		Just d -> label d edge
	where
		edge = quote fromid ++ " -> " ++ quote toid

{- adds a label to a node or edge -}
label :: String -> String -> String
label l s = attr "label" l s

{- adds an attribute to a node or edge
 - (can be called multiple times for multiple attributes) -}
attr :: String -> String -> String -> String
attr a v s = s ++ " [ " ++ a ++ "=" ++ quote v ++ " ]"

{- fills a node with a color -}
fillColor :: String -> String -> String
fillColor color s = attr "fillcolor" color $ attr "style" "filled" $ s

{- apply to graphNode to put the node in a labeled box -}
subGraph :: String -> String -> String -> String
subGraph subid l s =
	"subgraph " ++ name ++ "{\n\t" ++ setlabel ++ "\n\t\t" ++ s ++ "\n\t}"
	where
		-- the "cluster_" makes dot draw a box
		name = quote ("cluster_" ++ subid)
		setlabel = formatLine $ "label=" ++ quote l

formatLine :: String -> String	
formatLine s = "\t" ++ s ++ ";"

quote :: String -> String
quote s = "\"" ++ s' ++ "\""
	where
		s' = filter (/= '"') s