-
Notifications
You must be signed in to change notification settings - Fork 154
/
render.go
94 lines (86 loc) · 3.02 KB
/
render.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
package actions
import (
"fmt"
"html/template"
"path"
"strings"
"github.com/gobuffalo/buffalo/render"
"github.com/gobuffalo/docs/actions/helpers"
"github.com/gobuffalo/docs/search/godoc"
"github.com/gobuffalo/docs/search/vimeo"
"github.com/gobuffalo/packr/v2"
"github.com/gobuffalo/plush/v4"
"github.com/markbates/inflect"
)
var r *render.Engine
var assetBox = packr.New("app:assets", "../public")
func Renderer() *render.Engine {
return r
}
func init() {
r = render.New(render.Options{
HTMLLayout: "application.html",
Helpers: render.Helpers{
"doclink": godoc.DocLinkHelper,
"goDocPkgs": godoc.Pkgs,
"godoc": godoc.Helper,
"h1": helpers.H1,
"note": helpers.Note,
"warning": helpers.Warning,
"sinceVersion": func(version string, opts render.Data, help plush.HelperContext) (template.HTML, error) {
ctx := help.Context.New()
if !strings.HasPrefix(version, "v") {
version = "v" + version
}
var name string
pkg := "github.com/gobuffalo/buffalo"
if n, ok := opts["pkg"].(string); ok {
pkg = n
name = strings.TrimPrefix(n, "github.com/") + " "
}
ctx.Set("name", name)
ctx.Set("version", version)
ctx.Set("pkg", pkg)
s, err := plush.Render(sinceVersion, ctx)
return template.HTML(s), err
},
"vimeo": func(code string) template.HTML {
return template.HTML(fmt.Sprintf(vimeoTmpl, code))
},
"vimeoFromVideo": func(v vimeo.Video) template.HTML {
code := path.Base(v.Link)
return template.HTML(fmt.Sprintf(vimeoTmpl, code))
},
"codeTabs": helpers.CodeTabs,
"faq": helpers.Faq,
"githubRelease": func(version string) template.HTML {
return template.HTML(fmt.Sprintf(githubRelease, version, version))
},
"seoDescription": func(description string, help plush.HelperContext) {
help.Context.Set("metaDescription", description)
},
"seoKeywords": func(keywords []interface{}, help plush.HelperContext) {
kl := make([]string, 0)
for _, k := range keywords {
kl = append(kl, k.(string))
}
help.Context.Set("metaKeywords", strings.Join(kl, ","))
},
"pluralize_uint": func(s string, i uint64) string {
return inflect.PluralizeWithSize(s, int(i))
},
"score": func(f float64) string {
return fmt.Sprintf("%.2f%%", f*100)
},
},
TemplatesBox: packr.New("../templates", "../templates"),
AssetsBox: assetBox,
})
r.Helpers["exampleDir"] = helpers.ExampleDir(r)
r.Helpers["rn"] = releaseNotes{box: r.TemplatesBox}
}
const vimeoTmpl = `<div class="video">
<iframe src="https://player.vimeo.com/video/%s?portrait=0" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>`
const sinceVersion = `<span class="since-version"><a href="https://<%= pkg %>/releases/tag/<%= version %>" target="_blank"><%= raw(t("helpers.since", {"version": version, "name": name})) %></a></span>`
const githubRelease = `<a href="https://github.com/gobuffalo/buffalo/releases/tag/%s" target="_blank" rel="noopener noreferrer">releases/tag/%s</a>`