From 791366455ebaff5267074f5134dd3b28f25d2353 Mon Sep 17 00:00:00 2001 From: ericchiang Date: Thu, 18 Sep 2014 18:28:02 -0400 Subject: [PATCH] slices cleaned up --- README.md | 23 ++++++++++++++++++++--- main.go | 2 +- selector/selector.go | 6 +++--- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 91b112b..b151d53 100644 --- a/README.md +++ b/README.md @@ -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} ``` @@ -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} @@ -159,7 +159,24 @@ $ pup < robots.html a {:3} ``` -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} + + + Seattle Seahawks + + (13-3-0) + + + + Denver Broncos + + (13-3-0) + +``` ## Implemented Selectors diff --git a/main.go b/main.go index 6ac695b..239bf43 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,7 @@ import ( "strings" ) -const VERSION string = "0.2.0" +const VERSION string = "0.2.1" var ( // Flags diff --git a/selector/selector.go b/selector/selector.go index 4efac23..fb9650a 100644 --- a/selector/selector.go +++ b/selector/selector.go @@ -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) }