Skip to content

Commit 22290d6

Browse files
authored
Merge pull request #22 from JansthcirlU/20-subgraph-implementation
20-subgraph-implementation Closes #20 and #21
2 parents caf88e6 + cd04813 commit 22290d6

28 files changed

+723
-124
lines changed
Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
using System.Text;
2+
using Mermaid.Flowcharts.Links;
3+
using Mermaid.Flowcharts.Nodes;
4+
using Mermaid.Flowcharts.Subgraphs;
25

36
namespace Mermaid.Flowcharts;
47

5-
public class Flowchart
8+
public class Flowchart : IMermaidPrintable
69
{
7-
private readonly List<Node> _nodes = [];
10+
private readonly List<INode> _nodes = [];
811
private readonly List<Link> _links = [];
912

1013
public FlowchartTitle? Title { get; }
14+
public IEnumerable<Node> Nodes => _nodes.OfType<Node>();
15+
public IEnumerable<Subgraph> Subgraphs => _nodes.OfType<Subgraph>();
1116

1217
public Flowchart()
1318
{
@@ -18,41 +23,54 @@ public Flowchart(FlowchartTitle title)
1823
Title = title;
1924
}
2025

21-
public Flowchart AddNode(Node node)
26+
public Flowchart AddNode(INode node)
2227
{
23-
if (!_nodes.Any(n => n.Id == node.Id)) _nodes.Add(node);
28+
if (ContainsNode(node) || ContainsNodeNested(node)) return this;
29+
30+
_nodes.Add(node);
2431
return this;
2532
}
2633

2734
public Flowchart AddLink(Link link)
2835
{
2936
_links.Add(link);
37+
AddNode(link.Source);
38+
AddNode(link.Destination);
3039
return this;
3140
}
3241

3342
public override string ToString()
43+
=> ToMermaidString();
44+
45+
public string ToMermaidString(int indentations = 0, string indentationText = " ")
3446
{
3547
StringBuilder flowchartStringBuilder = new();
36-
if (Title is not null)
48+
if (Title is FlowchartTitle title)
3749
{
38-
flowchartStringBuilder.AppendLine(Title.ToString());
50+
flowchartStringBuilder.AppendLine(title.ToMermaidString(indentations, indentationText));
3951
}
4052
flowchartStringBuilder.AppendLine("flowchart TD");
4153

42-
IEnumerable<Node> allNodes = _nodes
43-
.Concat(_links.Select(link => link.Source))
44-
.Concat(_links.Select(link => link.Destination))
45-
.OrderBy(node => node.Id.Value)
46-
.DistinctBy(node => node.Id);
47-
foreach (Node node in allNodes)
54+
foreach (Node node in Nodes)
55+
{
56+
flowchartStringBuilder.AppendLine(node.ToMermaidString(indentations + 1, indentationText));
57+
}
58+
if (Subgraphs.Any()) flowchartStringBuilder.AppendLine();
59+
foreach (Subgraph subgraph in Subgraphs)
4860
{
49-
flowchartStringBuilder.AppendLine($" {node.ToString()}");
61+
flowchartStringBuilder.AppendLine(subgraph.ToMermaidString(indentations + 1, indentationText));
5062
}
51-
flowchartStringBuilder.AppendLine();
63+
if (_links.Any()) flowchartStringBuilder.AppendLine();
5264
foreach (Link link in _links)
5365
{
54-
flowchartStringBuilder.AppendLine($" {link.ToString()}");
66+
flowchartStringBuilder.AppendLine(link.ToMermaidString(indentations + 1, indentationText));
5567
}
5668
return flowchartStringBuilder.ToString();
5769
}
70+
71+
internal bool ContainsNode(INode node)
72+
=> Nodes.Any(n => n.Id == node.Id);
73+
74+
internal bool ContainsNodeNested(INode node)
75+
=> Subgraphs.Any(s => s.ContainsNode(node) || s.ContainsNodeNested(node));
5876
}

src/Mermaid.Flowcharts/FlowchartTitle.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Mermaid.Flowcharts;
22

3-
public readonly struct FlowchartTitle
3+
public readonly struct FlowchartTitle : IMermaidPrintable
44
{
55
public string Text { get; }
66

@@ -28,5 +28,8 @@ public static FlowchartTitle FromString(string text)
2828
}
2929

3030
public override string ToString()
31-
=> Text;
31+
=> ToMermaidString();
32+
33+
public string ToMermaidString(int indentations = 0, string indentationText = " ")
34+
=> $"{indentationText.Repeat(indentations)}{Text.Replace("\n", $"\n{indentationText.Repeat(indentations)}")}";
3235
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Mermaid.Flowcharts;
2+
3+
public interface IMermaidPrintable
4+
{
5+
string ToMermaidString(int indentations = 0, string indentationText = " ");
6+
}

