htmltools

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

main.go (702B)


      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"
     11 	"entf.net/htmltools/cmd"
     12 )
     13 
     14 func main() {
     15 	args := os.Args[1:]
     16 	if len(args) == 0 {
     17 		fmt.Println("usage: htmlremove SELECTOR [FILES...]")
     18 		os.Exit(1)
     19 	}
     20 	sel, err := cascadia.Compile(args[0])
     21 	if err != nil {
     22 		fmt.Fprintf(os.Stderr, "selector invalid: %v\n", err)
     23 		os.Exit(1)
     24 	}
     25 	cmd.Main(args[1:], func(doc *html.Node) {
     26 		unwrap(sel, doc)
     27 	})
     28 }
     29 
     30 func unwrap(sel cascadia.Selector, doc *html.Node) {
     31 	for _, n := range sel.MatchAll(doc) {
     32 		if err := htmltools.Unwrap(n); err != nil {
     33 			fmt.Fprintf(os.Stderr, "%v\n", err)
     34 			os.Exit(1)
     35 		}
     36 	}
     37 	html.Render(os.Stdout, doc)
     38 }