This repository has been archived by the owner on Apr 8, 2023. It is now read-only.
forked from kfish/vacuum
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use graphviz for creating Dot code rather than directly creating it w…
…ith pretty Documentation needs to be improved: the examples in GHC.Vacuum are no longer valid (that said, they weren't valid even before this change).
- Loading branch information
Showing
4 changed files
with
33 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,45 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
{- | | ||
> import GHC.Vacuum.Pretty.Dot | ||
> import Data.GraphViz.Commands | ||
> graphToDotPng :: FilePath -> [(String,[String])] -> IO FilePath | ||
> graphToDotPng fpre g = addExtension (runGraphviz (graphToDot g)) Png fpre | ||
-} | ||
module GHC.Vacuum.Pretty.Dot ( | ||
graphToDot | ||
,ppGraph,ppEdge,gStyle | ||
-- ,Doc,text,render | ||
,graphToDotParams | ||
,vacuumParams | ||
) where | ||
|
||
import Text.PrettyPrint | ||
|
||
------------------------------------------------ | ||
import Data.GraphViz hiding (graphToDot) | ||
import Data.GraphViz.Attributes.Complete( Attribute(RankDir, Splines, FontName) | ||
, RankDir(FromLeft), EdgeType(SplineEdges)) | ||
|
||
-- | . | ||
graphToDot :: (a -> String) -> [(a, [a])] -> Doc | ||
graphToDot f = ppGraph . fmap (f *** fmap f) | ||
where f *** g = \(a, b)->(f a, g b) | ||
import Control.Arrow(second) | ||
|
||
------------------------------------------------ | ||
|
||
gStyle :: String | ||
gStyle = unlines | ||
[" graph [rankdir=LR, splines=true];" | ||
," node [label=\"\\N\", shape=none, fontcolor=blue, fontname=courier];" | ||
," edge [color=black, style=dotted, fontname=courier, arrowname=onormal];"] | ||
graphToDot :: (Ord a) => [(a, [a])] -> DotGraph a | ||
graphToDot = graphToDotParams vacuumParams | ||
|
||
ppGraph :: [(String, [String])] -> Doc | ||
ppGraph xs = (text "digraph g" <+> text "{") | ||
$+$ text gStyle | ||
$+$ nest indent (vcat . fmap ppEdge $ xs) | ||
$+$ text "}" | ||
where indent = 2 | ||
graphToDotParams :: (Ord a, Ord cl) => GraphvizParams a () () cl l -> [(a, [a])] -> DotGraph a | ||
graphToDotParams params nes = graphElemsToDot params ns es | ||
where | ||
ns = map (second $ const ()) nes | ||
|
||
ppEdge :: (String, [String]) -> Doc | ||
ppEdge (x,xs) = (dQText x) <+> (text "->") | ||
<+> (braces . hcat . punctuate semi | ||
. fmap dQText $ xs) | ||
es = concatMap mkEs nes | ||
mkEs (f,ts) = map (\t -> (f,t,())) ts | ||
|
||
dQText :: String -> Doc | ||
dQText = doubleQuotes . text | ||
------------------------------------------------ | ||
|
||
{- | ||
import System.Cmd | ||
import System.Exit | ||
graphToDotPng :: FilePath -> [(String,[String])] -> IO Bool | ||
graphToDotPng fpre g = do | ||
let [dot,png] = fmap (fpre++) [".dot",".png"] | ||
writeFile dot | ||
. render . ppGraph | ||
-- . fmap (show***fmap show) | ||
$ g | ||
((==ExitSuccess) `fmap`) .system . intercalate " " $ | ||
-- ["cat",dot,"|","dot -Tpng",">",png,"2>/dev/null;","gliv",png,"&"] | ||
["cat",dot,"|","dot -Tpng",">",png,"2>/dev/null;","display",png,"&"] | ||
vacuumParams :: GraphvizParams a () () () () | ||
vacuumParams = defaultParams { globalAttributes = gStyle } | ||
|
||
graphToDotPdf :: FilePath -> [(String,[String])] -> IO Bool | ||
graphToDotPdf fpre g = do | ||
let [dot,png] = fmap (fpre++) [".dot",".pdf"] | ||
writeFile dot | ||
. render . ppGraph | ||
-- . fmap (show***fmap show) | ||
$ g | ||
((==ExitSuccess) `fmap`) .system . intercalate " " $ | ||
-- ["cat",dot,"|","dot -Tpng",">",png,"2>/dev/null;","gliv",png,"&"] | ||
["cat",dot,"tred","|","dot -Tpdf",">",png,"2>/dev/null;","evince",png,"&"] | ||
-} | ||
gStyle :: [GlobalAttributes] | ||
gStyle = [ GraphAttrs [RankDir FromLeft, Splines SplineEdges, FontName "courier"] | ||
, NodeAttrs [textLabel "\\N", shape PlainText, fontColor Blue] | ||
, EdgeAttrs [color Black, style dotted] | ||
] | ||
|
||
------------------------------------------------ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters