-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs.go
53 lines (45 loc) 路 1.09 KB
/
docs.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
package janet
import (
"regexp"
"strings"
)
var (
METHOD_REGEX = regexp.MustCompile("^# doc: (\\w+)$")
)
// Documented provides documentation in the form of a Markdown-formatted
// string. All of the lines following a top-level Markdown header with a title
// that matches a Go method name will be included as the Janet documentation
// for that method.
//
// For example, providing a string that looks like this:
// ```
// # doc: SomeMethod
// This is some documentation
// # doc: SomeMethodB
// This is some other documentation
// ```
// Will result in SomeMethod and SomeMethodB having those docstrings.
type Documented interface {
Documentation() string
}
func parseDocstrings(markdown string) (result map[string]string) {
result = make(map[string]string)
name := ""
docstring := ""
for _, line := range strings.Split(markdown, "\n") {
match := METHOD_REGEX.FindStringSubmatch(line)
if match == nil {
docstring += line + "\n"
continue
}
if len(name) > 0 {
result[name] = docstring
}
name = match[1]
docstring = ""
}
if len(name) > 0 {
result[name] = docstring
}
return
}