-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
74 lines (59 loc) · 1.68 KB
/
render.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
package docs
import (
"bytes"
_ "embed"
"sort"
"strings"
"text/template"
"github.com/cidverse/cid/pkg/core/catalog"
)
//go:embed templates/workflow.gohtml
var tplWorkflow string
//go:embed templates/action.gohtml
var tplAction string
//go:embed templates/action-index.gohtml
var tplActionIndex string
func GenerateWorkflow(payload catalog.Workflow) (string, error) {
out, err := render(tplWorkflow, payload)
return out, err
}
func GenerateActionIndex(payload []catalog.Action) (string, error) {
// group actions by category
categories := make(map[string][]catalog.Action)
for _, action := range payload {
categories[action.Category] = append(categories[action.Category], action)
}
// sort the actions in each category by name
for _, actions := range categories {
sort.Slice(actions, func(i, j int) bool {
return actions[i].Name < actions[j].Name
})
}
// template context
data := make(map[string]interface{})
data["Categories"] = categories
data["Actions"] = payload
out, err := render(tplActionIndex, data)
return out, err
}
func GenerateAction(payload catalog.Action) (string, error) {
for i := range payload.Access.Env {
payload.Access.Env[i].Description = strings.Trim(payload.Access.Env[i].Description, "\n")
}
out, err := render(tplAction, payload)
return out, err
}
func render(textTemplate string, payload interface{}) (string, error) {
// Create a new template with the given string
t, err := template.New("doc").Parse(textTemplate)
if err != nil {
return "", err
}
// Execute the template with the Person object as input
var output bytes.Buffer
err = t.Execute(&output, payload)
if err != nil {
return "", err
}
return output.String(), nil
}