aboutsummaryrefslogtreecommitdiff
path: root/web
diff options
context:
space:
mode:
authorMike Crute <mike@crute.us>2022-11-30 18:19:47 -0800
committerMike Crute <mike@crute.us>2022-11-30 18:19:47 -0800
commit640fa503c772a2412f3031ad8899eef6133344f0 (patch)
tree3d4100d6acb576dc193930ca988f2f923f3a0e27 /web
parent07034d66f08a0e205868c8ff09f4360dbd758854 (diff)
downloadgolib-640fa503c772a2412f3031ad8899eef6133344f0.tar.bz2
golib-640fa503c772a2412f3031ad8899eef6133344f0.tar.xz
golib-640fa503c772a2412f3031ad8899eef6133344f0.zip
Add web/sitemaps packagev0.5.0
Diffstat (limited to 'web')
-rw-r--r--web/sitemaps/sitemaps.go82
1 files changed, 82 insertions, 0 deletions
diff --git a/web/sitemaps/sitemaps.go b/web/sitemaps/sitemaps.go
new file mode 100644
index 0000000..bf2bb44
--- /dev/null
+++ b/web/sitemaps/sitemaps.go
@@ -0,0 +1,82 @@
1package sitemaps
2
3import (
4 "encoding/xml"
5 "fmt"
6 "strings"
7 "time"
8)
9
10const (
11 xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9"
12)
13
14type ChangeFreq string
15
16func (c *ChangeFreq) UnmarshalText(text []byte) error {
17 v, ok := map[string]ChangeFreq{
18 "always": Always,
19 "hourly": Hourly,
20 "daily": Daily,
21 "weekly": Weekly,
22 "monthly": Monthly,
23 "yearly": Yearly,
24 "never": Never,
25 }[strings.ToLower(string(text))]
26
27 if !ok {
28 return fmt.Errorf("Unknown change frequency %s", text)
29 } else {
30 *c = v
31 }
32
33 return nil
34}
35
36func (c ChangeFreq) MarshalText() ([]byte, error) {
37 return []byte(c), nil
38}
39
40const (
41 Always ChangeFreq = "always"
42 Hourly = "hourly"
43 Daily = "daily"
44 Weekly = "weekly"
45 Monthly = "monthly"
46 Yearly = "yearly"
47 Never = "never"
48)
49
50type SitemapTime time.Time
51
52func (t *SitemapTime) UnmarshalText(text []byte) error {
53 p, err := time.Parse(time.RFC3339, string(text))
54 if err != nil {
55 return err
56 }
57 *t = SitemapTime(p)
58 return nil
59}
60
61func (t SitemapTime) MarshalText() ([]byte, error) {
62 return []byte(time.Time(t).Format(time.RFC3339)), nil
63}
64
65type Sitemap struct {
66 XMLName xml.Name
67 Urls []*SiteUrl `xml:"url"`
68}
69
70type SiteUrl struct {
71 Location string `xml:"loc"`
72 LastModified *time.Time `xml:"lastmod,omitempty"`
73 ChangeFrequency ChangeFreq `xml:"changefreq,omitempty"`
74 Priority float32 `xml:"priority,omitempty"`
75}
76
77func NewSitemap() *Sitemap {
78 return &Sitemap{
79 XMLName: xml.Name{Local: "urlset", Space: xmlns},
80 Urls: []*SiteUrl{},
81 }
82}