~ecs/hq

d06d853e79bde54fc488d8b23294af965532867c — Eyal Sawady 2 months ago 78ab44c master
Add root filter
1 files changed, 39 insertions(+), 0 deletions(-)

A root.go
A root.go => root.go +39 -0
@@ 0,0 1,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{}
}