Skip to content

Commit

Permalink
Merge pull request #6 from go-spectest/feat/status-badge
Browse files Browse the repository at this point in the history
Feat generate status badge
  • Loading branch information
nao1215 authored Oct 26, 2023
2 parents 5f70811 + a5a88c6 commit c5796ce
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 3 deletions.
34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ Additionally, complex code that increases the complexity of the library, such as
- [x] text with image
- [x] plain text
- [x] details

Although not in Markdown syntax, there is a method to generate badges; RedBadge(), YellowBadge(), GreenBadge().

## Example
### Basic usage
```go
package main

Expand Down Expand Up @@ -135,10 +138,35 @@ func main() {
If you want to see how it looks in Markdown, please refer to the following link.
- [sample.md](./doc/generated_example.md)

## Generate Markdown using `"go generate ./..."`
### Generate status badge
The markdown package can create red, yellow, and green status badges.
[Code example:](./doc/badge/main.go)
```go
md.NewMarkdown(os.Stdout).
H1("badge example").
RedBadge("red_badge").
YellowBadge("yellow_badge").
GreenBadge("green_badge").
Build()
```

[Output:](./doc/badge/generated.md)
````text
# badge example
![Badge](https://img.shields.io/badge/red_badge-red)
![Badge](https://img.shields.io/badge/yellow_badge-yellow)
![Badge](https://img.shields.io/badge/green_badge-green)
````

Your badge will look like this;
![Badge](https://img.shields.io/badge/red_badge-red)
![Badge](https://img.shields.io/badge/yellow_badge-yellow)
![Badge](https://img.shields.io/badge/green_badge-green)

### Generate Markdown using `"go generate ./..."`
You can generate Markdown using `go generate`. Please define code to generate Markdown first. Then, run `"go generate ./..."` to generate Markdown.

[Code example:](./doc/main.go)
[Code example:](./doc/generate/main.go)
```go
package main

Expand Down Expand Up @@ -168,7 +196,7 @@ Run below command:
go generate ./...
```

[Output:](./doc/generated.md)
[Output:](./doc/generate/generated.md)
````text
# go generate example
This markdown is generated by `go generate`
Expand Down
36 changes: 36 additions & 0 deletions badge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package markdown

import "fmt"

// RedBadge return text with red badge format.
func (m *Markdown) RedBadge(text string) *Markdown {
m.body = append(m.body, fmt.Sprintf("![Badge](https://img.shields.io/badge/%s-red)", text))
return m
}

// RedBadgef return text with red badge format.
func (m *Markdown) RedBadgef(format string, args ...interface{}) *Markdown {
return m.RedBadge(fmt.Sprintf(format, args...))
}

// YellowBadge return text with yellow badge format.
func (m *Markdown) YellowBadge(text string) *Markdown {
m.body = append(m.body, fmt.Sprintf("![Badge](https://img.shields.io/badge/%s-yellow)", text))
return m
}

// YellowBadgef return text with yellow badge format.
func (m *Markdown) YellowBadgef(format string, args ...interface{}) *Markdown {
return m.YellowBadge(fmt.Sprintf(format, args...))
}

// GreenBadge return text with green badge format.
func (m *Markdown) GreenBadge(text string) *Markdown {
m.body = append(m.body, fmt.Sprintf("![Badge](https://img.shields.io/badge/%s-green)", text))
return m
}

// GreenBadgef return text with green badge format.
func (m *Markdown) GreenBadgef(format string, args ...interface{}) *Markdown {
return m.GreenBadge(fmt.Sprintf(format, args...))
}
50 changes: 50 additions & 0 deletions badge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package markdown

import (
"io"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestMarkdown_RedBadgef(t *testing.T) {
t.Parallel()
t.Run("success RedBadgef()", func(t *testing.T) {
t.Parallel()

m := NewMarkdown(io.Discard)
m.RedBadgef("%s", "Hello")
want := []string{"![Badge](https://img.shields.io/badge/Hello-red)"}
got := m.body

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("value is mismatch (-want +got):\n%s", diff)
}
})

t.Run("success YellowBadgef()", func(t *testing.T) {
t.Parallel()

m := NewMarkdown(io.Discard)
m.YellowBadgef("%s", "Hello")
want := []string{"![Badge](https://img.shields.io/badge/Hello-yellow)"}
got := m.body

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("value is mismatch (-want +got):\n%s", diff)
}
})

t.Run("success GreenBadgef()", func(t *testing.T) {
t.Parallel()

m := NewMarkdown(io.Discard)
m.GreenBadgef("%s", "Hello")
want := []string{"![Badge](https://img.shields.io/badge/Hello-green)"}
got := m.body

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("value is mismatch (-want +got):\n%s", diff)
}
})
}
4 changes: 4 additions & 0 deletions doc/badge/generated.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# badge example
![Badge](https://img.shields.io/badge/red_badge-red)
![Badge](https://img.shields.io/badge/yellow_badge-yellow)
![Badge](https://img.shields.io/badge/green_badge-green)
28 changes: 28 additions & 0 deletions doc/badge/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build linux || darwin

// Package main is generating markdown.
package main

import (
"os"

md "github.com/go-spectest/markdown"
)

//go:generate go run main.go

func main() {
f, err := os.Create("generated.md")
if err != nil {
panic(err)
}

if err := md.NewMarkdown(f).
H1("badge example").
RedBadge("red_badge").
YellowBadge("yellow_badge").
GreenBadge("green_badge").
Build(); err != nil {
panic(err)
}
}
File renamed without changes.
File renamed without changes.

0 comments on commit c5796ce

Please sign in to comment.