summaryrefslogtreecommitdiff
path: root/src/Hakyll/Core/DirectedGraph/Dot.hs
blob: 06198e4c7b4fcf9c6bfc0b58cee3493e81c9eb84 (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
--------------------------------------------------------------------------------
-- | Dump a directed graph in dot format. Used for debugging purposes
module Hakyll.Core.DirectedGraph.Dot
    ( toDot
    , writeDot
    ) where


--------------------------------------------------------------------------------
import qualified Data.Set                  as S
import           Hakyll.Core.DirectedGraph


--------------------------------------------------------------------------------
-- | Convert a directed graph into dot format for debugging purposes
toDot :: Ord a
      => (a -> String)    -- ^ Convert nodes to dot names
      -> DirectedGraph a  -- ^ Graph to dump
      -> String           -- ^ Resulting string
toDot showTag graph = unlines $ concat
    [ return "digraph dependencies {"
    , map showNode (S.toList $ nodes graph)
    , concatMap showEdges (S.toList $ nodes graph)
    , return "}"
    ]
  where
    showNode node = "    \"" ++ showTag node ++ "\";"
    showEdges node = map (showEdge node) $ neighbours node graph
    showEdge x y = "    \"" ++ showTag x ++ "\" -> \"" ++ showTag y ++ "\";"


--------------------------------------------------------------------------------
-- | Write out the @.dot@ file to a given file path. See 'toDot' for more
-- information.
writeDot :: Ord a => FilePath -> (a -> String) -> DirectedGraph a -> IO ()
writeDot path showTag = writeFile path . toDot showTag