-
Notifications
You must be signed in to change notification settings - Fork 1
/
pkg.go
40 lines (33 loc) · 1.11 KB
/
pkg.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
package templates
import (
"text/template"
pgs "github.com/lyft/protoc-gen-star"
pgsgo "github.com/lyft/protoc-gen-star/lang/go"
"github.com/youlu-cn/grpc-gen/protoc-gen-markdown/templates/markdown"
"github.com/youlu-cn/grpc-gen/protoc-gen-markdown/templates/toc"
)
type RegisterFn func(tpl *template.Template, params pgs.Parameters)
type FilePathFn func(f pgs.File, ctx pgsgo.Context, tpl *template.Template) *pgs.FilePath
func Template(params pgs.Parameters) []*template.Template {
return []*template.Template{
makeTemplate("markdown", markdown.Register, params),
}
}
func TOCTemplate(params pgs.Parameters) *template.Template {
return makeTemplate("toc", toc.Register, params)
}
func FilePathFor(tpl *template.Template) FilePathFn {
switch tpl.Name() {
default:
return func(f pgs.File, ctx pgsgo.Context, tpl *template.Template) *pgs.FilePath {
out := ctx.OutputPath(f)
out = pgs.JoinPaths(out.Dir().String(), f.Name().String()+".md")
return &out
}
}
}
func makeTemplate(typ string, fn RegisterFn, params pgs.Parameters) *template.Template {
tpl := template.New(typ)
fn(tpl, params)
return tpl
}