-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiss_reference_parser.go
More file actions
68 lines (55 loc) · 1.7 KB
/
iss_reference_parser.go
File metadata and controls
68 lines (55 loc) · 1.7 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
package main
import (
"io"
"log"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
)
type block struct {
name string
description string
args []argument
}
type argument struct {
name string
description string
typ string
}
func parseIssReference(body io.Reader) (path string, requiredArgs []string, blocks []block) {
doc, err := goquery.NewDocumentFromReader(body)
if err != nil {
log.Fatalf("failed to parse reference body, error: %v", err)
}
headerText := doc.Find("body > h1").Text()
path = regexp.MustCompile(`\/iss\/(.*)`).FindStringSubmatch(headerText)[1]
requiredArgs = parseRequiredArguments(path)
doc.Find("body > dl > dt").Each(func(_ int, blockSelector *goquery.Selection) {
blockArgsSelector := blockSelector.Next()
argsSelector := blockArgsSelector.Find("dl > dt")
var args []argument
argsSelector.Each(func(_ int, argSelector *goquery.Selection) {
argMeta := argSelector.Next()
contents := argMeta.Contents()
typeLabelSelector := argMeta.Find("strong:contains('Type:')")
args = append(args, argument{
name: argSelector.Text(),
description: strings.TrimSpace(argMeta.ChildrenFiltered("pre").Text()),
typ: contents.Get(contents.IndexOfSelection(typeLabelSelector) + 1).Data,
})
})
blocks = append(blocks, block{
name: strings.Split(blockSelector.Text(), " ")[0],
description: strings.TrimSpace(blockArgsSelector.ChildrenFiltered("pre").Text()),
args: args,
})
})
return
}
func parseRequiredArguments(path string) (arguments []string) {
re := regexp.MustCompile(`\[(\w+)\]`)
for _, arg := range re.FindAllStringSubmatch(path, 10) {
arguments = append(arguments, arg[1])
}
return
}