forked from ikeikeikeike/go-sitemap-generator
/
builder_indexurl.go
42 lines (32 loc) · 905 Bytes
/
builder_indexurl.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
package stm
import (
"time"
"github.com/beevik/etree"
)
// NewSitemapIndexURL and NewSitemapURL are almost the same behavior.
func NewSitemapIndexURL(opts *Options, url URL) SitemapURL {
return &sitemapIndexURL{opts: opts, data: url}
}
// sitemapIndexURL and sitemapURL are almost the same behavior.
type sitemapIndexURL struct {
opts *Options
data URL
}
// XML and sitemapIndexURL.XML are almost the same behavior.
func (su *sitemapIndexURL) XML() []byte {
doc := etree.NewDocument()
sitemap := doc.CreateElement("sitemap")
SetBuilderElementValue(sitemap, su.data, "loc")
if _, ok := SetBuilderElementValue(sitemap, su.data, "lastmod"); !ok {
lastmod := sitemap.CreateElement("lastmod")
lastmod.SetText(time.Now().Format(time.RFC3339))
}
if su.opts.pretty {
doc.Indent(2)
}
buf := poolBuffer.Get()
doc.WriteTo(buf)
bytes := buf.Bytes()
poolBuffer.Put(buf)
return bytes
}