-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsections.go
More file actions
92 lines (74 loc) · 2.09 KB
/
sections.go
File metadata and controls
92 lines (74 loc) · 2.09 KB
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
package queries
import (
"regexp"
"strings"
"sync"
)
var sectionMarkerRE = regexp.MustCompile(`(?m)^--\s*@(\w+)\s*$`)
type parsedSections struct {
once sync.Once
sections map[string]string
queries map[string]*Query
order []string
}
// Section returns raw content for a @tag section.
func (q *Query) Section(tag string) string {
q.ensureSectionsParsed()
return q.parsedSections.sections[tag]
}
// SectionQuery returns a parsed Query for a @tag section, with its own Args and OrdinalQuery.
func (q *Query) SectionQuery(tag string) *Query {
q.ensureSectionsParsed()
raw := q.parsedSections.sections[tag]
if raw == "" {
return nil
}
if cached := q.parsedSections.queries[tag]; cached != nil {
return cached
}
sq, err := NewQuery(q.Name+"@"+tag, q.Path, raw, q.Metadata)
if err != nil {
return nil
}
q.parsedSections.queries[tag] = sq
return sq
}
// HasSection reports whether the query contains a non-empty @tag section.
func (q *Query) HasSection(tag string) bool {
q.ensureSectionsParsed()
return q.parsedSections.sections[tag] != ""
}
// SectionNames returns @tag section names in order of appearance.
func (q *Query) SectionNames() []string {
q.ensureSectionsParsed()
if len(q.parsedSections.order) <= 1 {
return nil
}
return append([]string(nil), q.parsedSections.order[1:]...)
}
func (q *Query) ensureSectionsParsed() {
q.parsedSections.once.Do(func() {
q.parsedSections.sections, q.parsedSections.order = parseSections(q.Raw)
q.parsedSections.queries = make(map[string]*Query)
})
}
func parseSections(raw string) (map[string]string, []string) {
sections := make(map[string]string)
order := []string{""}
matches := sectionMarkerRE.FindAllStringSubmatchIndex(raw, -1)
if len(matches) == 0 {
sections[""] = strings.TrimSpace(raw)
return sections, order
}
sections[""] = strings.TrimSpace(raw[:matches[0][0]])
for i, m := range matches {
tag := strings.ToLower(raw[m[2]:m[3]])
order = append(order, tag)
end := len(raw)
if i+1 < len(matches) {
end = matches[i+1][0]
}
sections[tag] = strings.TrimSpace(raw[m[1]:end])
}
return sections, order
}