htmltools

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

main.go (888B)


      1 package main
      2 
      3 import (
      4 	"flag"
      5 	"fmt"
      6 	"os"
      7 	"strings"
      8 
      9 	"entf.net/htmltools"
     10 	"entf.net/htmltools/cmd"
     11 	"golang.org/x/net/html"
     12 )
     13 
     14 func main() {
     15 	var fs string
     16 	flag.StringVar(&fs, "fs", ",", "field seperator")
     17 	flag.Parse()
     18 	args := flag.Args()
     19 	if len(args) == 0 {
     20 		fmt.Println("usage: htmlattr [-fs FIELD_SEPERATOR] ATTRIBUTES [FILES...]")
     21 		os.Exit(1)
     22 	}
     23 	attrs := strings.Split(args[0], fs)
     24 	cmd.Main(args[1:], func(doc *html.Node) {
     25 		body, err := htmltools.Body(doc)
     26 		if err != nil {
     27 			fmt.Fprintf(os.Stderr, "%v\n", err)
     28 			os.Exit(1)
     29 		} else if body == nil {
     30 			fmt.Fprintln(os.Stderr, "Document does not contain a body")
     31 			os.Exit(1)
     32 		}
     33 		values, err := htmltools.Attr(attrs, htmltools.Children(body)...)
     34 		if err != nil {
     35 			fmt.Fprintf(os.Stderr, "%v\n", err)
     36 			os.Exit(1)
     37 		}
     38 		for _, v := range values {
     39 			line := strings.Join(v, fs)
     40 			fmt.Println(line)
     41 		}
     42 	})
     43 }