mirror of
https://github.com/ericchiang/pup
synced 2024-11-28 02:48:16 +00:00
attribute function added
This commit is contained in:
parent
1c07121c07
commit
fb2219a584
19
README.md
19
README.md
@ -164,6 +164,24 @@ References
|
|||||||
External links
|
External links
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### `attr{attrkey}`
|
||||||
|
|
||||||
|
Print the values of all attributes with a given key from all selected nodes.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ pup < robots.html a attr{href} | head
|
||||||
|
#mw-navigation
|
||||||
|
#p-search
|
||||||
|
/wiki/MediaWiki:Robots.txt
|
||||||
|
//en.wikipedia.org/robots.txt
|
||||||
|
/wiki/Wikipedia:What_Wikipedia_is_not#NOTHOWTO
|
||||||
|
//en.wikipedia.org/w/index.php?title=Robots_exclusion_standard&action=edit
|
||||||
|
//meta.wikimedia.org/wiki/Help:Transwiki
|
||||||
|
//en.wikiversity.org/wiki/
|
||||||
|
//en.wikibooks.org/wiki/
|
||||||
|
//en.wikivoyage.org/wiki/
|
||||||
|
```
|
||||||
|
|
||||||
## Flags
|
## Flags
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@ -178,6 +196,5 @@ External links
|
|||||||
|
|
||||||
## TODO:
|
## TODO:
|
||||||
|
|
||||||
* Print attribute function `attr{attr1, attr2}`
|
|
||||||
* Print as json function `json{}`
|
* Print as json function `json{}`
|
||||||
* Switch `-n` from a flag to a function
|
* Switch `-n` from a flag to a function
|
||||||
|
@ -28,11 +28,26 @@ func (t TextDisplayer) Display(nodes []*html.Node) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AttrDisplayer struct {
|
||||||
|
Attr string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a AttrDisplayer) Display(nodes []*html.Node) {
|
||||||
|
for _, node := range nodes {
|
||||||
|
attributes := node.Attr
|
||||||
|
for _, attr := range attributes {
|
||||||
|
if attr.Key == a.Attr {
|
||||||
|
fmt.Println(attr.Val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// Display function helpers
|
// Display function helpers
|
||||||
displayMatcher *regexp.Regexp = regexp.MustCompile(`\{[^\}]*\}$`)
|
displayMatcher *regexp.Regexp = regexp.MustCompile(`\{[^\}]*\}$`)
|
||||||
textFuncMatcher = regexp.MustCompile(`^text\{\}$`)
|
textFuncMatcher = regexp.MustCompile(`^text\{\}$`)
|
||||||
attrFuncMatcher = regexp.MustCompile(`^attr\{[^\}]*\}$`)
|
attrFuncMatcher = regexp.MustCompile(`^attr\{([^\}]*)\}$`)
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewDisplayFunc(text string) (Displayer, error) {
|
func NewDisplayFunc(text string) (Displayer, error) {
|
||||||
@ -43,7 +58,12 @@ func NewDisplayFunc(text string) (Displayer, error) {
|
|||||||
case textFuncMatcher.MatchString(text):
|
case textFuncMatcher.MatchString(text):
|
||||||
return TextDisplayer{}, nil
|
return TextDisplayer{}, nil
|
||||||
case attrFuncMatcher.MatchString(text):
|
case attrFuncMatcher.MatchString(text):
|
||||||
return nil, fmt.Errorf("attr")
|
matches := attrFuncMatcher.FindStringSubmatch(text)
|
||||||
|
if len(matches) != 2 {
|
||||||
|
return nil, fmt.Errorf("")
|
||||||
|
} else {
|
||||||
|
return AttrDisplayer{matches[1]}, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("Not a display function")
|
return nil, fmt.Errorf("Not a display function")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user