// This function is used to create a new TokenChain from a re-sliced slice // of an existing TokenChain // // Actually it can be used to build a chain from any slice of html.Token func (q *Query) GetTokenChain(tokenChain []html.Token) *tokenutil.Chain { var ( tChain *tokenutil.Chain end bool ) tChain = new(tokenutil.Chain) for _, token := range tokenChain { // we reached the end of our chain if end { break } tChain, end = tChain.Add(token, nil) } return tChain }
// Token search iterates through a TokenChain to find sub-results // in the already builded chain. This may be useful in case you match the outer DIV // of the DOM and still want to get deeper smaller results that may also match your // search-term func (q *Query) TokenSearch(tokenChain *tokenutil.Chain) *searchutil.Result { var success bool for { if tokenChain.Next() == nil { return q.result } // we start with the next sub-chain, as the one passed to this func as arg counts already as match tokenChain = tokenChain.Next() success = q.Match(tokenChain.StartToken()) if success == true { q.result.Add(tokenChain) // search within the new chain again this will be done recursively upon the deepest level of the chain // new results will have their own new chain // TODO: upon here this can actually be done in coroutines as we are working with totally independant data q.TokenSearch(tokenChain) } } }