htmltools

Various command line tools to transform HTML documents
git clone git://git.entf.net/htmltools
Log | Files | Refs | README | LICENSE

main.go (603B)


      1 package main
      2 
      3 import (
      4 	"fmt"
      5 	"os"
      6 
      7 	"github.com/andybalholm/cascadia"
      8 	"golang.org/x/net/html"
      9 
     10 	"entf.net/htmltools/cmd"
     11 )
     12 
     13 func main() {
     14 	args := os.Args[1:]
     15 	if len(args) == 0 {
     16 		fmt.Println("usage: htmlremove SELECTOR [FILES...]")
     17 		os.Exit(1)
     18 	}
     19 	sel, err := cascadia.Compile(args[0])
     20 	if err != nil {
     21 		fmt.Fprintf(os.Stderr, "selector invalid: %v\n", err)
     22 		os.Exit(1)
     23 	}
     24 	cmd.Main(args[1:], func(doc *html.Node) {
     25 		remove(sel, doc)
     26 	})
     27 }
     28 
     29 func remove(sel cascadia.Selector, doc *html.Node) {
     30 	for _, n := range sel.MatchAll(doc) {
     31 		n.Parent.RemoveChild(n)
     32 	}
     33 	html.Render(os.Stdout, doc)
     34 }