Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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`
- `Label` of `Edge` and `Node` now support HTML and no formatting: To **migrate** replace old calls to `Label` with calls to `label`
32 changes: 26 additions & 6 deletions src/Data/DotLang.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/Data/DotLang/Attr/Global.purs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ instance pageDirValueDotLang :: DotLang PageDirValue where
data Attr
= RankDir RankDirValue
| PageDir PageDirValue
| Concentrate Boolean

derive instance genericAttr :: Generic Attr _

Expand All @@ -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
21 changes: 17 additions & 4 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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(..))
Expand All @@ -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",
Expand All @@ -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)