| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "os" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | |
| 10 | "golang.org/x/net/html" |
| 11 | "golang.org/x/net/html/charset" |
| 12 | "golang.org/x/text/transform" |
| 13 | ) |
| 14 | |
| 15 | var ( |
| 16 | pupIn io.ReadCloser = os.Stdin |
| 17 | pupCharset string = "" |
| 18 | pupMaxPrintLevel int = -1 |
| 19 | pupPreformatted bool = false |
| 20 | pupPrintColor bool = false |
| 21 | pupEscapeHTML bool = true |
| 22 | pupIndentString string = " " |
| 23 | pupDisplayer Displayer = TreeDisplayer{} |
| 24 | ) |
| 25 | |
| 26 | // Parse the html while handling the charset |
| 27 | func ParseHTML(r io.Reader, cs string) (*html.Node, error) { |
| 28 | var err error |
| 29 | if cs == "" { |
| 30 | // attempt to guess the charset of the HTML document |
| 31 | r, err = charset.NewReader(r, "") |
| 32 | if err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | } else { |
| 36 | // let the user specify the charset |
| 37 | e, name := charset.Lookup(cs) |
| 38 | if name == "" { |
| 39 | return nil, fmt.Errorf("'%s' is not a valid charset", cs) |
| 40 | } |
| 41 | r = transform.NewReader(r, e.NewDecoder()) |
| 42 | } |
| 43 | return html.Parse(r) |
| 44 | } |
| 45 | |
| 46 | func PrintHelp(w io.Writer, exitCode int) { |
| 47 | helpString := `Usage |
| 48 | pup [flags] [selectors] [optional display function] |
| 49 | Version |
| 50 | %s |
| 51 | Flags |
| 52 | -c --color print result with color |
| 53 | -f --file file to read from |
| 54 | -h --help display this help |
| 55 | -i --indent number of spaces to use for indent or character |
| 56 | -n --number print number of elements selected |
| 57 | -l --limit restrict number of levels printed |
| 58 | -p --plain don't escape html |
| 59 | --pre preserve preformatted text |
| 60 | --charset specify the charset for pup to use |
| 61 | --version display version |
| 62 | ` |
| 63 | fmt.Fprintf(w, helpString, VERSION) |
| 64 | os.Exit(exitCode) |
| 65 | } |
| 66 | |
| 67 | func ParseArgs() ([]string, error) { |
| 68 | cmds, err := ProcessFlags(os.Args[1:]) |
| 69 | if err != nil { |
| 70 | return []string{}, err |
| 71 | } |
| 72 | return ParseCommands(strings.Join(cmds, " ")) |
| 73 | } |
| 74 | |
| 75 | // Process command arguments and return all non-flags. |
| 76 | func ProcessFlags(cmds []string) (nonFlagCmds []string, err error) { |
| 77 | var i int |
| 78 | defer func() { |
| 79 | if r := recover(); r != nil { |
| 80 | err = fmt.Errorf("Option '%s' requires an argument", cmds[i]) |
| 81 | } |
| 82 | }() |
| 83 | nonFlagCmds = make([]string, len(cmds)) |
| 84 | n := 0 |
| 85 | for i = 0; i < len(cmds); i++ { |
| 86 | cmd := cmds[i] |
| 87 | switch cmd { |
| 88 | case "-c", "--color": |
| 89 | pupPrintColor = true |
| 90 | case "-p", "--plain": |
| 91 | pupEscapeHTML = false |
| 92 | case "--pre": |
| 93 | pupPreformatted = true |
| 94 | case "-f", "--file": |
| 95 | filename := cmds[i+1] |
| 96 | pupIn, err = os.Open(filename) |
| 97 | if err != nil { |
| 98 | fmt.Fprintf(os.Stderr, "%s\n", err.Error()) |
| 99 | os.Exit(2) |
| 100 | } |
| 101 | i++ |
| 102 | case "-h", "--help": |
| 103 | PrintHelp(os.Stdout, 0) |
| 104 | case "-i", "--indent": |
| 105 | indentLevel, err := strconv.Atoi(cmds[i+1]) |
| 106 | if err == nil { |
| 107 | pupIndentString = strings.Repeat(" ", indentLevel) |
| 108 | } else { |
| 109 | pupIndentString = cmds[i+1] |
| 110 | } |
| 111 | i++ |
| 112 | case "-l", "--limit": |
| 113 | pupMaxPrintLevel, err = strconv.Atoi(cmds[i+1]) |
| 114 | if err != nil { |
| 115 | return []string{}, fmt.Errorf("Argument for '%s' must be numeric", cmd) |
| 116 | } |
| 117 | i++ |
| 118 | case "--charset": |
| 119 | pupCharset = cmds[i+1] |
| 120 | i++ |
| 121 | case "--version": |
| 122 | fmt.Println(VERSION) |
| 123 | os.Exit(0) |
| 124 | case "-n", "--number": |
| 125 | pupDisplayer = NumDisplayer{} |
| 126 | default: |
| 127 | if cmd[0] == '-' { |
| 128 | return []string{}, fmt.Errorf("Unrecognized flag '%s'", cmd) |
| 129 | } |
| 130 | nonFlagCmds[n] = cmds[i] |
| 131 | n++ |
| 132 | } |
| 133 | } |
| 134 | return nonFlagCmds[:n], nil |
| 135 | } |
| 136 | |
| 137 | // Split a string with awareness for quoted text and commas |
| 138 | func ParseCommands(cmdString string) ([]string, error) { |
| 139 | cmds := []string{} |
| 140 | last, next, max := 0, 0, len(cmdString) |
| 141 | for { |
| 142 | // if we're at the end of the string, return |
| 143 | if next == max { |
| 144 | if next > last { |
| 145 | cmds = append(cmds, cmdString[last:next]) |
| 146 | } |
| 147 | return cmds, nil |
| 148 | } |
| 149 | // evaluate a rune |
| 150 | c := cmdString[next] |
| 151 | switch c { |
| 152 | case ' ': |
| 153 | if next > last { |
| 154 | cmds = append(cmds, cmdString[last:next]) |
| 155 | } |
| 156 | last = next + 1 |
| 157 | case ',': |
| 158 | if next > last { |
| 159 | cmds = append(cmds, cmdString[last:next]) |
| 160 | } |
| 161 | cmds = append(cmds, ",") |
| 162 | last = next + 1 |
| 163 | case '\'', '"': |
| 164 | // for quotes, consume runes until the quote has ended |
| 165 | quoteChar := c |
| 166 | for { |
| 167 | next++ |
| 168 | if next == max { |
| 169 | return []string{}, fmt.Errorf("Unmatched open quote (%c)", quoteChar) |
| 170 | } |
| 171 | if cmdString[next] == '\\' { |
| 172 | next++ |
| 173 | if next == max { |
| 174 | return []string{}, fmt.Errorf("Unmatched open quote (%c)", quoteChar) |
| 175 | } |
| 176 | } else if cmdString[next] == quoteChar { |
| 177 | break |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | next++ |
| 182 | } |
| 183 | } |