[enh] add goquery for context specific search in html documents

pull/116/head
Adam Tauber 7 years ago
parent 92b46368ce
commit 7272323841

@ -61,7 +61,6 @@ Keybinding | Description
## TODO
* Response specific filters (xpath, etc..)
* Better navigation
* Autocompletion
* Tests

@ -5,10 +5,12 @@ import (
"errors"
"io"
"github.com/PuerkitoBio/goquery"
"github.com/x86kernel/htmlcolor"
)
type htmlFormatter struct {
parsedBody goquery.Document
TextFormatter
}
@ -28,3 +30,25 @@ func (f *htmlFormatter) Format(writer io.Writer, data []byte) error {
func (f *htmlFormatter) Title() string {
return "[html]"
}
func (f *htmlFormatter) Search(q string, body []byte) ([]string, error) {
if q == "" {
buf := bytes.NewBuffer(make([]byte, 0, len(body)))
err := f.Format(buf, body)
return []string{buf.String()}, err
}
doc, err := goquery.NewDocumentFromReader(bytes.NewBuffer(body))
if err != nil {
return nil, err
}
results := make([]string, 0, 8)
doc.Find(q).Each(func(_ int, s *goquery.Selection) {
htmlResult, err := goquery.OuterHtml(s)
if err == nil {
results = append(results, htmlResult)
}
})
return results, nil
}

Loading…
Cancel
Save