Merge from develop
This commit is contained in:
commit
2b58869a04
3 files changed
+22
-8
No files matched your search
@@ -31,7 +31,7 @@ vet:
|
|||||||
go vet ./src/...
|
go vet ./src/...
|
||||||
|
|
||||||
test:
|
test:
|
||||||
go test ./src/... | ./util/testfilter.sh
|
go test -v ./src/... | ./util/testfilter.sh
|
||||||
|
|
||||||
autotest:
|
autotest:
|
||||||
fswatch -or ./src | \
|
fswatch -or ./src | \
|
||||||
|
|||||||
+15
-5
@@ -3,19 +3,29 @@ package main
|
|||||||
import (
|
import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// a regexp which captures src/href/xhref="..." from text
|
// a regexp which captures src/href/xhref="..." from text
|
||||||
var reURL = regexp.MustCompile(`(src|href|xhref)\s*=\s*"([^"]+)"`) // TODO make RFC nice
|
var reURL = regexp.MustCompile(`(?i)(src|href|xhref)\s*=\s*"([^"]+)"`) // TODO make RFC compliant
|
||||||
|
var reTitle = regexp.MustCompile(`(?i)<\s*title\s*>([^<]*)<\s*\/\s*title`)
|
||||||
|
|
||||||
// parse takes a string and attempts to parse any html links out of it,
|
// parse takes a string and attempts to parse any html title and all links out of it,
|
||||||
// and returns a sorted slice of the captures found
|
// and returns a sorted slice of the captures found
|
||||||
func parse(s string) ([]string, error) {
|
func parse(s string) (string, []string, error) {
|
||||||
// find all of the matches (submatch = include capture groups)
|
// find the title, or default to empty
|
||||||
|
title := ""
|
||||||
|
titleMatches := reTitle.FindStringSubmatch(s)
|
||||||
|
if titleMatches != nil {
|
||||||
|
// we found a title, replace our default title with the result
|
||||||
|
title = strings.TrimSpace(titleMatches[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
// find all of the URL matches (submatch = include capture groups)
|
||||||
results := []string{}
|
results := []string{}
|
||||||
for _, match := range reURL.FindAllStringSubmatch(s, -1) {
|
for _, match := range reURL.FindAllStringSubmatch(s, -1) {
|
||||||
results = append(results, match[2])
|
results = append(results, match[2])
|
||||||
}
|
}
|
||||||
sort.Strings(results)
|
sort.Strings(results)
|
||||||
return results, nil
|
return title, results, nil
|
||||||
}
|
}
|
||||||
@@ -4,20 +4,24 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestSimpleParse verifies that we can extract URLs (etc.) from a known document
|
// TestSimpleParse verifies that we can extract the page Title and all URLs (etc.) from a known document
|
||||||
func TestSimpleParse(t *testing.T) {
|
func TestSimpleParse(t *testing.T) {
|
||||||
doc := `<html>
|
doc := `<html>
|
||||||
<head>
|
<head>
|
||||||
|
<TITle> Test Page </title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<img src="/assets/image.png"/>
|
<img src="/assets/image.png"/>
|
||||||
<a href="/about.html">
|
<a href="/about.html">
|
||||||
</body>
|
</body>
|
||||||
</html>`
|
</html>`
|
||||||
matches, err := parse(doc)
|
title, matches, err := parse(doc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
if title != "Test Page" {
|
||||||
|
t.Error("got wrong title")
|
||||||
|
}
|
||||||
if len(matches) != 2 {
|
if len(matches) != 2 {
|
||||||
t.Error("invalid number of matches in parse")
|
t.Error("invalid number of matches in parse")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user