-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_sitemap.go
72 lines (63 loc) · 1.68 KB
/
gen_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
package main
import (
"encoding/xml"
"path"
"time"
)
// SiteMapURLSet represents <urlset>
type SiteMapURLSet struct {
XMLName xml.Name `xml:"urlset"`
Ns string `xml:"xmlns,attr"`
URLS []SiteMapURL
}
func makeSiteMapURLSet() *SiteMapURLSet {
return &SiteMapURLSet{
Ns: "http://www.sitemaps.org/schemas/sitemap/0.9",
}
}
// SiteMapURL represents a single url
type SiteMapURL struct {
XMLName xml.Name `xml:"url"`
URL string `xml:"loc"`
LastModified string `xml:"lastmod"`
}
// There are more static pages, but those are the important ones
var staticURLS = []string{
"/book/go-cookbook.html",
"/articles/cbz-cbr-comic-book-reader-viewer-for-windows.html",
"/articles/chm-reader-viewer-for-windows.html",
"/articles/mobi-ebook-reader-viewer-for-windows.html",
"/articles/epub-ebook-reader-viewer-for-windows.html",
"/articles/where-to-get-free-ebooks-epub-mobi.html",
"/software/",
"/documents.html",
}
func genSiteMap(store *Articles, host string) ([]byte, error) {
articles := store.getNotHidden()
urlset := makeSiteMapURLSet()
var urls []SiteMapURL
for _, article := range articles {
pageURL := path.Join(host, article.URL())
uri := SiteMapURL{
URL: pageURL,
LastModified: article.UpdatedOn.Format("2006-01-02"),
}
urls = append(urls, uri)
}
now := time.Now()
for _, staticURL := range staticURLS {
pageURL := path.Join(host, staticURL)
uri := SiteMapURL{
URL: pageURL,
LastModified: now.Format("2006-01-02"),
}
urls = append(urls, uri)
}
urlset.URLS = urls
xmlData, err := xml.MarshalIndent(urlset, "", "")
if err != nil {
return nil, err
}
d := append([]byte(xml.Header), xmlData...)
return d, nil
}