-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
338 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package pagser | ||
|
||
import ( | ||
"github.com/PuerkitoBio/goquery" | ||
) | ||
|
||
// CallFunc write function interface | ||
|
||
// # Define Global Function | ||
// | ||
// func MyFunc(node *goquery.Selection, args ...string) (out interface{}, err error) { | ||
// //Todo | ||
// return "Hello", nil | ||
// } | ||
// | ||
// //Register function | ||
// pagser.RegisterFunc("MyFunc", MyFunc) | ||
// | ||
// //Use function | ||
// type PageData struct{ | ||
// Text string `pagser:"h1->MyFunc()"` | ||
// } | ||
// | ||
// | ||
// # Define Struct Function | ||
// //Use function | ||
// type PageData struct{ | ||
// Text string `pagser:"h1->MyFunc()"` | ||
// } | ||
// | ||
// func (pd PageData) MyFunc(node *goquery.Selection, args ...string) (out interface{}, err error) { | ||
// //Todo | ||
// return "Hello", nil | ||
// } | ||
// | ||
// # Lookup function priority order | ||
// | ||
// struct method -> parent method -> ... -> global | ||
// | ||
// # Implicit convert type | ||
// | ||
// Automatic type conversion, Output result string convert to int, int64, float64... | ||
// | ||
// CallFunc is a define function interface | ||
type CallFunc func(node *goquery.Selection, args ...string) (out interface{}, err error) | ||
|
||
//BuiltinFunctions instance | ||
var builtinFun BuiltinFunctions | ||
|
||
//BuiltinSelections instance | ||
var builtinSel BuiltinSelections | ||
|
||
//builtin functions | ||
var builtinFuncMap = map[string]CallFunc{ | ||
"absHref": builtinFun.AbsHref, | ||
"attr": builtinFun.Attr, | ||
"attrConcat": builtinFun.AttrConcat, | ||
"attrEmpty": builtinFun.AttrEmpty, | ||
"attrSplit": builtinFun.AttrSplit, | ||
"eachAttr": builtinFun.EachAttr, | ||
"eachAttrEmpty": builtinFun.EachAttrEmpty, | ||
"eachHtml": builtinFun.EachHtml, | ||
"eachOutHtml": builtinFun.EachOutHtml, | ||
"eachText": builtinFun.EachText, | ||
"eachTextEmpty": builtinFun.EachTextEmpty, | ||
"eachTextJoin": builtinFun.EachTextJoin, | ||
"eqAndAttr": builtinFun.EqAndAttr, | ||
"eqAndHtml": builtinFun.EqAndHtml, | ||
"eqAndOutHtml": builtinFun.EqAndOutHtml, | ||
"eqAndText": builtinFun.EqAndText, | ||
"html": builtinFun.Html, | ||
"outerHtml": builtinFun.OutHtml, | ||
"text": builtinFun.Text, | ||
"textConcat": builtinFun.TextConcat, | ||
"textEmpty": builtinFun.TextEmpty, | ||
"textSplit": builtinFun.TextSplit, | ||
// selector | ||
"child": builtinSel.Child, | ||
"eq": builtinSel.Eq, | ||
"first": builtinSel.First, | ||
"last": builtinSel.Last, | ||
"next": builtinSel.Next, | ||
"parent": builtinSel.Parent, | ||
"parents": builtinSel.Parents, | ||
"parentsUntil": builtinSel.ParentsUntil, | ||
"prev": builtinSel.Prev, | ||
"siblings": builtinSel.Siblings, | ||
} | ||
|
||
// RegisterFunc register function for parse result | ||
// pagser.RegisterFunc("MyFunc", func(node *goquery.Selection, args ...string) (out interface{}, err error) { | ||
// //Todo | ||
// return "Hello", nil | ||
// }) | ||
func (p *Pagser) RegisterFunc(name string, fn CallFunc) { | ||
p.ctxFuncs[name] = fn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.