From eba19b2bab0ffe502ed27a9bdb494086a80fff89 Mon Sep 17 00:00:00 2001 From: CHIKAMATSU Naohiro Date: Wed, 18 Dec 2024 19:55:30 +0900 Subject: [PATCH] Add example code --- README.md | 3 +- doc/architecture/generated.md | 2 +- doc/er/generated.md | 32 +++--- doc/flowchart/generated.md | 16 +-- mermaid/arch/examples_test.go | 118 ++++++++++++++++++++ mermaid/er/entity.go | 8 +- mermaid/er/entity_relationship_test.go | 38 +++---- mermaid/er/examples_test.go | 143 +++++++++++++++++++++++++ mermaid/flowchart/examples_test.go | 53 +++++++++ mermaid/flowchart/flowchart_test.go | 80 +++++++------- mermaid/flowchart/link.go | 18 ++-- mermaid/flowchart/node.go | 34 +++--- mermaid/piechart/examples_test.go | 42 ++++++++ mermaid/sequence/examples_test.go | 64 +++++++++++ 14 files changed, 536 insertions(+), 115 deletions(-) create mode 100644 mermaid/arch/examples_test.go create mode 100644 mermaid/er/examples_test.go create mode 100644 mermaid/flowchart/examples_test.go create mode 100644 mermaid/piechart/examples_test.go create mode 100644 mermaid/sequence/examples_test.go diff --git a/README.md b/README.md index 40b687d..f72b641 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ # What is markdown package The Package markdown is a simple markdown builder in golang. The markdown package assembles Markdown using method chaining, not uses a template engine like [html/template](https://pkg.go.dev/html/template). The syntax of Markdown follows **GitHub Markdown**. -The markdown package was initially developed to save test results in [nao1215/spectest](https://github.com/nao1215/spectest). Therefore, the markdown package implements the features required by spectest. For example, the markdown package supports **mermaid sequence diagrams (entity relationship diagram, sequence diagram, flowchart, pie chart)**, which was a necessary feature in spectest. +The markdown package was initially developed to save test results in [nao1215/spectest](https://github.com/nao1215/spectest). Therefore, the markdown package implements the features required by spectest. For example, the markdown package supports **mermaid sequence diagrams (entity relationship diagram, sequence diagram, flowchart, pie chart, architecture diagram)**, which was a necessary feature in spectest. Additionally, complex code that increases the complexity of the library, such as generating nested lists, will not be added. I want to keep this library as simple as possible. @@ -36,6 +36,7 @@ Additionally, complex code that increases the complexity of the library, such as - [x] mermaid entity relationship diagram - [x] mermaid flowchart - [x] mermaid pie chart +- [x] mermaid architecture diagram (beta feature) ### Features not in Markdown syntax - Generate badges; RedBadge(), YellowBadge(), GreenBadge(). diff --git a/doc/architecture/generated.md b/doc/architecture/generated.md index 4e4ca3b..159355e 100644 --- a/doc/architecture/generated.md +++ b/doc/architecture/generated.md @@ -15,4 +15,4 @@ architecture-beta junctionCenter:R -- L:junctionRight top_gateway:B -- T:junctionRight bottom_gateway:T -- B:junctionRight -``` +``` \ No newline at end of file diff --git a/doc/er/generated.md b/doc/er/generated.md index bbbdaa8..1ae736e 100644 --- a/doc/er/generated.md +++ b/doc/er/generated.md @@ -1,21 +1,21 @@ ## Entity Relationship Diagram ```mermaid erDiagram - teachers ||--o{ students : "Teacher has many students" - teachers }|..|| schools : "School has many teachers" - schools { - int id PK,UK "School ID" - string name "School Name" - int teacher_id FK,UK "Teacher ID" - } - students { - int id PK,UK "Student ID" - string name "Student Name" - int teacher_id FK,UK "Teacher ID" - } - teachers { - int id PK,UK "Teacher ID" - string name "Teacher Name" - } + teachers ||--o{ students : "Teacher has many students" + teachers }|..|| schools : "School has many teachers" + schools { + int id PK,UK "School ID" + string name "School Name" + int teacher_id FK,UK "Teacher ID" + } + students { + int id PK,UK "Student ID" + string name "Student Name" + int teacher_id FK,UK "Teacher ID" + } + teachers { + int id PK,UK "Teacher ID" + string name "Teacher Name" + } ``` \ No newline at end of file diff --git a/doc/flowchart/generated.md b/doc/flowchart/generated.md index e4c1ca2..3c4bc8e 100644 --- a/doc/flowchart/generated.md +++ b/doc/flowchart/generated.md @@ -4,12 +4,12 @@ title: mermaid flowchart builder --- flowchart TB - A["Node A"] - B(["Node B"]) - C[["Node C"]] - D[("Database")] - A-->B - B-->|"send original data"|D - B-->C - C-. "send filtered data" .-> D + A["Node A"] + B(["Node B"]) + C[["Node C"]] + D[("Database")] + A-->B + B-->|"send original data"|D + B-->C + C-. "send filtered data" .-> D ``` \ No newline at end of file diff --git a/mermaid/arch/examples_test.go b/mermaid/arch/examples_test.go new file mode 100644 index 0000000..80df50b --- /dev/null +++ b/mermaid/arch/examples_test.go @@ -0,0 +1,118 @@ +//go:build linux || darwin + +package arch_test + +import ( + "io" + "os" + + "github.com/nao1215/markdown" + "github.com/nao1215/markdown/mermaid/arch" +) + +// ExampleArchitecture skip this test on Windows. +// The newline codes in the comment section where +// the expected values are written are represented as '\n', +// causing failures when testing on Windows. +func ExampleArchitecture() { + diagram := arch.NewArchitecture(io.Discard). + Service("left_disk", arch.IconDisk, "Disk"). + Service("top_disk", arch.IconDisk, "Disk"). + Service("bottom_disk", arch.IconDisk, "Disk"). + Service("top_gateway", arch.IconInternet, "Gateway"). + Service("bottom_gateway", arch.IconInternet, "Gateway"). + Junction("junctionCenter"). + Junction("junctionRight"). + LF(). + Edges( + arch.Edge{ + ServiceID: "left_disk", + Position: arch.PositionRight, + Arrow: arch.ArrowNone, + }, + arch.Edge{ + ServiceID: "junctionCenter", + Position: arch.PositionLeft, + Arrow: arch.ArrowNone, + }). + Edges( + arch.Edge{ + ServiceID: "top_disk", + Position: arch.PositionBottom, + Arrow: arch.ArrowNone, + }, + arch.Edge{ + ServiceID: "junctionCenter", + Position: arch.PositionTop, + Arrow: arch.ArrowNone, + }). + Edges( + arch.Edge{ + ServiceID: "bottom_disk", + Position: arch.PositionTop, + Arrow: arch.ArrowNone, + }, + arch.Edge{ + ServiceID: "junctionCenter", + Position: arch.PositionBottom, + Arrow: arch.ArrowNone, + }). + Edges( + arch.Edge{ + ServiceID: "junctionCenter", + Position: arch.PositionRight, + Arrow: arch.ArrowNone, + }, + arch.Edge{ + ServiceID: "junctionRight", + Position: arch.PositionLeft, + Arrow: arch.ArrowNone, + }). + Edges( + arch.Edge{ + ServiceID: "top_gateway", + Position: arch.PositionBottom, + Arrow: arch.ArrowNone, + }, + arch.Edge{ + ServiceID: "junctionRight", + Position: arch.PositionTop, + Arrow: arch.ArrowNone, + }). + Edges( + arch.Edge{ + ServiceID: "bottom_gateway", + Position: arch.PositionTop, + Arrow: arch.ArrowNone, + }, + arch.Edge{ + ServiceID: "junctionRight", + Position: arch.PositionBottom, + Arrow: arch.ArrowNone, + }).String() //nolint + + markdown.NewMarkdown(os.Stdout). + H2("Architecture Diagram"). + CodeBlocks(markdown.SyntaxHighlightMermaid, diagram). + Build() //nolint + + // Output: + //## Architecture Diagram + //```mermaid + //architecture-beta + // service left_disk(disk)[Disk] + // service top_disk(disk)[Disk] + // service bottom_disk(disk)[Disk] + // service top_gateway(internet)[Gateway] + // service bottom_gateway(internet)[Gateway] + // junction junctionCenter + // junction junctionRight + // + // left_disk:R -- L:junctionCenter + // top_disk:B -- T:junctionCenter + // bottom_disk:T -- B:junctionCenter + // junctionCenter:R -- L:junctionRight + // top_gateway:B -- T:junctionRight + // bottom_gateway:T -- B:junctionRight + //``` +} diff --git a/mermaid/er/entity.go b/mermaid/er/entity.go index 1637e81..ed3ba98 100644 --- a/mermaid/er/entity.go +++ b/mermaid/er/entity.go @@ -24,12 +24,12 @@ func (e *Entity) string() string { return fmt.Sprintf( "%s%s {%s%s%s%s}", - " ", // indent + " ", // indent e.Name, internal.LineFeed(), strings.Join(attrs, internal.LineFeed()), internal.LineFeed(), - " ", // indent + " ", // indent ) } @@ -70,7 +70,7 @@ func (a *Attribute) string() string { keys = append(keys, "UK") } - s := fmt.Sprintf(" %s %s %s \"%s\"", a.Type, a.Name, strings.Join(keys, ","), a.Comment) + s := fmt.Sprintf(" %s %s %s \"%s\"", a.Type, a.Name, strings.Join(keys, ","), a.Comment) s = strings.TrimSuffix(s, " ") return strings.ReplaceAll(s, "\"\"", "") } @@ -84,7 +84,7 @@ func (a *Attribute) string() string { func (d *Diagram) Relationship(leftE, rightE Entity, leftR, rightR Relationship, identidy Identify, comment string) *Diagram { d.body = append( d.body, - fmt.Sprintf(" %s %s%s%s %s : \"%s\"", + fmt.Sprintf(" %s %s%s%s %s : \"%s\"", leftE.Name, leftR.string(left), identidy.string(), diff --git a/mermaid/er/entity_relationship_test.go b/mermaid/er/entity_relationship_test.go index a337e8e..599a71b 100644 --- a/mermaid/er/entity_relationship_test.go +++ b/mermaid/er/entity_relationship_test.go @@ -133,25 +133,25 @@ func TestDiagram_Build(t *testing.T) { } want := `erDiagram - teachers ||--o{ students : "Teacher has many students" - teachers }|..|| schools : "School has many teachers" - personal_computers { - int id PK,UK "Personal Computer ID" - } - schools { - int id PK,UK "School ID" - string name "School Name" - int teacher_id FK,UK "Teacher ID" - } - students { - int id PK,UK "Student ID" - string name "Student Name" - int teacher_id FK,UK "Teacher ID" - } - teachers { - int id PK,UK "Teacher ID" - string name "Teacher Name" - } + teachers ||--o{ students : "Teacher has many students" + teachers }|..|| schools : "School has many teachers" + personal_computers { + int id PK,UK "Personal Computer ID" + } + schools { + int id PK,UK "School ID" + string name "School Name" + int teacher_id FK,UK "Teacher ID" + } + students { + int id PK,UK "Student ID" + string name "Student Name" + int teacher_id FK,UK "Teacher ID" + } + teachers { + int id PK,UK "Teacher ID" + string name "Teacher Name" + } ` want = strings.ReplaceAll(want, "\r\n", "\n") got := strings.ReplaceAll(b.String(), "\r\n", "\n") diff --git a/mermaid/er/examples_test.go b/mermaid/er/examples_test.go new file mode 100644 index 0000000..ddcb44a --- /dev/null +++ b/mermaid/er/examples_test.go @@ -0,0 +1,143 @@ +//go:build linux || darwin + +package er_test + +import ( + "os" + + "github.com/nao1215/markdown" + "github.com/nao1215/markdown/mermaid/er" +) + +// ExampleDiagram skip this test on Windows. +// The newline codes in the comment section where +// the expected values are written are represented as '\n', +// causing failures when testing on Windows. +func ExampleDiagram() { + teachers := er.NewEntity( + "teachers", + []*er.Attribute{ + { + Type: "int", + Name: "id", + IsPrimaryKey: true, + IsForeignKey: false, + IsUniqueKey: true, + Comment: "Teacher ID", + }, + { + Type: "string", + Name: "name", + IsPrimaryKey: false, + IsForeignKey: false, + IsUniqueKey: false, + Comment: "Teacher Name", + }, + }, + ) + students := er.NewEntity( + "students", + []*er.Attribute{ + { + Type: "int", + Name: "id", + IsPrimaryKey: true, + IsForeignKey: false, + IsUniqueKey: true, + Comment: "Student ID", + }, + { + Type: "string", + Name: "name", + IsPrimaryKey: false, + IsForeignKey: false, + IsUniqueKey: false, + Comment: "Student Name", + }, + { + Type: "int", + Name: "teacher_id", + IsPrimaryKey: false, + IsForeignKey: true, + IsUniqueKey: true, + Comment: "Teacher ID", + }, + }, + ) + schools := er.NewEntity( + "schools", + []*er.Attribute{ + { + Type: "int", + Name: "id", + IsPrimaryKey: true, + IsForeignKey: false, + IsUniqueKey: true, + Comment: "School ID", + }, + { + Type: "string", + Name: "name", + IsPrimaryKey: false, + IsForeignKey: false, + IsUniqueKey: false, + Comment: "School Name", + }, + { + Type: "int", + Name: "teacher_id", + IsPrimaryKey: false, + IsForeignKey: true, + IsUniqueKey: true, + Comment: "Teacher ID", + }, + }, + ) + + erString := er.NewDiagram(os.Stdout). + Relationship( + teachers, + students, + er.ExactlyOneRelationship, // "||" + er.ZeroToMoreRelationship, // "}o" + er.Identifying, // "--" + "Teacher has many students", + ). + Relationship( + teachers, + schools, + er.OneToMoreRelationship, // "|}" + er.ExactlyOneRelationship, // "||" + er.NonIdentifying, // ".." + "School has many teachers", + ). + String() + + markdown.NewMarkdown(os.Stdout). + H2("Entity Relationship Diagram"). + CodeBlocks(markdown.SyntaxHighlightMermaid, erString). + Build() //nolint + + // Output: + //## Entity Relationship Diagram + //```mermaid + //erDiagram + // teachers ||--o{ students : "Teacher has many students" + // teachers }|..|| schools : "School has many teachers" + // schools { + // int id PK,UK "School ID" + // string name "School Name" + // int teacher_id FK,UK "Teacher ID" + // } + // students { + // int id PK,UK "Student ID" + // string name "Student Name" + // int teacher_id FK,UK "Teacher ID" + // } + // teachers { + // int id PK,UK "Teacher ID" + // string name "Teacher Name" + // } + // + //``` +} diff --git a/mermaid/flowchart/examples_test.go b/mermaid/flowchart/examples_test.go new file mode 100644 index 0000000..30ec368 --- /dev/null +++ b/mermaid/flowchart/examples_test.go @@ -0,0 +1,53 @@ +//go:build linux || darwin + +package flowchart_test + +import ( + "os" + + "github.com/nao1215/markdown" + "github.com/nao1215/markdown/mermaid/flowchart" +) + +// ExampleFlowchart skip this test on Windows. +// The newline codes in the comment section where +// the expected values are written are represented as '\n', +// causing failures when testing on Windows. +func ExampleFlowchart() { + fc := flowchart.NewFlowchart( + os.Stdout, + flowchart.WithTitle("mermaid flowchart builder"), + flowchart.WithOrientalTopToBottom(), + ). + NodeWithText("A", "Node A"). + StadiumNode("B", "Node B"). + SubroutineNode("C", "Node C"). + DatabaseNode("D", "Database"). + LinkWithArrowHead("A", "B"). + LinkWithArrowHeadAndText("B", "D", "send original data"). + LinkWithArrowHead("B", "C"). + DottedLinkWithText("C", "D", "send filtered data"). + String() + + markdown.NewMarkdown(os.Stdout). + H2("Flowchart"). + CodeBlocks(markdown.SyntaxHighlightMermaid, fc). + Build() //nolint + + // Output: + //## Flowchart + //```mermaid + //--- + //title: mermaid flowchart builder + //--- + //flowchart TB + // A["Node A"] + // B(["Node B"]) + // C[["Node C"]] + // D[("Database")] + // A-->B + // B-->|"send original data"|D + // B-->C + // C-. "send filtered data" .-> D + //``` +} diff --git a/mermaid/flowchart/flowchart_test.go b/mermaid/flowchart/flowchart_test.go index 1483d36..0569d7d 100644 --- a/mermaid/flowchart/flowchart_test.go +++ b/mermaid/flowchart/flowchart_test.go @@ -40,14 +40,14 @@ func TestFlowchart_Build(t *testing.T) { title: mermaid flowchart builder --- flowchart TB - A["Node A"] - B(["Node B"]) - C[["Node C"]] - D[("Database")] - A-->B - B-->|"send original data"|D - B-->C - C-. "send filtered data" .-> D` + A["Node A"] + B(["Node B"]) + C[["Node C"]] + D[("Database")] + A-->B + B-->|"send original data"|D + B-->C + C-. "send filtered data" .-> D` got := strings.ReplaceAll(b.String(), "\r\n", "\n") @@ -71,7 +71,7 @@ flowchart TB } want := `flowchart TB - A["Node A"]` + A["Node A"]` got := strings.ReplaceAll(b.String(), "\r\n", "\n") if diff := cmp.Diff(want, got); diff != "" { @@ -93,7 +93,7 @@ flowchart TB } want := `flowchart TD - A["Node A"]` + A["Node A"]` got := strings.ReplaceAll(b.String(), "\r\n", "\n") if diff := cmp.Diff(want, got); diff != "" { @@ -116,7 +116,7 @@ flowchart TB } want := `flowchart BT - A["Node A"]` + A["Node A"]` got := strings.ReplaceAll(b.String(), "\r\n", "\n") if diff := cmp.Diff(want, got); diff != "" { @@ -139,7 +139,7 @@ flowchart TB } want := `flowchart RL - A["Node A"]` + A["Node A"]` got := strings.ReplaceAll(b.String(), "\r\n", "\n") if diff := cmp.Diff(want, got); diff != "" { @@ -162,7 +162,7 @@ flowchart TB } want := `flowchart LR - A["Node A"]` + A["Node A"]` got := strings.ReplaceAll(b.String(), "\r\n", "\n") if diff := cmp.Diff(want, got); diff != "" { @@ -212,36 +212,36 @@ D`).RoundEdgesNode("E", "Node E"). } want := `flowchart TB - A - B["Node B"] - ` + A + B["Node B"] + ` want += fmt.Sprintf("C[\"`**Node C**`\"]\n") //nolint:gosimple - want += fmt.Sprintf("\tD[\"`Node\n") //nolint:gosimple + want += fmt.Sprintf(" D[\"`Node\n") //nolint:gosimple want += "D`\"]" want += ` - E("Node E") - F(["Node F"]) - G[["Node G"]] - H[("Node H")] - I[("Database")] - J(("Node J")) - K>"Node K"] - L{"Node L"} - M{{"Node M"}} - N[/"Node N"/] - O[\"Node O"\] - P[/"Node P"\] - Q[\"Node Q"/] - R((("Node R"))) - A-->B - B-->|"send"|C - C --- D - D---|"send"|E - E-.->F - F-. "send" .-> G - G ==> H - H == "send" ==> I - I ~~~ J` + E("Node E") + F(["Node F"]) + G[["Node G"]] + H[("Node H")] + I[("Database")] + J(("Node J")) + K>"Node K"] + L{"Node L"} + M{{"Node M"}} + N[/"Node N"/] + O[\"Node O"\] + P[/"Node P"\] + Q[\"Node Q"/] + R((("Node R"))) + A-->B + B-->|"send"|C + C --- D + D---|"send"|E + E-.->F + F-. "send" .-> G + G ==> H + H == "send" ==> I + I ~~~ J` got := strings.ReplaceAll(b.String(), "\r\n", "\n") diff --git a/mermaid/flowchart/link.go b/mermaid/flowchart/link.go index 337668b..9dadfa0 100644 --- a/mermaid/flowchart/link.go +++ b/mermaid/flowchart/link.go @@ -4,54 +4,54 @@ import "fmt" // LinkWithArrowHead adds a link with an arrow head to the flowchart. func (f *Flowchart) LinkWithArrowHead(from, to string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s-->%s", from, to)) + f.body = append(f.body, fmt.Sprintf(" %s-->%s", from, to)) return f } // LinkWithArrowHeadAndText adds a link with an arrow head and text to the flowchart. func (f *Flowchart) LinkWithArrowHeadAndText(from, to, text string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s-->|\"%s\"|%s", from, text, to)) + f.body = append(f.body, fmt.Sprintf(" %s-->|\"%s\"|%s", from, text, to)) return f } // OpenLink adds an open link to the flowchart. func (f *Flowchart) OpenLink(from, to string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s --- %s", from, to)) + f.body = append(f.body, fmt.Sprintf(" %s --- %s", from, to)) return f } // OpenLinkWithText adds an open link with text to the flowchart. func (f *Flowchart) OpenLinkWithText(from, to, text string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s---|\"%s\"|%s", from, text, to)) + f.body = append(f.body, fmt.Sprintf(" %s---|\"%s\"|%s", from, text, to)) return f } // DottedLink adds a dotted link to the flowchart. func (f *Flowchart) DottedLink(from, to string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s-.->%s", from, to)) + f.body = append(f.body, fmt.Sprintf(" %s-.->%s", from, to)) return f } // DottedLinkWithText adds a dotted link with text to the flowchart. func (f *Flowchart) DottedLinkWithText(from, to, text string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s-. \"%s\" .-> %s", from, text, to)) + f.body = append(f.body, fmt.Sprintf(" %s-. \"%s\" .-> %s", from, text, to)) return f } // ThickLink adds a thick link to the flowchart. func (f *Flowchart) ThickLink(from, to string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s ==> %s", from, to)) + f.body = append(f.body, fmt.Sprintf(" %s ==> %s", from, to)) return f } // ThickLinkWithText adds a thick link with text to the flowchart. func (f *Flowchart) ThickLinkWithText(from, to, text string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s == \"%s\" ==> %s", from, text, to)) + f.body = append(f.body, fmt.Sprintf(" %s == \"%s\" ==> %s", from, text, to)) return f } // InvisibleLink adds an invisible link to the flowchart. func (f *Flowchart) InvisibleLink(from, to string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s ~~~ %s", from, to)) + f.body = append(f.body, fmt.Sprintf(" %s ~~~ %s", from, to)) return f } diff --git a/mermaid/flowchart/node.go b/mermaid/flowchart/node.go index dc0fc80..f480855 100644 --- a/mermaid/flowchart/node.go +++ b/mermaid/flowchart/node.go @@ -4,50 +4,50 @@ import "fmt" // Node adds a node to the flowchart. func (f *Flowchart) Node(name string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s", name)) + f.body = append(f.body, fmt.Sprintf(" %s", name)) return f } // NodeWithText adds a node with text to the flowchart. // Unicode characters are supported. func (f *Flowchart) NodeWithText(name, text string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s[\"%s\"]", name, text)) + f.body = append(f.body, fmt.Sprintf(" %s[\"%s\"]", name, text)) return f } // NodeWithMarkdown adds a node with markdown text to the flowchart. func (f *Flowchart) NodeWithMarkdown(name, markdownText string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s[\"`%s`\"]", name, markdownText)) + f.body = append(f.body, fmt.Sprintf(" %s[\"`%s`\"]", name, markdownText)) return f } // NodeWithNewLines adds a node with new lines to the flowchart. func (f *Flowchart) NodeWithNewLines(name, textWithNewLines string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s[\"`%s`\"]", name, textWithNewLines)) + f.body = append(f.body, fmt.Sprintf(" %s[\"`%s`\"]", name, textWithNewLines)) return f } // RoundEdgesNode adds a node with round edges to the flowchart. func (f *Flowchart) RoundEdgesNode(name, text string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s(\"%s\")", name, text)) + f.body = append(f.body, fmt.Sprintf(" %s(\"%s\")", name, text)) return f } // StadiumNode adds a node with stadium shape to the flowchart. func (f *Flowchart) StadiumNode(name, text string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s([\"%s\"])", name, text)) + f.body = append(f.body, fmt.Sprintf(" %s([\"%s\"])", name, text)) return f } // SubroutineNode adds a node with subroutine shape to the flowchart. func (f *Flowchart) SubroutineNode(name, text string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s[[\"%s\"]]", name, text)) + f.body = append(f.body, fmt.Sprintf(" %s[[\"%s\"]]", name, text)) return f } // CylindricalNode adds a node with cylindrical shape to the flowchart. func (f *Flowchart) CylindricalNode(name, text string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s[(\"%s\")]", name, text)) + f.body = append(f.body, fmt.Sprintf(" %s[(\"%s\")]", name, text)) return f } @@ -59,54 +59,54 @@ func (f *Flowchart) DatabaseNode(name, text string) *Flowchart { // CircleNode adds a node with circle shape to the flowchart. func (f *Flowchart) CircleNode(name, text string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s((\"%s\"))", name, text)) + f.body = append(f.body, fmt.Sprintf(" %s((\"%s\"))", name, text)) return f } // AsymmetricNode adds a node with asymmetric shape to the flowchart. func (f *Flowchart) AsymmetricNode(name, text string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s>\"%s\"]", name, text)) + f.body = append(f.body, fmt.Sprintf(" %s>\"%s\"]", name, text)) return f } // RhombusNode adds a node with rhombus shape to the flowchart. func (f *Flowchart) RhombusNode(name, text string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s{\"%s\"}", name, text)) + f.body = append(f.body, fmt.Sprintf(" %s{\"%s\"}", name, text)) return f } // HexagonNode adds a node with hexagon shape to the flowchart. func (f *Flowchart) HexagonNode(name, text string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s{{\"%s\"}}", name, text)) + f.body = append(f.body, fmt.Sprintf(" %s{{\"%s\"}}", name, text)) return f } // ParallelogramNode adds a node with parallelogram shape to the flowchart. func (f *Flowchart) ParallelogramNode(name, text string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s[/\"%s\"/]", name, text)) + f.body = append(f.body, fmt.Sprintf(" %s[/\"%s\"/]", name, text)) return f } // ParallelogramAltNode adds a node with parallelogram shape to the flowchart. func (f *Flowchart) ParallelogramAltNode(name, text string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s[\\\"%s\"\\]", name, text)) + f.body = append(f.body, fmt.Sprintf(" %s[\\\"%s\"\\]", name, text)) return f } // TrapezoidNode adds a node with trapezoid shape to the flowchart. func (f *Flowchart) TrapezoidNode(name, text string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s[/\"%s\"\\]", name, text)) + f.body = append(f.body, fmt.Sprintf(" %s[/\"%s\"\\]", name, text)) return f } // TrapezoidAltNode adds a node with trapezoid shape to the flowchart. func (f *Flowchart) TrapezoidAltNode(name, text string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s[\\\"%s\"/]", name, text)) + f.body = append(f.body, fmt.Sprintf(" %s[\\\"%s\"/]", name, text)) return f } // DoubleCircleNode adds a node with double circle shape to the flowchart. func (f *Flowchart) DoubleCircleNode(name, text string) *Flowchart { - f.body = append(f.body, fmt.Sprintf(" %s(((\"%s\")))", name, text)) + f.body = append(f.body, fmt.Sprintf(" %s(((\"%s\")))", name, text)) return f } diff --git a/mermaid/piechart/examples_test.go b/mermaid/piechart/examples_test.go new file mode 100644 index 0000000..05abc85 --- /dev/null +++ b/mermaid/piechart/examples_test.go @@ -0,0 +1,42 @@ +//go:build linux || darwin + +package piechart_test + +import ( + "os" + + md "github.com/nao1215/markdown" + "github.com/nao1215/markdown/mermaid/piechart" +) + +// ExamplePieChart skip this test on Windows. +// The newline codes in the comment section where +// the expected values are written are represented as '\n', +// causing failures when testing on Windows. +func ExamplePieChart() { + chart := piechart.NewPieChart( + os.Stdout, + piechart.WithTitle("mermaid pie chart builder"), + piechart.WithShowData(true), + ). + LabelAndIntValue("A", 10). + LabelAndFloatValue("B", 20.1). + LabelAndIntValue("C", 30). + String() + + md.NewMarkdown(os.Stdout). + H2("Pie Chart Diagram"). + CodeBlocks(md.SyntaxHighlightMermaid, chart). + Build() //nolint + + // Output: + // ## Pie Chart Diagram + // ```mermaid + //%%{init: {"pie": {"textPosition": 0.75}, "themeVariables": {"pieOuterStrokeWidth": "5px"}} }%% + //pie showData + // title mermaid pie chart builder + // "A" : 10 + // "B" : 20.100000 + // "C" : 30 + // ``` +} diff --git a/mermaid/sequence/examples_test.go b/mermaid/sequence/examples_test.go new file mode 100644 index 0000000..0f8e2df --- /dev/null +++ b/mermaid/sequence/examples_test.go @@ -0,0 +1,64 @@ +//go:build linux || darwin + +package sequence_test + +import ( + "os" + + md "github.com/nao1215/markdown" + "github.com/nao1215/markdown/mermaid/sequence" +) + +// ExampleSequence skip this test on Windows. +// The newline codes in the comment section where +// the expected values are written are represented as '\n', +// causing failures when testing on Windows. +func ExampleDiagram() { + diagram := sequence.NewDiagram(os.Stdout). + Participant("Sophia"). + Participant("David"). + Participant("Subaru"). + LF(). + SyncRequest("Sophia", "David", "Please wake up Subaru"). + SyncResponse("David", "Sophia", "OK"). + LF(). + LoopStart("until Subaru wake up"). + SyncRequest("David", "Subaru", "Wake up!"). + SyncResponse("Subaru", "David", "zzz"). + SyncRequest("David", "Subaru", "Hey!!!"). + BreakStart("if Subaru wake up"). + SyncResponse("Subaru", "David", "......"). + BreakEnd(). + LoopEnd(). + LF(). + SyncResponse("David", "Sophia", "wake up, wake up"). + String() + + md.NewMarkdown(os.Stdout). + H2("Sequence Diagram"). + CodeBlocks(md.SyntaxHighlightMermaid, diagram). + Build() //nolint + + // Output: + // ## Sequence Diagram + // ```mermaid + // sequenceDiagram + // participant Sophia + // participant David + // participant Subaru + // + // Sophia->>David: Please wake up Subaru + // David-->>Sophia: OK + // + // loop until Subaru wake up + // David->>Subaru: Wake up! + // Subaru-->>David: zzz + // David->>Subaru: Hey!!! + // break if Subaru wake up + // Subaru-->>David: ...... + // end + // end + // + // David-->>Sophia: wake up, wake up + // ``` +}