-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_linker.go
36 lines (28 loc) · 937 Bytes
/
custom_linker.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
package importshttp
import (
"fmt"
"strings"
)
// CustomLinker generates a link with a configurable URL template.
type CustomLinker struct {
// Ordering which will be propagated to any generated Link.
Ordering int
// Label which will be propagated to any generated Link.
Label string
// PkgTemplate may use the `{/pkg}` placeholder.
PkgTemplate string
// DirTemplate may use the `{/pkg}` and `{/dir}` placeholders. If empty, PkgTemplate is used.
DirTemplate string
}
func (l CustomLinker) Link(pkg Package) *Link {
link := &Link{
Ordering: l.Ordering,
Label: l.Label,
}
if len(pkg.ImportSubpackage) > 0 && len(l.DirTemplate) > 0 {
link.URL = strings.Replace(strings.Replace(l.DirTemplate, "{/pkg}", fmt.Sprintf("/%s", pkg.Import), 1), "{/dir}", fmt.Sprintf("/%s", pkg.ImportSubpackage), 1)
} else {
link.URL = strings.Replace(l.PkgTemplate, "{/pkg}", fmt.Sprintf("/%s", pkg.Import), 1)
}
return link
}