-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippet.go
127 lines (110 loc) · 3.21 KB
/
snippet.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
package snippet
import (
"fmt"
"github.com/pkg/errors"
"regexp"
"strings"
)
type metadata struct {
sourceIndent string
tag string
}
type snippet struct {
meta metadata
content []string
}
func (s *snippet) addLine(line string) {
s.content = append(s.content, line)
}
func (s snippet) String(targetIndent string) string {
var result []string
for _, line := range s.content {
result = append(result, targetIndent+strings.TrimPrefix(line, s.meta.sourceIndent))
}
return strings.Join(result, "\n")
}
type snippets map[string]snippet
var (
errEndNotSpecified = errors.New("END SNIPPET undefined")
errNoParserForExtension = errors.New("no comment prefix defined for file extension")
)
func snippetsFromContent(p parser, content string) (snippets, error) {
ss := snippets{}
inSnippet := false
activeSnippet := snippet{}
for _, line := range strings.Split(content, "\n") {
if p.snippetEnd(line) {
inSnippet = false
ss.addSnippet(activeSnippet)
activeSnippet = snippet{}
}
if inSnippet {
activeSnippet.addLine(line)
}
if m, ok := p.snippetStart(line); ok {
inSnippet = true
activeSnippet.meta = m
}
}
if inSnippet {
return nil, errors.Wrapf(errEndNotSpecified,
"can not extract snippets from file, end with '%s END'", p.commentPrefix)
}
return ss, nil
}
func (s snippets) addSnippet(newSnippets ...snippet) {
for _, snp := range newSnippets {
s[snp.meta.tag] = snp
}
}
func (s snippets) addSnippets(newSnippets snippets) {
for key, value := range newSnippets {
s[key] = value
}
}
type parser struct {
start *regexp.Regexp
end *regexp.Regexp
location *regexp.Regexp
commentPrefix string
}
// snippetStart parses a line, and returns the snippet metadata
// and an "ok value" if the line was the start of a snippet or not
func (p parser) snippetStart(line string) (metadata, bool) {
captures := p.start.FindStringSubmatch(line)
if len(captures) == 3 {
return metadata{
sourceIndent: captures[1],
tag: captures[2],
}, true
}
return metadata{}, false
}
// snippetEnd parses a line, and returns an "ok value" if the line was the end of a snippet or not
func (p parser) snippetEnd(line string) bool {
found := p.end.FindString(line)
return len(found) > 0
}
// snippetLocation parses a line, and returns the indentation, the snippet tag
// and true if the snippet target location is that line
func (p parser) snippetLocation(line string) (string, string, bool) {
captures := p.location.FindStringSubmatch(line)
if len(captures) == 3 {
return captures[1], captures[2], true
}
return "", "", false
}
func newParser(extension string) (parser, error) {
prefix, ok := commentPrefixMap[extension]
if !ok {
return parser{}, errors.Wrapf(errNoParserForExtension, "cannot create parser for file extension %s", extension)
}
startStr := fmt.Sprintf(`^([ \t]*)%s START SNIPPET ([a-zA-Z0-9_-]+)[ \t]*$`, prefix)
endStr := fmt.Sprintf(`^[ \t]*%s END SNIPPET[ \t]*$`, prefix)
locationStr := fmt.Sprintf(`^([ \t]*)%s PUT SNIPPET ([a-zA-Z0-9_-]+)[ \t]*$`, prefix)
return parser{
start: regexp.MustCompile(startStr),
end: regexp.MustCompile(endStr),
location: regexp.MustCompile(locationStr),
commentPrefix: prefix}, nil
}