mirror of
https://github.com/Luzifer/repo-template.git
synced 2024-12-22 20:21:19 +00:00
Add has_topic filter for conditional templates
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
4c373ea236
commit
2ac6bd1366
1 changed files with 22 additions and 0 deletions
22
template.go
22
template.go
|
@ -4,10 +4,12 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/flosch/pongo2"
|
||||
"github.com/google/go-github/github"
|
||||
"github.com/gosimple/slug"
|
||||
)
|
||||
|
||||
func init() {
|
||||
pongo2.RegisterFilter("has_topic", tplRepoHasTopic)
|
||||
pongo2.RegisterFilter("groovy_save", tplGroovyFileSave)
|
||||
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) {
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue