-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsitemap.go
114 lines (95 loc) · 2.21 KB
/
sitemap.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package common
import (
"encoding/xml"
"fmt"
"net/url"
"time"
)
type Frequency int
const (
Always Frequency = 1 << iota
Hourly
Daily
Weekly
Monthly
Yearly
Never
)
func (frequency Frequency) String() string {
switch frequency {
case Always:
return "always"
case Hourly:
return "hourly"
case Daily:
return "daily"
case Weekly:
return "weekly"
case Monthly:
return "monthly"
case Yearly:
return "yearly"
case Never:
return "never"
default:
return ""
}
}
func (frequency Frequency) MarshalXML(encoder *xml.Encoder, start xml.StartElement) error {
freqStr := frequency.String()
return encoder.EncodeElement(&freqStr, start)
}
type Url struct {
Location string `xml:"loc"`
LastModification time.Time `xml:"lastmod,omitempty"`
ChangeFrequency Frequency `xml:"changefreq,omitempty"`
Priority float32 `xml:"priority,omitempty"`
}
type UrlSet struct {
XMLName xml.Name `xml:"http://www.sitemaps.org/schemas/sitemap/0.9 urlset"`
Urls []Url `xml:"url"`
}
func (urlset *UrlSet) AddUrl(newUrl Url) error {
_, err := url.Parse(newUrl.Location)
if newUrl.Priority < 0.0 || newUrl.Priority > 1.0 {
err = fmt.Errorf("Invalid priority %f", newUrl.Priority)
}
if "" == string(newUrl.ChangeFrequency) {
err = fmt.Errorf("Invalid change frequency: %d", newUrl.ChangeFrequency)
}
if nil == err {
urlset.Urls = append(urlset.Urls, newUrl)
}
return err
}
func (urlset UrlSet) String() string {
output, err := xml.MarshalIndent(urlset, "", " ")
if nil != err {
return ""
} else {
return xml.Header + string(output)
}
}
type Sitemap struct {
Location string `xml:"loc"`
LastModification time.Time `xml:"lastmod,omitempty"`
}
type SitemapIndex struct {
XMLName xml.Name `xml:"http://www.sitemaps.org/schemas/sitemap/0.9 sitemapindex"`
Sitemaps []Sitemap `xml:"sitemap"`
}
func (sitemapIdx *SitemapIndex) AddSitemap(sitemap Sitemap) error {
_, err := url.Parse(sitemap.Location)
if nil == err {
sitemapIdx.Sitemaps = append(sitemapIdx.Sitemaps, sitemap)
}
return err
}
func (sitemapIdx SitemapIndex) String() string {
output, err := xml.MarshalIndent(sitemapIdx, "", " ")
if nil != err {
return ""
} else {
return xml.Header + string(output)
}
}