added pongo2 & pongo2v3 filter registration methods

This commit is contained in:
Darko Luketic 2019-07-01 16:05:41 +02:00
parent 6d356928f1
commit a4c3d7de36
2 changed files with 19 additions and 8 deletions

View File

@ -1,8 +1,4 @@
# ytid # ytid
simple Go package to extract a Youtube ID from a passed URL simple Go package to extract a Youtube ID from a passed URL
## methods Also for flosch/pongo2 and pongo2v3 filter registration methods
GetID(url string) string
GETIDWithError(url string) (string, error)

21
ytid.go
View File

@ -3,6 +3,9 @@ package ytid
import ( import (
"fmt" "fmt"
"regexp" "regexp"
"github.com/flosch/pongo2"
p2v3 "gopkg.in/flosch/pongo2.v3"
) )
var ( var (
@ -16,6 +19,21 @@ var (
} }
) )
func RegisterPongo2V3() {
p2v3.RegisterFilter("ytid", filterYtid2v3)
}
func filterYtid2v3(in *p2v3.Value, param *p2v3.Value) (*p2v3.Value, *p2v3.Error) {
return p2v3.AsValue(GetID(in.String())), nil
}
func RegisterPongo() {
pongo2.RegisterFilter("ytid", filterYtid)
}
func filterYtid(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
return pongo2.AsValue(GetID(in.String())), nil
}
func GetID(url string) string { func GetID(url string) string {
if !regexpYoutubeDomain.MatchString(url) { if !regexpYoutubeDomain.MatchString(url) {
return "" return ""
@ -40,6 +58,3 @@ func GetIDWithError(url string) (string, error) {
return "", fmt.Errorf("ytid: no match found") return "", fmt.Errorf("ytid: no match found")
} }
/*
*/