-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
342 lines (306 loc) · 9.06 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package main
import (
"bytes"
"fmt"
"github.com/Masterminds/sprig"
"github.com/linuxsuren/yaml-readme/function"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"html/template"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
)
var logger *log.Logger
type option struct {
pattern string
templateFile string
includeHeader bool
sortBy string
groupBy string
printFunctions bool
printVariables bool
}
func loadMetadata(pattern, groupBy string) (items []map[string]interface{},
groupData map[string][]map[string]interface{}, err error) {
groupData = make(map[string][]map[string]interface{})
// find YAML files
var files []string
var data []byte
if files, err = filepath.Glob(pattern); err == nil {
for _, metaFile := range files {
if data, err = ioutil.ReadFile(metaFile); err != nil {
logger.Printf("failed to read file [%s], error: %v\n", metaFile, err)
continue
}
metaMap := make(map[string]interface{})
if err = yaml.Unmarshal(data, metaMap); err != nil {
logger.Printf("failed to parse file [%s] as a YAML, error: %v\n", metaFile, err)
continue
}
// skip this item if there is a 'ignore' key is true
if val, ok := metaMap["ignore"]; ok {
if ignore, ok := val.(bool); ok && ignore {
continue
}
}
filename := strings.TrimSuffix(filepath.Base(metaFile), filepath.Ext(metaFile))
parentname := filepath.Base(filepath.Dir(metaFile))
metaMap["filename"] = filename
metaMap["parentname"] = parentname
metaMap["fullpath"] = metaFile
if val, ok := metaMap[groupBy]; ok && val != "" {
var strVal string
switch val.(type) {
case string:
strVal = val.(string)
case int:
strVal = strconv.Itoa(val.(int))
}
if _, ok := groupData[strVal]; ok {
groupData[strVal] = append(groupData[strVal], metaMap)
} else {
groupData[strVal] = []map[string]interface{}{
metaMap,
}
}
}
items = append(items, metaMap)
}
}
return
}
func sortMetadata(items []map[string]interface{}, sortByField string) {
descending := true
if strings.HasPrefix(sortByField, "!") {
sortByField = strings.TrimPrefix(sortByField, "!")
descending = false
}
sortBy(items, sortByField, descending)
}
func loadTemplate(templateFile string, includeHeader bool) (readmeTpl string, err error) {
// load readme template
var data []byte
if data, err = ioutil.ReadFile(templateFile); err != nil {
fmt.Printf("failed to load README template, error: %v\n", err)
err = nil
readmeTpl = `|中文名称|英文名称|JD|
|---|---|---|
{{- range $val := .}}
|{{$val.zh}}|{{$val.en}}|{{$val.jd}}|
{{- end}}`
}
if includeHeader {
readmeTpl = fmt.Sprintf("> This file was generated by [%s](%s) via [yaml-readme](https://github.com/LinuxSuRen/yaml-readme), please don't edit it directly!\n\n",
filepath.Base(templateFile), filepath.Base(templateFile))
}
readmeTpl = readmeTpl + string(data)
s, err := regexp.Compile("#!yaml-readme .*\n")
readmeTpl = s.ReplaceAllString(readmeTpl, "")
return
}
func (o *option) runE(cmd *cobra.Command, args []string) (err error) {
logger = log.New(cmd.ErrOrStderr(), "", log.LstdFlags)
if o.printFunctions {
printFunctions(cmd.OutOrStdout())
return
}
if o.printVariables {
printVariables(cmd.OutOrStdout())
return
}
// load metadata from YAML files
var items []map[string]interface{}
var groupData map[string][]map[string]interface{}
if items, groupData, err = loadMetadata(o.pattern, o.groupBy); err != nil {
err = fmt.Errorf("failed to load metadat from %q", o.pattern)
return
}
if o.sortBy != "" {
sortMetadata(items, o.sortBy)
}
// load readme template
var readmeTpl string
if readmeTpl, err = loadTemplate(o.templateFile, o.includeHeader); err != nil {
err = fmt.Errorf("failed to load template file from %q", o.templateFile)
return
}
// render it with grouped data
if o.groupBy != "" {
err = renderTemplate(readmeTpl, groupData, cmd.OutOrStdout())
} else {
err = renderTemplate(readmeTpl, items, cmd.OutOrStdout())
}
return
}
func renderTemplateToString(tplContent string, object interface{}) (output string, err error) {
buf := bytes.NewBuffer([]byte{})
if err = renderTemplate(tplContent, object, buf); err == nil {
output = buf.String()
}
return
}
func renderTemplate(tplContent string, object interface{}, writer io.Writer) (err error) {
var tpl *template.Template
if tpl, err = template.New("readme").
Funcs(getFuncMap(tplContent)).
Funcs(sprig.FuncMap()).Parse(tplContent); err == nil {
err = tpl.Execute(writer, object)
}
return
}
func printVariables(stdout io.Writer) {
_, _ = stdout.Write([]byte(`filename
parentname
fullpath`))
}
func printFunctions(stdout io.Writer) {
funcMap := getFuncMap("")
var funcs []string
for k := range funcMap {
funcs = append(funcs, k)
}
sort.SliceStable(funcs, func(i, j int) bool {
return strings.Compare(funcs[i], funcs[j]) < 0
})
_, _ = stdout.Write([]byte(strings.Join(funcs, "\n")))
}
func getFuncMap(readmeTpl string) template.FuncMap {
return template.FuncMap{
"printHelp": func(cmd string) (output string) {
var err error
var data []byte
if data, err = exec.Command(cmd, "--help").Output(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, "failed to run command", cmd)
} else {
output = fmt.Sprintf(`%s
%s
%s`, "```shell", string(data), "```")
}
return
},
"printToc": func() string {
return generateTOC(readmeTpl)
},
"printContributors": func(owner, repo string) template.HTML {
return template.HTML(function.PrintContributors(owner, repo))
},
"printStarHistory": func(owner, repo string) string {
return printStarHistory(owner, repo)
},
"printVisitorCount": func(id string) string {
return fmt.Sprintf(`![Visitor Count](https://profile-counter.glitch.me/%s/count.svg)`, id)
},
"printPages": func(owner string) string {
return function.PrintPages(owner)
},
"render": dataRender,
"gh": function.GithubUserLink,
"ghs": function.GitHubUsersLink,
"ghEmoji": function.GitHubEmojiLink,
"link": function.Link,
"linkOrEmpty": function.LinkOrEmpty,
"twitterLink": function.TwitterLink,
"youTubeLink": function.YouTubeLink,
"gstatic": function.GStatic,
"ghID": function.GetIDFromGHLink,
"printGHTable": function.PrintUserAsTable,
}
}
func sortBy(items []map[string]interface{}, sortBy string, descending bool) {
sort.SliceStable(items, func(i, j int) (compare bool) {
left, ok := items[i][sortBy].(string)
if !ok {
return false
}
right, ok := items[j][sortBy].(string)
if !ok {
return false
}
compare = strings.Compare(left, right) < 0
if !descending {
compare = !compare
}
return
})
}
func generateTOC(txt string) (toc string) {
items := strings.Split(txt, "\n")
for i := range items {
item := items[i]
var prefix string
var tag string
if strings.HasPrefix(item, "## ") {
tag = strings.TrimPrefix(item, "## ")
prefix = "- "
} else if strings.HasPrefix(item, "### ") {
tag = strings.TrimPrefix(item, "### ")
prefix = " - "
} else {
continue
}
// not support those titles which have whitespaces
tag = strings.TrimSpace(tag)
if len(strings.Split(tag, " ")) > 1 {
continue
}
toc = toc + fmt.Sprintf("%s[%s](#%s)\n", prefix, tag, strings.ToLower(tag))
}
return
}
func printStarHistory(owner, repo string) string {
return fmt.Sprintf(`[![Star History Chart](https://api.star-history.com/svg?repos=%[1]s/%[2]s&type=Date)](https://star-history.com/#%[1]s/%[2]s&Date)`,
owner, repo)
}
func dataRender(data interface{}) string {
switch val := data.(type) {
case bool:
if val {
return ":white_check_mark:"
} else {
return ":x:"
}
case string:
return val
}
return ""
}
func newRootCommand() (cmd *cobra.Command) {
opt := &option{}
cmd = &cobra.Command{
Use: "yaml-readme",
Short: "A helper to generate a README file from Golang-based template",
Long: `A helper to generate a README file from Golang-based template
Some functions rely on the GitHub API, in order to avoid X-RateLimit-Limit errors you can set an environment variable: 'GITHUB_TOKEN'`,
RunE: opt.runE,
}
cmd.SetOut(os.Stdout)
flags := cmd.Flags()
flags.StringVarP(&opt.pattern, "pattern", "p", "items/*.yaml",
"The glob pattern with Golang spec to find files")
flags.StringVarP(&opt.templateFile, "template", "t", "README.tpl",
"The template file which should follow Golang template spec")
flags.BoolVarP(&opt.includeHeader, "include-header", "", true,
"Indicate if include a notice header on the top of the README file")
flags.StringVarP(&opt.sortBy, "sort-by", "", "",
"Sort the array data descending by which field, or sort it ascending with the prefix '!'. For example: --sort-by !year")
flags.StringVarP(&opt.groupBy, "group-by", "", "",
"Group the array data by which field")
flags.BoolVarP(&opt.printFunctions, "print-functions", "", false,
"Print all the functions and exit")
flags.BoolVarP(&opt.printVariables, "print-variables", "", false,
"Print all the variables and exit")
return
}
func main() {
if err := newRootCommand().Execute(); err != nil {
os.Exit(1)
}
}