~ecs/hq

ref: 05e89aaebceb0242aaa43c75277e0b2cc05a11a0 hq/selector.go -rw-r--r-- 707 bytes
05e89aaeEmber Sawady Improve multi error handling 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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{}
}