-
Notifications
You must be signed in to change notification settings - Fork 193
/
comment.go
91 lines (75 loc) · 2.05 KB
/
comment.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
package helm
import (
"strings"
)
const (
PrefixComment = "# --"
)
func ParseComment(commentLines []string) (string, ChartValueDescription) {
var valueKey string
var c ChartValueDescription
var docStartIdx int
// Work around https://github.com/norwoodj/helm-docs/issues/96 by considering only
// the last "group" of comment lines starting with '# --'.
lastIndex := 0
for i, v := range commentLines {
if strings.HasPrefix(v, PrefixComment) {
lastIndex = i
}
}
if lastIndex > 0 {
// If there's a non-zero last index, consider that alone.
return ParseComment(commentLines[lastIndex:])
}
for i := range commentLines {
match := valuesDescriptionRegex.FindStringSubmatch(commentLines[i])
if len(match) < 3 {
continue
}
valueKey = match[1]
c.Description = match[2]
docStartIdx = i
break
}
valueTypeMatch := valueTypeRegex.FindStringSubmatch(c.Description)
if len(valueTypeMatch) > 0 && valueTypeMatch[1] != "" {
c.ValueType = valueTypeMatch[1]
c.Description = valueTypeMatch[2]
}
var isRaw = false
for _, line := range commentLines[docStartIdx+1:] {
rawFlagMatch := rawDescriptionRegex.FindStringSubmatch(line)
defaultCommentMatch := defaultValueRegex.FindStringSubmatch(line)
notationTypeCommentMatch := valueNotationTypeRegex.FindStringSubmatch(line)
sectionCommentMatch := sectionRegex.FindStringSubmatch(line)
if !isRaw && len(rawFlagMatch) == 1 {
isRaw = true
continue
}
if len(defaultCommentMatch) > 1 {
c.Default = defaultCommentMatch[1]
continue
}
if len(notationTypeCommentMatch) > 1 {
c.NotationType = notationTypeCommentMatch[1]
continue
}
if len(sectionCommentMatch) > 1 {
c.Section = sectionCommentMatch[1]
continue
}
commentContinuationMatch := commentContinuationRegex.FindStringSubmatch(line)
if isRaw {
if len(commentContinuationMatch) > 1 {
c.Description += "\n" + commentContinuationMatch[2]
}
continue
} else {
if len(commentContinuationMatch) > 1 {
c.Description += " " + commentContinuationMatch[2]
}
continue
}
}
return valueKey, c
}