| 1 | package mainscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src go1.13 fmt/" |
| 5 | "ioscip-go gomod github.com/golang/go/src go1.13 io/" |
| 6 | "osscip-go gomod github.com/golang/go/src go1.13 os/" |
| 7 | "strconvscip-go gomod github.com/golang/go/src go1.13 strconv/" |
| 8 | "stringsscip-go gomod github.com/golang/go/src go1.13 strings/" |
| 9 | |
| 10 | "golang.org/x/net/htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/" |
| 11 | "golang.org/x/net/html/charsetscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html/charset`/" |
| 12 | "golang.org/x/text/transformscip-go gomod golang.org/x/text 0a5a09ee4409 `golang.org/x/text/transform`/" |
| 13 | ) |
| 14 | |
| 15 | var ( |
| 16 | pupInscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/pupIn. ioscip-go gomod github.com/golang/go/src go1.13 io/.ReadCloserscip-go gomod github.com/golang/go/src go1.13 io/ReadCloser# = osscip-go gomod github.com/golang/go/src go1.13 os/.Stdinscip-go gomod github.com/golang/go/src go1.13 os/Stdin. |
| 17 | pupCharsetscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/pupCharset. string = "" |
| 18 | pupMaxPrintLevelscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/pupMaxPrintLevel. int = -1 |
| 19 | pupPreformattedscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/pupPreformatted. bool = false |
| 20 | pupPrintColorscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/pupPrintColor. bool = false |
| 21 | pupEscapeHTMLscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/pupEscapeHTML. bool = true |
| 22 | pupIndentStringscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/pupIndentString. string = " " |
| 23 | pupDisplayerscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/pupDisplayer. Displayerscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/Displayer# = TreeDisplayerscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/TreeDisplayer#{} |
| 24 | ) |
| 25 | |
| 26 | // Parse the html while handling the charset |
| 27 | func ParseHTMLscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseHTML().(r ioscip-go gomod github.com/golang/go/src go1.13 io/.Readerscip-go gomod github.com/golang/go/src go1.13 io/Reader#, cs string) (*htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#, error) { |
| 28 | var err error |
| 29 | if cs == "" { |
| 30 | // attempt to guess the charset of the HTML document |
| 31 | r, err = charsetscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html/charset`/.NewReaderscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html/charset`/NewReader().(r, "") |
| 32 | if err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | } else { |
| 36 | // let the user specify the charset |
| 37 | e, name := charsetscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html/charset`/.Lookupscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html/charset`/Lookup().(cs) |
| 38 | if name == "" { |
| 39 | return nil, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("'%s' is not a valid charset", cs) |
| 40 | } |
| 41 | r = transformscip-go gomod golang.org/x/text 0a5a09ee4409 `golang.org/x/text/transform`/.NewReaderscip-go gomod golang.org/x/text 0a5a09ee4409 `golang.org/x/text/transform`/NewReader().(r, e.NewDecoderscip-go gomod golang.org/x/text 0a5a09ee4409 `golang.org/x/text/encoding`/Encoding#NewDecoder().()) |
| 42 | } |
| 43 | return htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Parsescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Parse().(r) |
| 44 | } |
| 45 | |
| 46 | func PrintHelpscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/PrintHelp().(w ioscip-go gomod github.com/golang/go/src go1.13 io/.Writerscip-go gomod github.com/golang/go/src go1.13 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 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintf().(w, helpString, VERSIONscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/VERSION.) |
| 64 | osscip-go gomod github.com/golang/go/src go1.13 os/.Exitscip-go gomod github.com/golang/go/src go1.13 os/Exit().(exitCode) |
| 65 | } |
| 66 | |
| 67 | func ParseArgsscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseArgs().() ([]string, error) { |
| 68 | cmds, err := ProcessFlagsscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ProcessFlags().(osscip-go gomod github.com/golang/go/src go1.13 os/.Argsscip-go gomod github.com/golang/go/src go1.13 os/Args.[1:]) |
| 69 | if err != nil { |
| 70 | return []string{}, err |
| 71 | } |
| 72 | return ParseCommandsscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseCommands().(stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Joinscip-go gomod github.com/golang/go/src go1.13 strings/Join().(cmds, " ")) |
| 73 | } |
| 74 | |
| 75 | // Process command arguments and return all non-flags. |
| 76 | func ProcessFlagsscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ProcessFlags().(cmds []string) (nonFlagCmds []string, err error) { |
| 77 | var i int |
| 78 | defer func() { |
| 79 | if r := recover(); r != nil { |
| 80 | err = fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 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 | pupPrintColorscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/pupPrintColor. = true |
| 90 | case "-p", "--plain": |
| 91 | pupEscapeHTMLscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/pupEscapeHTML. = false |
| 92 | case "--pre": |
| 93 | pupPreformattedscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/pupPreformatted. = true |
| 94 | case "-f", "--file": |
| 95 | filename := cmds[i+1] |
| 96 | pupInscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/pupIn., err = osscip-go gomod github.com/golang/go/src go1.13 os/.Openscip-go gomod github.com/golang/go/src go1.13 os/Open().(filename) |
| 97 | if err != nil { |
| 98 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintf().(osscip-go gomod github.com/golang/go/src go1.13 os/.Stderrscip-go gomod github.com/golang/go/src go1.13 os/Stderr., "%s\n", err.Error()) |
| 99 | osscip-go gomod github.com/golang/go/src go1.13 os/.Exitscip-go gomod github.com/golang/go/src go1.13 os/Exit().(2) |
| 100 | } |
| 101 | i++ |
| 102 | case "-h", "--help": |
| 103 | PrintHelpscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/PrintHelp().(osscip-go gomod github.com/golang/go/src go1.13 os/.Stdoutscip-go gomod github.com/golang/go/src go1.13 os/Stdout., 0) |
| 104 | case "-i", "--indent": |
| 105 | indentLevel, err := strconvscip-go gomod github.com/golang/go/src go1.13 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.13 strconv/Atoi().(cmds[i+1]) |
| 106 | if err == nil { |
| 107 | pupIndentStringscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/pupIndentString. = stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Repeatscip-go gomod github.com/golang/go/src go1.13 strings/Repeat().(" ", indentLevel) |
| 108 | } else { |
| 109 | pupIndentStringscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/pupIndentString. = cmds[i+1] |
| 110 | } |
| 111 | i++ |
| 112 | case "-l", "--limit": |
| 113 | pupMaxPrintLevelscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/pupMaxPrintLevel., err = strconvscip-go gomod github.com/golang/go/src go1.13 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.13 strconv/Atoi().(cmds[i+1]) |
| 114 | if err != nil { |
| 115 | return []string{}, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Argument for '%s' must be numeric", cmd) |
| 116 | } |
| 117 | i++ |
| 118 | case "--charset": |
| 119 | pupCharsetscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/pupCharset. = cmds[i+1] |
| 120 | i++ |
| 121 | case "--version": |
| 122 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.13 fmt/Println().(VERSIONscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/VERSION.) |
| 123 | osscip-go gomod github.com/golang/go/src go1.13 os/.Exitscip-go gomod github.com/golang/go/src go1.13 os/Exit().(0) |
| 124 | case "-n", "--number": |
| 125 | pupDisplayerscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/pupDisplayer. = NumDisplayerscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/NumDisplayer#{} |
| 126 | default: |
| 127 | if cmd[0] == '-' { |
| 128 | return []string{}, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 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 ParseCommandsscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/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{}, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Unmatched open quote (%c)", quoteChar) |
| 170 | } |
| 171 | if cmdString[next] == '\\' { |
| 172 | next++ |
| 173 | if next == max { |
| 174 | return []string{}, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Unmatched open quote (%c)", quoteChar) |
| 175 | } |
| 176 | } else if cmdString[next] == quoteChar { |
| 177 | break |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | next++ |
| 182 | } |
| 183 | } |