-
Notifications
You must be signed in to change notification settings - Fork 16
/
sitemap.go
100 lines (85 loc) · 2.58 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
// Package sitemap provides tools for creating XML sitemaps
// and sitemap indexes and writing them to [io.Writer] (such as
// [net/http.ResponseWriter]).
//
// Please see https://www.sitemaps.org/ for description of sitemap contents.
package sitemap
import (
"encoding/xml"
"io"
"time"
"github.com/snabb/diagio"
)
// ChangeFreq specifies change frequency of a [Sitemap] or [SitemapIndex]
// [URL] entry. It is just a string.
type ChangeFreq string
// Feel free to use these constants for [ChangeFreq] (or you can just supply
// a string directly).
const (
Always ChangeFreq = "always"
Hourly ChangeFreq = "hourly"
Daily ChangeFreq = "daily"
Weekly ChangeFreq = "weekly"
Monthly ChangeFreq = "monthly"
Yearly ChangeFreq = "yearly"
Never ChangeFreq = "never"
)
// URL entry in [Sitemap] or [SitemapIndex]. LastMod is a pointer
// to [time.Time] because omitempty does not work otherwise. Loc is the
// only mandatory item. ChangeFreq and Priority must be left empty when
// using with a sitemap index.
type URL struct {
Loc string `xml:"loc"`
LastMod *time.Time `xml:"lastmod,omitempty"`
ChangeFreq ChangeFreq `xml:"changefreq,omitempty"`
Priority float32 `xml:"priority,omitempty"`
}
// Sitemap represents a complete sitemap which can be marshaled to XML.
// New instances must be created with [New] in order to set the xmlns
// attribute correctly. Minify can be set to make the output less human
// readable.
type Sitemap struct {
XMLName xml.Name `xml:"urlset"`
Xmlns string `xml:"xmlns,attr"`
URLs []*URL `xml:"url"`
Minify bool `xml:"-"`
}
// New returns a new [Sitemap].
func New() *Sitemap {
return &Sitemap{
Xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9",
URLs: make([]*URL, 0),
}
}
// Add adds an [URL] to a [Sitemap].
func (s *Sitemap) Add(u *URL) {
s.URLs = append(s.URLs, u)
}
// WriteTo writes XML encoded sitemap to given [io.Writer].
// Implements [io.WriterTo].
func (s *Sitemap) WriteTo(w io.Writer) (n int64, err error) {
cw := diagio.NewCounterWriter(w)
_, err = cw.Write([]byte(xml.Header))
if err != nil {
return cw.Count(), err
}
en := xml.NewEncoder(cw)
if !s.Minify {
en.Indent("", " ")
}
err = en.Encode(s)
if err != nil {
return cw.Count(), err
}
_, err = cw.Write([]byte{'\n'})
return cw.Count(), err
}
var _ io.WriterTo = (*Sitemap)(nil)
// ReadFrom reads and parses an XML encoded sitemap from [io.Reader].
// Implements [io.ReaderFrom].
func (s *Sitemap) ReadFrom(r io.Reader) (n int64, err error) {
de := xml.NewDecoder(r)
err = de.Decode(s)
return de.InputOffset(), err
}
var _ io.ReaderFrom = (*Sitemap)(nil)