Skip to content

Commit e027ff6

Browse files
committed
Add markdown format to docgen
1 parent 0f43d1f commit e027ff6

File tree

3 files changed

+149
-1
lines changed

3 files changed

+149
-1
lines changed

docgen/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# DocGen
2+
3+
This package provides documentation generator with JSON or Markdown output.
4+
5+
## Usage
6+
7+
Create a file and put next code into it.
8+
9+
```go
10+
package main
11+
12+
import (
13+
"encoding/json"
14+
"fmt"
15+
16+
"github.com/antonmedv/expr/docgen"
17+
)
18+
19+
func main() {
20+
// TODO: Replace env with your own types.
21+
doc := docgen.CreateDoc(env)
22+
23+
buf, err := json.MarshalIndent(doc, "", " ")
24+
if err != nil {
25+
panic(err)
26+
}
27+
fmt.Println(string(buf))
28+
}
29+
```
30+
31+
Run `go run your_file.go`. Documentation will be printed in JSON format.
32+
33+
## Markdown
34+
35+
To generate markdown documentation:
36+
37+
```go
38+
package main
39+
40+
import "github.com/antonmedv/expr/docgen"
41+
42+
func main() {
43+
// TODO: Replace env with your own types.
44+
doc := docgen.CreateDoc(env)
45+
46+
print(doc.Markdown())
47+
}
48+
```

docgen/docgen_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package docgen_test
22

33
import (
4+
"github.com/stretchr/testify/require"
45
"math"
56
"testing"
67

@@ -136,5 +137,11 @@ func TestCreateDoc_FromMap(t *testing.T) {
136137
},
137138
}
138139

139-
assert.Equal(t, litter.Sdump(expected), litter.Sdump(doc))
140+
require.Equal(t, litter.Sdump(expected), litter.Sdump(doc))
141+
}
142+
143+
func TestContext_Markdown(t *testing.T) {
144+
doc := CreateDoc(&Env{})
145+
md := doc.Markdown()
146+
require.True(t, len(md) > 0)
140147
}

docgen/markdown.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package docgen
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func (c *Context) Markdown() string {
9+
out := `### Variables
10+
| Name | Type |
11+
|------|------|
12+
`
13+
for ident, v := range c.Variables {
14+
if v.Kind == "func" {
15+
continue
16+
}
17+
if v.Kind == "operator" {
18+
continue
19+
}
20+
out += fmt.Sprintf("| %v | %v |\n", ident, link(v))
21+
}
22+
23+
out += `
24+
### Functions
25+
| Name | Return type |
26+
|------|-------------|
27+
`
28+
for ident, v := range c.Variables {
29+
if v.Kind == "func" {
30+
args := make([]string, len(v.Arguments))
31+
for i, arg := range v.Arguments {
32+
args[i] = link(arg)
33+
}
34+
out += fmt.Sprintf("| %v(%v) | %v |\n", ident, strings.Join(args, ", "), link(v.Return))
35+
}
36+
}
37+
38+
out += "\n### Types\n"
39+
for name, t := range c.Types {
40+
out += fmt.Sprintf("#### %v\n", name)
41+
out += fields(t)
42+
out += "\n"
43+
}
44+
45+
return out
46+
}
47+
48+
func link(t *Type) string {
49+
if t == nil {
50+
return "nil"
51+
}
52+
if t.Name != "" {
53+
return fmt.Sprintf("[%v](#%v)", t.Name, t.Name)
54+
}
55+
if t.Kind == "array" {
56+
return fmt.Sprintf("array(%v)", link(t.Type))
57+
}
58+
if t.Kind == "map" {
59+
return fmt.Sprintf("map(%v => %v)", link(t.Key), link(t.Type))
60+
}
61+
return fmt.Sprintf("`%v`", t.Kind)
62+
}
63+
64+
func fields(t *Type) string {
65+
out := ""
66+
foundFields := false
67+
for ident, v := range t.Fields {
68+
if v.Kind != "func" {
69+
if !foundFields {
70+
out += "| Field | Type |\n|---|---|\n"
71+
}
72+
foundFields = true
73+
74+
out += fmt.Sprintf("| %v | %v |\n", ident, link(v))
75+
}
76+
}
77+
foundMethod := false
78+
for ident, v := range t.Fields {
79+
if v.Kind == "func" {
80+
if !foundMethod {
81+
out += "\n| Method | Returns |\n|---|---|\n"
82+
}
83+
foundMethod = true
84+
85+
args := make([]string, len(v.Arguments))
86+
for i, arg := range v.Arguments {
87+
args[i] = link(arg)
88+
}
89+
out += fmt.Sprintf("| %v(%v) | %v |\n", ident, strings.Join(args, ", "), link(v.Return))
90+
}
91+
}
92+
return out
93+
}

0 commit comments

Comments
 (0)