/
helper.go
56 lines (48 loc) · 1.09 KB
/
helper.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
package godoc
import (
"bytes"
"html/template"
"strings"
"github.com/gobuffalo/here"
"github.com/gobuffalo/plush/v4"
"github.com/gobuffalo/tags"
"github.com/pkg/errors"
)
func Helper(help plush.HelperContext) (template.HTML, error) {
if !help.HasBlock() {
return "", errors.New("a block is required")
}
bb := &bytes.Buffer{}
for _, pkg := range Pkgs {
// p, err := Get(pkg)
// if err != nil {
// return "", errors.WithStack(err)
// }
ctx := help.Context.New()
ctx.Set("pkg", &Doc{
Info: here.Info{
Name: pkg,
ImportPath: pkg,
},
})
s, err := help.BlockWith(ctx)
if err != nil {
return "", errors.WithStack(err)
}
bb.WriteString(s)
}
return template.HTML(bb.String()), nil
}
const gdURL = "https://godoc.org/"
func DocLinkHelper(pkg string) template.HTML {
pkg = strings.TrimPrefix(pkg, gdURL)
u := gdURL + strings.TrimPrefix(pkg, "*")
t := tags.New("a", tags.Options{
"target": "_blank",
"href": u,
})
c := tags.New("code", tags.Options{})
c.Append(strings.Replace(pkg, "github.com/gobuffalo/", "", -1))
t.Append(c)
return t.HTML()
}