forked from ribice/glice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta.go
142 lines (126 loc) · 3.29 KB
/
meta.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package glice
import (
"fmt"
"golang.org/x/net/html"
"io"
"strings"
)
var chkSourceDomains = []string{
"go.googlesource.com",
}
var gitHubLikeDomains = []string{
"github.com",
"bitbucket.com",
}
type Meta struct {
Import string `meta:"go-import"`
Source string `meta:"go-source"`
}
func GetMetaFromHTMLReader(doc io.Reader) (meta *Meta, err error) {
var found int8
meta = &Meta{}
tokenizer := html.NewTokenizer(doc)
for {
tt := tokenizer.Next()
if tt == html.ErrorToken {
if tokenizer.Err() != io.EOF {
err = fmt.Errorf("HTML parse error: %w", tokenizer.Err())
}
goto end
}
token := tokenizer.Token()
if token.Type == html.EndTagToken && token.Data == htmlRegion {
goto end
}
if token.Data == htmlTag {
var property, content string
for _, attr := range token.Attr {
switch attr.Key {
case "property", "name":
property = strings.TrimSpace(strings.ToLower(attr.Val))
case "content":
content = strings.TrimSpace(attr.Val)
}
}
switch property {
case importName:
meta.Import = ReplaceWhitespace(content, ' ')
found++
case sourceName:
meta.Source = ReplaceWhitespace(content, ' ')
found++
}
}
if found == 2 {
goto end
}
}
end:
return meta, nil
}
func (m *Meta) ResolveGoSource() (repoURL string, err error) {
// The 3rd field gives access to the repository
// Examples of all fields:
// golang.org/x/crypto https://github.com/golang/crypto/ https://github.com/golang/crypto/tree/master{/dir} https://github.com/golang/crypto/blob/master{/dir}/{file}#L{line}
// gopkg.in/jarcoal/httpmock.v1 _ https://github.com/jarcoal/httpmock/tree/v1.2.0{/dir} https://github.com/jarcoal/httpmock/blob/v1.2.0{/dir}/{file}#L{line}
//
_url := ExtractField(m.Source, 2)
if "_" == _url {
_url = ExtractField(m.Source, 3)
}
domain := StripURLScheme(UpToN(_url, '/', 3))
if !ContainsString(gitHubLikeDomains, domain) {
err = fmt.Errorf("repo domain '%s' not handled yet [Full URL=%s]", domain, _url)
goto end
}
// Get up to but not including the URL path
repoURL = UpToN(_url, '/', 5)
end:
return repoURL, err
}
func (m *Meta) ResolveGoImport(imp string) (repoURL string, err error) {
var msg string
var chkSource bool
prefix := ExtractField(m.Import, 1)
switch {
case len(prefix) < len(imp):
if imp[0:len(prefix)] == prefix {
repoURL = fmt.Sprintf("https://%s", prefix)
err = ErrRequestPrefixInstead
goto end
}
msg = "does not match a subset of"
case len(prefix) > len(imp):
msg = "is longer than"
default:
repoURL = ExtractField(m.Import, 3)
repo := StripURLScheme(repoURL)
if repo == imp {
chkSource = true
}
if ContainsString(chkSourceDomains, UpToN(repo, '/', 1)) {
chkSource = true
}
if chkSource {
// The import and repo are the same. We need to look in <meta name="go-source">
// Example: https://gopkg.in/jarcoal/httpmock.v1?go-get=1
repoURL, err = m.ResolveGoSource()
if err != nil {
err = fmt.Errorf("unable to resolve import '%s'; %w", imp, err)
repoURL = ""
}
goto end
}
// We found the URL in <meta name="go-import">
goto end
}
// Anything but switch-default generates this error:
err = fmt.Errorf("remote data error in <meta name='%s'>; prefix '%s' %s import '%s'",
importName,
prefix,
msg,
imp)
repoURL = ""
end:
return repoURL, err
}