-
Notifications
You must be signed in to change notification settings - Fork 3
/
render.go
126 lines (100 loc) · 2.69 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
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
/*
Copyright 2024 The cert-manager Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package render
import (
"embed"
"errors"
"io"
"io/fs"
"os"
"regexp"
"strings"
"text/template"
"github.com/cert-manager/helm-tool/parser"
"github.com/Masterminds/sprig/v3"
)
//go:embed markdown-plain
//go:embed markdown-table
//go:embed markdown-table-vertical
var templates embed.FS
func openTemplate(path string) (fs.File, error) {
file, err := os.Open(path)
if os.IsNotExist(err) {
return templates.Open(path)
}
if err != nil {
return nil, err
}
return file, nil
}
func Render(templateName string, document *parser.Document) (string, error) {
tpl, err := openTemplate(templateName)
if err != nil {
return "", err
}
defer tpl.Close()
templateBytes, err := io.ReadAll(tpl)
if err != nil {
return "", err
}
funcMap := sprig.TxtFuncMap()
funcMap["indentWith"] = func(pad string, v string) string {
return pad + strings.Replace(v, "\n", "\n"+pad, -1)
}
template, err := template.New(templateName).Funcs(funcMap).Parse(string(templateBytes))
if err != nil {
return "", err
}
var sb strings.Builder
if err := template.Execute(&sb, document); err != nil {
return "", err
}
return sb.String(), nil
}
func Inject(path, templateName string, document *parser.Document, headerMatch, footerMatch *regexp.Regexp) error {
// Open the file
file, err := os.OpenFile(path, os.O_RDWR, 0666)
if err != nil {
return err
}
// Read the contents
fileContents, err := io.ReadAll(file)
if err != nil {
return err
}
// Find the start of where to inject
startIdx := headerMatch.FindIndex(fileContents)
if startIdx == nil {
return errors.New("could not find parameters tag")
}
start := startIdx[1]
// Find the end of where to inject
endIdx := footerMatch.FindIndex(fileContents[start:])
end := len(fileContents)
if endIdx != nil {
end = start + endIdx[0]
}
renderedDocument, err := Render(templateName, document)
if err != nil {
return errors.New("could not render documentation from template")
}
header := fileContents[:start]
content := []byte(renderedDocument + "\n")
footer := fileContents[end:]
file.Truncate(0)
file.Seek(0, 0)
file.Write(header)
file.Write(content)
file.Write(footer)
return nil
}