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
|
package main
import (
"encoding/xml"
"fmt"
"net/url"
)
type XMLURLset struct {
XMLName xml.Name `xml:"urlset"`
Xmlns string `xml:"xmlns,attr"`
Comment string `xml:",comment"`
URLs []URL `xml:"url"`
}
type URL struct {
Loc Loc `xml:"loc"`
}
type Loc struct {
Text string `xml:",chardata"`
}
func toSitemap(seen map[url.URL]int, maxDepth, maxURLs int) (string, error) {
var xmlURLs []URL
for u := range seen {
xmlURL := URL{
Loc: Loc{
Text: fmt.Sprintf("%s", &u),
},
}
xmlURLs = append(xmlURLs, xmlURL)
}
set := XMLURLset{
Xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9",
URLs: xmlURLs,
Comment: fmt.Sprintf("max depth: %s, max urls: %s, total entries: %d", toSymbol(maxDepth), toSymbol(maxURLs), len(seen)),
}
output, err := xml.MarshalIndent(&set, "", "\t")
if err != nil {
return "", err
}
withHeader := fmt.Sprintf("%s\n%s", xml.Header, output)
return withHeader, nil
}
func toSymbol(figure int) string {
if figure == 0 {
return "∞"
}
return fmt.Sprintf("%d", figure)
}
|