-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlink.go
71 lines (61 loc) · 1.69 KB
/
link.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
package main
import (
"bytes"
"go/doc/comment"
"strings"
"text/template"
"go.abhg.dev/doc2go/internal/pathtree"
"go.abhg.dev/doc2go/internal/relative"
)
type docLinker struct {
knownImports map[string]struct{}
templates pathtree.Root[*template.Template]
RelLinkStyle relLinkStyle
}
// LocalPackage marks an import path as a "local" package.
//
// A local package is part of the current documentation generation scope,
// so links to these packages will be relative.
func (rl *docLinker) LocalPackage(importPath string) {
if rl.knownImports == nil {
rl.knownImports = make(map[string]struct{})
}
rl.knownImports[importPath] = struct{}{}
}
// Template specifies a package documentation template
// for packages at this import path and its descendants.
func (rl *docLinker) Template(path string, tmpl *template.Template) {
rl.templates.Set(path, tmpl)
}
func (rl *docLinker) packageDocURL(fromPkg, pkg string) string {
if _, ok := rl.knownImports[pkg]; ok {
return rl.RelLinkStyle.Normalize(relative.Path(fromPkg, pkg))
}
if tmpl, ok := rl.templates.Lookup(pkg); ok {
d := struct{ ImportPath string }{ImportPath: pkg}
var buff bytes.Buffer
if err := tmpl.Execute(&buff, d); err == nil {
return strings.TrimSpace(buff.String())
}
// TODO: log the error
}
return "https://pkg.go.dev/" + pkg
}
func (rl *docLinker) DocLinkURL(fromPkg string, l *comment.DocLink) string {
var sb strings.Builder
if l.ImportPath != "" {
sb.WriteString(rl.packageDocURL(fromPkg, l.ImportPath))
}
if len(l.Recv) > 0 {
sb.WriteRune('#')
sb.WriteString(l.Recv)
sb.WriteRune('.')
}
if len(l.Name) > 0 {
if len(l.Recv) == 0 {
sb.WriteRune('#')
}
sb.WriteString(l.Name)
}
return sb.String()
}