src/Mermaid.Flowcharts/Link.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Mermaid.Flowcharts.Nodes;
2+
3+
namespace Mermaid.Flowcharts.Links;
4+
5+
public readonly record struct Link : IMermaidPrintable
6+
{
7+
public readonly INode Source { get; }
8+
public readonly INode Destination { get; }
9+
public readonly LinkStyle Style { get; }
10+
public readonly LinkText? Text { get; }
11+
12+
public Link(
13+
INode source,
14+
INode destination,
15+
LinkStyle style,
16+
LinkText? text = null)
17+
{
18+
Source = source;
19+
Destination = destination;
20+
Style = style;
21+
Text = text;
22+
}
23+
24+
public override string ToString()
25+
=> ToMermaidString();
26+
27+
public string ToMermaidString(int indentations = 0, string indentationText = " ")
28+
=> Text is null
29+
? $"{indentationText.Repeat(indentations)}{Source.Id} {Style} {Destination.Id}"
30+
: $"{indentationText.Repeat(indentations)}{Source.Id} {Style}|{Text}| {Destination.Id}";
31+
}

src/Mermaid.Flowcharts/LinkArrowType.cs renamed to src/Mermaid.Flowcharts/Links/LinkArrowType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Mermaid.Flowcharts;
1+
namespace Mermaid.Flowcharts.Links;
22

33
public enum LinkArrowType
44
{

src/Mermaid.Flowcharts/LinkDirection.cs renamed to src/Mermaid.Flowcharts/Links/LinkDirection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Mermaid.Flowcharts;
1+
namespace Mermaid.Flowcharts.Links;
22

33
public enum LinkDirection
44
{
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
namespace Mermaid.Flowcharts;
1+
namespace Mermaid.Flowcharts.Links;
22

3-
public readonly record struct LinkStyle
3+
public readonly record struct LinkStyle : IMermaidPrintable
44
{
55
public readonly LinkArrowType ArrowType { get; } = LinkArrowType.Arrow;
66
public readonly LinkDirection Direction { get; } = LinkDirection.LeftToRight;
@@ -17,6 +17,9 @@ public LinkStyle(
1717
}
1818

1919
public override string ToString()
20+
=> ToMermaidString();
21+
22+
public string ToMermaidString(int indentations = 0, string indentationText = " ")
2023
{
2124
string thickness = Thickness switch
2225
{
@@ -26,7 +29,7 @@ public override string ToString()
2629
_ => "---"
2730
};
2831
if (Thickness is LinkThickness.Invisible) return thickness;
29-
32+
3033
string arrowLeft = (ArrowType, Direction) switch
3134
{
3235
(LinkArrowType.Arrow, LinkDirection.RightToLeft or LinkDirection.Both) => "<",
@@ -41,11 +44,11 @@ public override string ToString()
4144
(LinkArrowType.Cross, _) => "x",
4245
_ => string.Empty
4346
};
44-
return Direction switch
47+
return $"{indentationText.Repeat(indentations)}{Direction switch
4548
{
4649
LinkDirection.RightToLeft => $"{arrowLeft}{thickness}",
4750
LinkDirection.Both => $"{arrowLeft}{thickness}{arrowRight}",
4851
_ => $"{thickness}{arrowRight}",
49-
};
52+
}}";
5053
}
5154
}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System.Buffers;
22

3-
namespace Mermaid.Flowcharts;
3+
namespace Mermaid.Flowcharts.Links;
44

5-
public readonly record struct LinkText
5+
public readonly record struct LinkText : IMermaidPrintable
66
{
77
public string Value { get; }
88

@@ -14,7 +14,7 @@ public LinkText()
1414
}
1515
private LinkText(string text)
1616
=> Value = text;
17-
17+
1818
public static LinkText FromString(string value)
1919
{
2020
if (string.IsNullOrEmpty(value)) throw new ArgumentException("Link text must not be null or empty.", nameof(value));
@@ -23,4 +23,10 @@ public static LinkText FromString(string value)
2323
if (illegalCharacterIndex > -1) throw new ArgumentException($"Link text must not contain illegal character \"{value[illegalCharacterIndex]}\".", nameof(value));
2424
return new(value);
2525
}
26+
27+
public override string ToString()
28+
=> ToMermaidString();
29+
30+
public string ToMermaidString(int indentations = 0, string indentationText = " ")
31+
=> $"{indentationText.Repeat(indentations)}{Value}";
2632
}

src/Mermaid.Flowcharts/LinkThickness.cs renamed to src/Mermaid.Flowcharts/Links/LinkThickness.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Mermaid.Flowcharts;
1+
namespace Mermaid.Flowcharts.Links;
22

33
public enum LinkThickness
44
{

0 commit comments

Comments
 (0)