-
Notifications
You must be signed in to change notification settings - Fork 1
/
html.go
70 lines (63 loc) · 1.25 KB
/
html.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package reporter
import (
"html/template"
"io"
"net/url"
"sync"
)
var sitemapHTML = `
<html>
<head></head>
<body>
{{ range $key, $value := . }}
<div>
<h2><div id="{{ $key.Path }}">Page {{ $key }}</div></h2>
<h4>Has assets:</h4>
{{ range $value.Assets }}
<li>{{ . }}</li>
{{ end }}
<h4>Links to:</h4>
{{ range $value.Links }}
<li><a href="#{{ .Path }}">{{ . }}</a></li>
{{ end }}
</div>
{{ end }}
</body>
</html>
`
type pageContent struct {
Links []*url.URL
Assets []string
}
// HTML is a reporter that can output a html sitemap.
type HTML struct {
sitemap map[*url.URL]pageContent
template *template.Template
sync.Mutex
}
// NewHTML creates a new HTML reporter.
func NewHTML() *HTML {
return &HTML{
sitemap: make(map[*url.URL]pageContent),
template: template.Must(template.New("sitemap").Parse(sitemapHTML)),
}
}
// Add links and assets to a URI.
func (r *HTML) Add(uri *url.URL, links []*url.URL, assets []string) {
r.Lock()
defer r.Unlock()
_, ok := r.sitemap[uri]
if ok {
return
}
r.sitemap[uri] = pageContent{
Links: links,
Assets: assets,
}
}
// Report writes HTML to the given writer.
func (r *HTML) Report(w io.Writer) error {
r.Lock()
defer r.Unlock()
return r.template.Execute(w, r.sitemap)
}