main.go (716B)
1 package main 2 3 import ( 4 "bytes" 5 "fmt" 6 "os" 7 "strings" 8 9 "github.com/andybalholm/cascadia" 10 "golang.org/x/net/html" 11 12 "entf.net/htmltools/cmd" 13 ) 14 15 func main() { 16 args := os.Args[1:] 17 if len(args) == 0 { 18 fmt.Println("usage: htmlselect SELECTOR [FILES...]") 19 os.Exit(1) 20 } 21 sel, err := cascadia.Compile(args[0]) 22 if err != nil { 23 fmt.Fprintf(os.Stderr, "selector invalid: %v\n", err) 24 os.Exit(1) 25 } 26 cmd.Main(args[1:], func(doc *html.Node) { 27 dosel(sel, doc) 28 }) 29 } 30 31 func dosel(sel cascadia.Selector, doc *html.Node) { 32 for _, n := range sel.MatchAll(doc) { 33 buf := &bytes.Buffer{} 34 html.Render(buf, n) 35 l := buf.String() 36 l = strings.ReplaceAll(l, "\n", " ") 37 l = strings.TrimSpace(l) 38 fmt.Println(l) 39 } 40 }