-
Notifications
You must be signed in to change notification settings - Fork 0
/
links.go
52 lines (46 loc) · 1.08 KB
/
links.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package web
import (
"fmt"
"net/url"
"os"
"path"
"github.com/gregoryv/find"
"golang.org/x/net/html"
)
type BrokenLink struct {
File string
Ref string
Err error
}
func (l *BrokenLink) String() string {
return fmt.Sprintf("%s -> %s: %s", l.File, l.Ref, l.Err)
}
func CheckLinks(root string, broken chan BrokenLink) {
htmlFiles, _ := find.By(find.NewShellPattern("*.html"), root)
for e := htmlFiles.Front(); e != nil; e = e.Next() {
file, _ := e.Value.(string)
fh, _ := os.Open(file)
defer fh.Close()
doc, _ := html.Parse(fh)
CheckLink(file, path.Dir(file), doc, broken)
}
close(broken)
}
func CheckLink(file, rel string, n *html.Node, broken chan BrokenLink) {
if n.Type == html.ElementNode {
for _, a := range n.Attr {
if a.Key == "href" || a.Key == "src" {
url, _ := url.Parse(a.Val)
switch url.Scheme {
case "file", "":
if _, err := os.Stat(path.Join(rel, url.Path)); err != nil {
broken <- BrokenLink{file, url.Path, err}
}
}
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
CheckLink(file, rel, c, broken)
}
}