Skip to content

Commit 3f6eb48

Browse files
committed
feat: add built-in apidocs template
1 parent 313ee51 commit 3f6eb48

File tree

12 files changed

+268
-49
lines changed

12 files changed

+268
-49
lines changed

README.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ apidocgen is a tool for Go to generate apis markdown docs.
55
## Install
66

77
```bash
8-
go install github.com/alovn/apidocgen/@latest
8+
go install github.com/alovn/apidocgen@latest
99
```
1010

1111
## Usage
@@ -21,8 +21,43 @@ Flags:
2121
--dir: Search apis dir, comma separated, default .
2222
--excludes: Exclude directories and files when searching, comma separated
2323
--output: Generate markdown files dir, default ./docs/
24-
--template: Custom template files dir.
24+
--template: Template name or custom template directory, built-in includes markdown and apidocs, default markdown.
2525
--single: If true, generate a single markdown file, default false
2626
```
2727

28+
built-in templates include `markdown`, `apidocs`, default is `markdown`.
29+
2830
run the command in the go module directory.
31+
32+
```bash
33+
cd your-api-service-dir
34+
apidocgen \
35+
--dir=svc-user,common \
36+
--output=./docs
37+
38+
apidocgen \
39+
--dir=svc-user,common \
40+
--template=apidocs \
41+
--output=./docs
42+
43+
44+
```
45+
46+
## Template
47+
48+
The built-in includes `markdown` and `apidocs`.
49+
50+
The built-in template `apidocs` is the template for generate website [apidocs]([email protected]:alovn/apidocs.git).
51+
52+
You can also use the custom template:
53+
54+
```bash
55+
apidocgen \
56+
--dir=svc-user,common \
57+
--template=/Users/xxx/workspace/apidocs/custom-template-direcoty \
58+
--output=./docs
59+
```
60+
61+
## Examples
62+
63+
Some examples and generated markdown docs are here: [apidocgen/examples](https://github.com/alovn/apidocgen/tree/main/examples).

gen/gen.go

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,47 @@ import (
1717
type Gen struct {
1818
c *Config
1919
templateFS fs.FS
20-
defaultFS fs.FS
21-
}
22-
23-
func New(c *Config) *Gen {
24-
return &Gen{
25-
c: c,
26-
}
2720
}
2821

2922
type Config struct {
30-
SearchDir string
31-
OutputDir string
32-
TemplateDir string
33-
ExcludesDir string
34-
IsGenSingleFile bool
23+
SearchDir string
24+
OutputDir string
25+
TemplateName string
26+
CustomTemplateDir string
27+
ExcludesDir string
28+
IsGenSingleFile bool
3529
}
3630

3731
type TemplateConfig struct {
3832
Name string `json:"name"`
3933
Index string `json:"index"`
4034
}
4135

36+
func New(c *Config) *Gen {
37+
var defaultTemplateName = "markdown"
38+
if c.TemplateName == "" {
39+
c.TemplateName = defaultTemplateName
40+
}
41+
if strings.ContainsAny(c.TemplateName, "/\\") {
42+
c.CustomTemplateDir = c.TemplateName
43+
c.TemplateName = ""
44+
}
45+
return &Gen{
46+
c: c,
47+
}
48+
}
49+
4250
func (g *Gen) readTemplate(name string) (s string, err error) {
4351
var bs []byte
44-
if g.templateFS != nil {
45-
if bs, err = fs.ReadFile(g.templateFS, name); err != nil {
46-
return
47-
} else {
48-
s = string(bs)
49-
return
50-
}
52+
if g.templateFS == nil {
53+
err = errors.New("error: templateFS nil")
54+
return
55+
}
56+
if bs, err = fs.ReadFile(g.templateFS, name); err != nil {
57+
return
5158
} else {
52-
if bs, err = fs.ReadFile(g.defaultFS, name); err != nil {
53-
return
54-
} else {
55-
s = string(bs)
56-
return
57-
}
59+
s = string(bs)
60+
return
5861
}
5962
}
6063

@@ -69,29 +72,19 @@ func (g *Gen) Build() error {
6972
}
7073
}
7174
var err error
72-
if g.defaultFS == nil {
73-
if g.defaultFS, err = fs.Sub(defaultTemplateFS, "template"); err != nil {
75+
if g.c.CustomTemplateDir != "" {
76+
g.templateFS = os.DirFS(g.c.CustomTemplateDir)
77+
} else {
78+
if g.templateFS, err = fs.Sub(defaultTemplateFS, fmt.Sprintf("template/%s", g.c.TemplateName)); err != nil {
7479
return err
7580
}
7681
}
7782

78-
if g.templateFS == nil && g.c.TemplateDir != "" {
79-
g.templateFS = os.DirFS(g.c.TemplateDir)
80-
}
8183
var templateSingleIndex string
8284
var templateGroupIndex string
8385
var templateGroupApis string
8486
var templateConfig TemplateConfig
8587

86-
if templateSingleIndex, err = g.readTemplate("single_index.tpl"); err != nil {
87-
return err
88-
}
89-
if templateGroupIndex, err = g.readTemplate("group_index.tpl"); err != nil {
90-
return err
91-
}
92-
if templateGroupApis, err = g.readTemplate("group_apis.tpl"); err != nil {
93-
return err
94-
}
9588
if s, err := g.readTemplate("config.json"); err != nil {
9689
return err
9790
} else {
@@ -104,6 +97,19 @@ func (g *Gen) Build() error {
10497
fmt.Println("use template:", templateConfig.Name)
10598
}
10699

100+
if g.c.IsGenSingleFile {
101+
if templateSingleIndex, err = g.readTemplate("single_index.tpl"); err != nil {
102+
return err
103+
}
104+
} else {
105+
if templateGroupIndex, err = g.readTemplate("group_index.tpl"); err != nil {
106+
return err
107+
}
108+
if templateGroupApis, err = g.readTemplate("group_apis.tpl"); err != nil {
109+
return err
110+
}
111+
}
112+
107113
p := parser.New()
108114
parser.SetExcludedDirsAndFiles(g.c.ExcludesDir)(p)
109115
if err := p.Parse(searchDirs); err != nil {

gen/template/apidocs/config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name":"apidocs",
3+
"index":"_index.md"
4+
}

gen/template/apidocs/group_apis.tpl

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
title: {{.Title}}
3+
navtitle: {{.Title}}
4+
weight: 3
5+
---
6+
7+
8+
## {{.Title}}
9+
{{- if .Description}}
10+
11+
{{.Description}}
12+
{{- end}}
13+
{{range $k,$api := $.Apis}}
14+
{{add $k 1}}. [{{$api.Title}}](#{{add $k 1}}-{{$api.Title}}) {{- if $api.Deprecated}}(Deprecated){{end}}
15+
{{- end}}
16+
17+
## apis
18+
{{- range $k,$v := .Apis}}
19+
20+
### {{add $k 1}}. {{$v.Title}}
21+
22+
{{- if $v.Deprecated}}
23+
24+
___Deprecated___
25+
{{- end}}
26+
27+
{{- if $v.Description}}
28+
29+
{{$v.Description}}
30+
{{- end}}
31+
32+
{{- if $v.Author}}
33+
34+
author: _{{$v.Author}}_
35+
{{- end}}
36+
37+
{{- if $v.Version}}
38+
39+
version: _{{$v.Version}}_
40+
{{- end}}
41+
42+
```text
43+
{{$v.HTTPMethod}} {{$v.FullURL}}
44+
```
45+
{{- if $v.Requests.Parameters}}
46+
47+
__Request__:
48+
49+
parameter|parameterType|dataType|required|validate|example|description
50+
--|--|--|--|--|--|--
51+
{{- range $p:= $v.Requests.Parameters}}
52+
__{{$p.Name}}__|_{{$p.ParameterTypes}}_|{{$p.DataType}}|{{$p.Required}}|{{$p.Validate}}|{{$p.Example}}|{{$p.Description}}
53+
{{- end}}
54+
{{- if $v.Requests.Body}}
55+
56+
_body_:
57+
58+
```{{if eq $v.Accept "json"}}javascript{{else}}{{$v.Accept}}{{end}}
59+
{{$v.Requests.Body}}
60+
```
61+
{{- end}}
62+
{{- end}}
63+
{{- if $v.Responses}}
64+
65+
__Response__:
66+
{{- range $res := $v.Responses}}
67+
68+
```{{if eq $v.Format "json"}}javascript{{else}}{{$v.Format}}{{end}}
69+
//StatusCode: {{$res.StatusCode}} {{$res.Description}}
70+
{{$res.Body}}
71+
```
72+
{{- end}}
73+
74+
---
75+
{{- end}}
76+
{{- end}}

gen/template/apidocs/group_index.tpl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: {{.Title}}
3+
weight: 1
4+
---
5+
6+
## {{.Title}} ({{.Service}})
7+
8+
{{- if .Version}}
9+
10+
version: _@{{.Version}}_
11+
{{- end}}
12+
13+
{{- if .Description}}
14+
15+
{{.Description}}
16+
{{- end}}
17+
{{range $k,$v := .Groups}}
18+
{{add $k 1}}. [{{$v.Title}}](./apis-{{$v.Group}})
19+
{{range $k2,$api := $v.Apis}}
20+
{{add $k 1}}.{{add $k2 1}}. [{{$api.Title}}](./apis-{{$api.Group}}#{{add $k2 1}}-{{$api.Title}}) {{- if $api.Deprecated}}(Deprecated){{end}}
21+
{{end}}{{end}}

gen/template/apidocs/single_index.tpl

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# {{.Title}} {{if .Service}} ({{.Service}}){{end}}
2+
3+
{{- if .Version}}
4+
5+
version: _@{{.Version}}_
6+
{{- end}}
7+
8+
{{- if .Description}}
9+
10+
{{.Description}}
11+
{{- end}}
12+
{{range $k,$v := .Groups}}
13+
{{add $k 1}}. [{{$v.Title}}](#{{add $k 1}}-{{$v.Title}})
14+
{{range $k2,$api := $v.Apis}}
15+
{{add $k 1}}.{{add $k2 1}}. [{{$api.Title}}](#{{add $k 1}}{{add $k2 1}}-{{$api.Title}}) {{- if $api.Deprecated}}(Deprecated){{end}}
16+
{{end}}{{end}}
17+
## apis
18+
{{- range $k,$v := .Groups}}
19+
20+
### {{add $k 1}}. {{$v.Title}}
21+
{{- range $k2,$v := $v.Apis}}
22+
23+
#### {{add $k 1}}.{{add $k2 1}}. {{$v.Title}}
24+
25+
{{- if $v.Deprecated}}
26+
27+
___Deprecated___
28+
{{- end}}
29+
30+
{{- if $v.Description}}
31+
32+
{{$v.Description}}
33+
{{- end}}
34+
35+
{{- if $v.Author}}
36+
37+
author: _{{$v.Author}}_
38+
{{- end}}
39+
40+
{{- if $v.Version}}
41+
42+
version: _{{$v.Version}}_
43+
{{- end}}
44+
45+
```text
46+
{{$v.HTTPMethod}} {{$v.FullURL}}
47+
```
48+
{{- if $v.Requests.Parameters}}
49+
50+
__Request__:
51+
52+
parameter|parameterType|dataType|required|validate|example|description
53+
--|--|--|--|--|--|--
54+
{{- range $p:= $v.Requests.Parameters}}
55+
__{{$p.Name}}__|_{{$p.ParameterTypes}}_|{{$p.DataType}}|{{$p.Required}}|{{$p.Validate}}|{{$p.Example}}|{{$p.Description}}
56+
{{- end}}
57+
{{- if $v.Requests.Body}}
58+
59+
_body_:
60+
61+
```{if eq $v.Accept "json"}}javascript{{else}}{{$v.Accept}}{{end}}
62+
{{$v.Requests.Body}}
63+
```
64+
{{- end}}
65+
{{- end}}
66+
{{- if $v.Responses}}
67+
68+
__Response__:
69+
{{- range $res := $v.Responses}}
70+
71+
```{if eq $v.Format "json"}}javascript{{else}}{{$v.Format}}{{end}}
72+
//StatusCode: {{$res.StatusCode}} {{$res.Description}}
73+
{{$res.Body}}
74+
```
75+
{{- end}}
76+
77+
---
78+
{{- end}}
79+
{{- end}}
80+
{{- end}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"name":"default",
2+
"name":"markdown",
33
"index":"README.md"
44
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)