forked from zhemao/glisp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc.go
69 lines (63 loc) · 1.36 KB
/
doc.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
package glisp
import (
"bytes"
_ "embed"
"strings"
)
var (
//go:embed documentation.txt
documentations string
docMap = make(map[string]string)
)
func QueryBuiltinDoc(funcName string) string {
return docMap[funcName]
}
func init() {
docMap = ParseDoc(documentations)
documentations = ``
}
func ParseDoc(docstr string) map[string]string {
dmap := make(map[string]string)
lines := strings.Split(docstr, "\n")
var foundHeader bool
var header string
docBuf := bytes.Buffer{}
for i := 0; i < len(lines); i++ {
if !foundHeader {
header, foundHeader = getDocHeader(lines[i])
} else {
if h, ok := getDocHeader(lines[i]); ok {
dmap[header] = strings.TrimSpace(docBuf.String())
header = h
docBuf.Reset()
} else {
docBuf.WriteString(lines[i] + "\n")
}
}
}
if header != `` && docBuf.Len() > 0 {
dmap[header] = strings.TrimSpace(docBuf.String())
}
return dmap
}
func getDocHeader(line string) (string, bool) {
lineBytes := []byte(strings.TrimSpace(line))
var start, end int
for i := 0; i < len(lineBytes); i++ {
if lineBytes[i] != '=' {
start = i
break
}
}
for i := len(lineBytes) - 1; i > start; i-- {
if lineBytes[i] != '=' {
end = i
break
}
}
if start >= 5 && end <= len(lineBytes)-5 && start < end {
str := strings.TrimSpace(string(lineBytes[start : end+1]))
return str, str != ``
}
return "", false
}