-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtemplate.go
135 lines (116 loc) · 2.75 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package cmd
import (
"fmt"
"os"
"strings"
"text/template"
"github.com/b5/outline/lib"
"github.com/spf13/cobra"
)
// backticks don't work with golang string literals, so use "'" as a stand-in & strings.Replace
var mdIndex = strings.Replace(`{{- define "mdFn" }}
#### '{{ .Signature }}'
{{- if ne .Description "" }}
{{ .Description }}
{{- end -}}
{{- if gt (len .Params) 0 }}
**parameters:**
| name | type | description |
|------|------|-------------|
{{ range .Params -}}
| '{{ .Name }}' | '{{ .Type }}' | {{ .Description }} |
{{ end -}}
{{- end -}}
{{- end -}}
{{- range . -}}
# {{ .Name }}
{{ if ne .Description "" }}{{ .Description }}{{ end }}
{{- if gt (len .Functions) 0 }}
## Functions
{{ range .Functions -}}
{{ template "mdFn" . }}
{{ end -}}
{{- end }}
{{ if gt (len .Types) 0 }}
## Types
{{ range .Types -}}
### '{{ .Name }}'
{{ if ne .Description "" }}{{ .Description }}{{ end -}}
{{ if gt (len .Fields) 0 }}
**Fields**
| name | type | description |
|------|------|-------------|
{{ range .Fields -}}
| {{ .Name }} | {{ .Type }} | {{ .Description }} |
{{ end -}}
{{ end -}}
{{ if gt (len .Methods) 0 }}
**Methods**
{{- range .Methods -}}
{{ template "mdFn" . }}
{{ end -}}
{{- if gt (len .Operators) 0 }}
**Operators**
| operator | description |
|----------|-------------|
{{ range .Operators -}}
| {{ .Opr }} | {{ .Description }} |
{{ end }}
{{ end }}
{{ end }}
{{- end -}}
{{- end -}}
{{ end }}`, "'", "`", -1)
// TemplateCmd parses outline documents & executes them against a template
var TemplateCmd = &cobra.Command{
Use: "template",
Aliases: []string{"md"},
Short: "execute outline documents against a template",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
t := template.Must(template.New("mdIndex").Parse(mdIndex))
str, err := cmd.Flags().GetString("template")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if str != "" {
t, err = template.ParseFiles(str)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
var options []lib.Option
shouldSort, err := cmd.Flags().GetBool("sort")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if shouldSort {
options = append(options, lib.AlphaSortFuncs(), lib.AlphaSortTypes())
}
var docs lib.Docs
for _, fp := range args {
f, err := os.Open(fp)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
read, err := lib.Parse(f, options...)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
docs = append(docs, read...)
}
if err := t.Execute(os.Stdout, docs); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
},
}
func init() {
TemplateCmd.Flags().StringP("template", "t", "", "template file to load. overrides preset")
TemplateCmd.Flags().Bool("sort", false, "alpha-sort fields & outline documents")
}