aboutsummaryrefslogtreecommitdiff
path: root/web/sitemaps/sitemaps.go
blob: 9d2f739a5ebde0a8e85f062f19e61ca640a062e2 (plain)
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
package sitemaps

import (
	"encoding/xml"
	"fmt"
	"strings"
	"time"
)

const (
	xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9"
)

type ChangeFreq string

func (c *ChangeFreq) UnmarshalText(text []byte) error {
	v, ok := map[string]ChangeFreq{
		"always":  Always,
		"hourly":  Hourly,
		"daily":   Daily,
		"weekly":  Weekly,
		"monthly": Monthly,
		"yearly":  Yearly,
		"never":   Never,
	}[strings.ToLower(string(text))]

	if !ok {
		return fmt.Errorf("Unknown change frequency %s", text)
	} else {
		*c = v
	}

	return nil
}

func (c ChangeFreq) MarshalText() ([]byte, error) {
	return []byte(c), nil
}

const (
	Always  ChangeFreq = "always"
	Hourly             = "hourly"
	Daily              = "daily"
	Weekly             = "weekly"
	Monthly            = "monthly"
	Yearly             = "yearly"
	Never              = "never"
)

type SitemapTime time.Time

func (t *SitemapTime) UnmarshalText(text []byte) error {
	p, err := time.Parse(time.RFC3339, string(text))
	if err != nil {
		return err
	}
	*t = SitemapTime(p)
	return nil
}

func (t SitemapTime) MarshalText() ([]byte, error) {
	return []byte(time.Time(t).Format(time.RFC3339)), nil
}

type Sitemap struct {
	XMLName xml.Name
	Urls    []*SiteUrl `xml:"url"`
}

type SiteUrl struct {
	Location        string     `xml:"loc"`
	LastModified    *time.Time `xml:"lastmod,omitempty"`
	ChangeFrequency ChangeFreq `xml:"changefreq,omitempty"`
	Priority        float32    `xml:"priority,omitempty"`
}

func NewSitemap() *Sitemap {
	return &Sitemap{
		XMLName: xml.Name{Local: "urlset", Space: xmlns},
	}
}