1
0
mirror of https://github.com/ericchiang/pup synced 2024-11-24 08:58:08 +00:00

slices cleaned up

This commit is contained in:
ericchiang 2014-09-18 18:28:02 -04:00
parent 5c76aeeba9
commit 791366455e
3 changed files with 24 additions and 7 deletions

View File

@ -139,7 +139,7 @@ selected for the next round of selection.
Provide one number for a simple index.
```bash
$ pup < robots.html a {0}
$ pup < robots.html a slice{0}
<a id="top">
</a>
```
@ -148,7 +148,7 @@ You can provide an end to limit the number of nodes selected.
```bash
$ # {:3} is the same as {0:3}
$ pup < robots.html a {:3}
$ pup < robots.html a slice{:3}
<a id="top">
</a>
<a href="#mw-navigation">
@ -159,7 +159,24 @@ $ pup < robots.html a {:3}
</a>
```
The `by` index still needs work.
The `by` indexer works particularly well with tables.
```bash
$ curl -s http://www.pro-football-reference.com/boxscores/201402020den.htm | \
pup table#linescore td slice{::6}
<td align="left">
<a href="/teams/sea/2013.htm">
Seattle Seahawks
</a>
(13-3-0)
</td>
<td align="left">
<a href="/teams/den/2013.htm">
Denver Broncos
</a>
(13-3-0)
</td>
```
## Implemented Selectors

View File

@ -12,7 +12,7 @@ import (
"strings"
)
const VERSION string = "0.2.0"
const VERSION string = "0.2.1"
var (
// Flags

View File

@ -269,7 +269,7 @@ func (sel SliceSelector) Select(nodes []*html.Node) []*html.Node {
selected = append(selected, nodes[i])
}
} else {
for i := end - 1; i > 0 && i >= start; i = i + by {
for i := end - 1; i >= 0 && i >= start; i = i + by {
selected = append(selected, nodes[i])
}
}
@ -328,12 +328,12 @@ func parseSliceSelector(s string) (sel SliceSelector, err error) {
func parseSelectorFunc(s string) (Selector, error) {
switch {
case strings.HasPrefix(s, "{"):
case strings.HasPrefix(s, "slice{"):
if !strings.HasSuffix(s, "}") {
return nil, fmt.Errorf(
"slice func must end with a '}'")
}
s = strings.TrimPrefix(s, "{")
s = strings.TrimPrefix(s, "slice{")
s = strings.TrimSuffix(s, "}")
return parseSliceSelector(s)
}