~ecs/hq

ref: 05e89aaebceb0242aaa43c75277e0b2cc05a11a0 hq/root.go -rw-r--r-- 688 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
package main

import (
	"errors"
	"golang.org/x/net/html"
)

type root struct{}

func (root) Execute(args []string, ins []<-chan Datum, out chan Datum) error {
	defer func() {
		out <- Datum{End: true}
		close(out)
	}()
	if len(args) != 1 {
		return errors.New("usage: root")
	}
	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 c := d.HTML.FirstChild; c != nil; {
			next := c.NextSibling
			if c.Type == html.ElementNode {
				d.HTML.RemoveChild(c)
			}
			c = next
		}
		out <- Datum{HTML: d.HTML}
	}
	return nil
}

func init() {
	Filters["root"] = root{}
}