1
0
Fork 0
mirror of https://github.com/Luzifer/repo-template.git synced 2024-12-23 04:31:20 +00:00

Add has_topic filter for conditional templates

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2019-04-15 20:39:24 +02:00
parent 4c373ea236
commit 2ac6bd1366
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

View file

@ -4,10 +4,12 @@ import (
"strings" "strings"
"github.com/flosch/pongo2" "github.com/flosch/pongo2"
"github.com/google/go-github/github"
"github.com/gosimple/slug" "github.com/gosimple/slug"
) )
func init() { func init() {
pongo2.RegisterFilter("has_topic", tplRepoHasTopic)
pongo2.RegisterFilter("groovy_save", tplGroovyFileSave) pongo2.RegisterFilter("groovy_save", tplGroovyFileSave)
pongo2.RegisterFilter("slugify", tplSlugify) pongo2.RegisterFilter("slugify", tplSlugify)
} }
@ -19,3 +21,23 @@ func tplSlugify(in, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
func tplGroovyFileSave(in, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) { func tplGroovyFileSave(in, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
return pongo2.AsValue(strings.Replace(in.String(), "-", "_", -1)), nil return pongo2.AsValue(strings.Replace(in.String(), "-", "_", -1)), nil
} }
func tplRepoHasTopic(in, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
repo, ok := in.Interface().(*github.Repository)
if !ok {
return nil, &pongo2.Error{
Sender: "filter:has_topic",
ErrorMsg: "Input was no github.Repository",
}
}
topic := param.String()
for _, t := range repo.Topics {
if t == topic {
return pongo2.AsValue(true), nil
}
}
return pongo2.AsValue(false), nil
}