package main
import (
"errors"
"strings"
"github.com/andybalholm/cascadia"
)
type sel struct{}
func (sel) Execute(args []string, ins []<-chan Datum, out chan Datum) error {
defer func() {
out <- Datum{End: true}
close(out)
}()
if len(args) == 0 {
return errors.New("usage: sel <selector>")
}
s, err := cascadia.Parse(strings.Join(args[1:], " "))
if err != nil {
return err
}
for _, d := Select(ins); d != nil; _, d = Select(ins) {
if d.End {
continue
}
if d.HTML == nil {
return errors.New("type error: expected html, got text")
}
for _, n := range cascadia.QueryAll(d.HTML, s) {
out <- Datum{HTML: n}
}
}
return nil
}
func init() {
Filters["sel"] = sel{}
}