added pongo2 & pongo2v3 filter registration methods
This commit is contained in:
parent
6d356928f1
commit
a4c3d7de36
@ -1,8 +1,4 @@
|
||||
# ytid
|
||||
simple Go package to extract a Youtube ID from a passed URL
|
||||
|
||||
## methods
|
||||
GetID(url string) string
|
||||
|
||||
GETIDWithError(url string) (string, error)
|
||||
|
||||
Also for flosch/pongo2 and pongo2v3 filter registration methods
|
21
ytid.go
21
ytid.go
@ -3,6 +3,9 @@ package ytid
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"github.com/flosch/pongo2"
|
||||
p2v3 "gopkg.in/flosch/pongo2.v3"
|
||||
)
|
||||
|
||||
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 {
|
||||
if !regexpYoutubeDomain.MatchString(url) {
|
||||
return ""
|
||||
@ -40,6 +58,3 @@ func GetIDWithError(url string) (string, error) {
|
||||
return "", fmt.Errorf("ytid: no match found")
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user