diff --git a/README.md b/README.md index c85e0bc..f2157ca 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ documentation can be found on [pursuit](https://pursuit.purescript.org/packages/ define your model like this: ```purescript -DiGraph [ +digraph [ node "a" [ Shape Diamond, Style Filled, Node.FillColor red ], node "b" [], "a" ==> "b", @@ -68,9 +68,16 @@ bower i purescript-dotlang Changelog ========= +v4.0.0 +------ + +Breaking Changes: + +- Strictness behaviour of the graph is now supported: To **migrate** replace old calls to `DiGraph` with `digraph` + v3.0.0 ------ Breaking Changes: -- `Label` of `Edge` and `Node` now support HTML and no formatting: To **migrate** replace old calls to `Label` with calls to `label` \ No newline at end of file +- `Label` of `Edge` and `Node` now support HTML and no formatting: To **migrate** replace old calls to `Label` with calls to `label` diff --git a/src/Data/DotLang.purs b/src/Data/DotLang.purs index dc6c7e7..fd12038 100644 --- a/src/Data/DotLang.purs +++ b/src/Data/DotLang.purs @@ -167,20 +167,40 @@ instance definitionDotlang :: DotLang Definition where toText (EdgeDef e) = toText e <> "; " toText (Subgraph defs) = "subgraph { " <> (joinWith "" $ toText <$> defs) <> "}" +-- | A graph can either be strict or non-strict +data Strict + = Strict + | NonStrict + +instance strictDotLang :: DotLang Strict where + toText Strict = "strict " + toText NonStrict = "" + -- | graph can either be a graph or digraph data Graph - = Graph (Array Definition) - | DiGraph (Array Definition) - + = Graph Strict (Array Definition) + | DiGraph Strict (Array Definition) instance graphDotLang :: DotLang Graph where - toText (Graph defs) = "graph {" <> (joinWith "" $ toText <$> defs) <> "}" - toText (DiGraph defs) = "digraph {" <> (joinWith "" $ toText <$> defs) <> "}" + toText (Graph strict defs) = toText strict <> "graph {" <> (joinWith "" $ toText <$> defs) <> "}" + toText (DiGraph strict defs) = toText strict <> "digraph {" <> (joinWith "" $ toText <$> defs) <> "}" + +digraph :: Array Definition -> Graph +digraph = DiGraph NonStrict + +graph :: Array Definition -> Graph +graph = Graph NonStrict + +strictDigraph :: Array Definition -> Graph +strictDigraph = DiGraph Strict + +strictGraph :: Array Definition -> Graph +strictGraph = Graph Strict -- | create graph from Nodes and Edges -- | example: `graphFromElements [Node "e" [], Node "d" []] [Edge "e" "f"]` graphFromElements :: Array (Node) -> Array (Edge) -> Graph -graphFromElements n e = DiGraph $ (NodeDef <$> n) <> (EdgeDef <$> e) +graphFromElements n e = digraph $ (NodeDef <$> n) <> (EdgeDef <$> e) -- | `a` is a type that can be represented by a Dot-Graph class GraphRepr a where diff --git a/src/Data/DotLang/Attr/Global.purs b/src/Data/DotLang/Attr/Global.purs index 0db342d..df85262 100644 --- a/src/Data/DotLang/Attr/Global.purs +++ b/src/Data/DotLang/Attr/Global.purs @@ -45,6 +45,7 @@ instance pageDirValueDotLang :: DotLang PageDirValue where data Attr = RankDir RankDirValue | PageDir PageDirValue + | Concentrate Boolean derive instance genericAttr :: Generic Attr _ @@ -54,3 +55,4 @@ instance showAttr :: Show Attr where instance attrDotLang :: DotLang Attr where toText (RankDir dir) = "rankdir=" <> toText dir toText (PageDir dir) = "pagedir=" <> toText dir + toText (Concentrate concentrate) = "concentrate=" <> show concentrate diff --git a/test/Main.purs b/test/Main.purs index b87b311..e0af019 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -3,7 +3,7 @@ module Test.Main where import Prelude import Color.Scheme.MaterialDesign (red) -import Data.DotLang (Definition(..), Graph(..), Edge(..), EdgeType(..), global, node, (==>), (=*>)) +import Data.DotLang (Definition(..), Edge(..), EdgeType(..), digraph, strictDigraph, global, node, (==>), (=*>)) import Data.DotLang.Attr (FillStyle(..)) import Data.DotLang.Attr.Edge as Edge import Data.DotLang.Attr.Node (Attr(..), ShapeType(..)) @@ -21,8 +21,8 @@ main = runTest do suite "DotLang" do test "basic test" do let - g = DiGraph [ - global [ Global.RankDir FromLeft ], + g = digraph [ + global [ Global.RankDir FromLeft, Global.Concentrate true ], node "a" [ Shape Diamond, Style Filled, Node.FillColor red ], node "b" [], "a" ==> "b", @@ -31,7 +31,20 @@ main = runTest do node "d" [] ] ] - equal "digraph {rankdir=LR; a [shape=diamond, style=filled, fillcolor=\"#f44336\"]; b []; a -> b; a -> d [fillcolor=\"#f44336\"]; subgraph { d []; }}" (toText g) + equal "digraph {rankdir=LR; concentrate=true; a [shape=diamond, style=filled, fillcolor=\"#f44336\"]; b []; a -> b; a -> d [fillcolor=\"#f44336\"]; subgraph { d []; }}" (toText g) test "examples from documentation" do equal (toText $ Edge Forward "a" "b" []) "a -> b" equal (toText $ "a" =*> "b" $ [ Edge.FillColor red ]) "a -> b [fillcolor=\"#f44336\"]; " + test "strict graph" do + let + g = strictDigraph [ + global [ Global.RankDir FromLeft ], + node "a" [ Shape Diamond, Style Filled, Node.FillColor red ], + node "b" [], + "a" ==> "b", + "a" =*> "d" $ [ Edge.FillColor red ], + Subgraph [ + node "d" [] + ] + ] + equal "strict digraph {rankdir=LR; a [shape=diamond, style=filled, fillcolor=\"#f44336\"]; b []; a -> b; a -> d [fillcolor=\"#f44336\"]; subgraph { d []; }}" (toText g)