ref: 2a902bbca632153177c918a4d6eb3379dd6cc344
parent: f8e675d064fb0b14515cdc6c9488b83bfdad0c5b
author: Vincent Batoufflet <[email protected]>
date: Tue May 6 13:02:56 EDT 2014
Add Sitemaps config values handling
--- a/commands/hugo.go
+++ b/commands/hugo.go
@@ -109,6 +109,7 @@
viper.SetDefault("CanonifyUrls", false)
viper.SetDefault("Indexes", map[string]string{"tag": "tags", "category": "categories"})
viper.SetDefault("Permalinks", make(hugolib.PermalinkOverrides, 0))
+ viper.SetDefault("Sitemap", hugolib.Sitemap{"", -1})
if hugoCmdV.PersistentFlags().Lookup("build-drafts").Changed {
viper.Set("BuildDrafts", Draft)
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -141,7 +141,7 @@
func newPage(filename string) *Page {
page := Page{contentType: "",
File: File{FileName: filename, Extension: "html"},
- Node: Node{Keywords: []string{}},
+ Node: Node{Keywords: []string{}, Sitemap: Sitemap{Priority: -1}},
Params: make(map[string]interface{})}
jww.DEBUG.Println("Reading from", page.File.FileName)
@@ -342,6 +342,8 @@
}
case "status":
page.Status = cast.ToString(v)
+ case "sitemap":
+ page.Sitemap = parseSitemap(cast.ToStringMap(v))
default:
// If not one of the explicit values, store in Params
switch vv := v.(type) {
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -749,10 +749,33 @@
return nil
}
+ sitemapDefault := parseSitemap(viper.GetStringMap("Sitemap"))
+
optChanged := false
n := s.NewNode()
- n.Data["Pages"] = s.Pages
+
+ // Prepend homepage to the list of pages
+ pages := make(Pages, 0)
+
+ page := &Page{}
+ page.Site = s.Info
+ page.Url = "/"
+
+ pages = append(pages, page)
+ pages = append(pages, s.Pages...)
+
+ n.Data["Pages"] = pages
+
+ for _, page := range pages {
+ if page.Sitemap.ChangeFreq == "" {
+ page.Sitemap.ChangeFreq = sitemapDefault.ChangeFreq
+ }
+
+ if page.Sitemap.Priority == -1 {
+ page.Sitemap.Priority = sitemapDefault.Priority
+ }
+ }
// Force `UglyUrls` option to force `sitemap.xml` file name
switch s.Target.(type) {
--- a/hugolib/sitemap.go
+++ b/hugolib/sitemap.go
@@ -1,18 +1,28 @@
package hugolib
-import jww "github.com/spf13/jwalterweatherman"
+import (
+ "github.com/spf13/cast"
+ jww "github.com/spf13/jwalterweatherman"
+)
type Sitemap struct {
ChangeFreq string
- Priority float32
+ Priority float64
}
-func (s Sitemap) Validate() {
- if s.Priority < 0 {
- jww.WARN.Printf("Sitemap priority should be greater than 0, found: %f", s.Priority)
- s.Priority = 0
- } else if s.Priority > 1 {
- jww.WARN.Printf("Sitemap priority should be lesser than 1, found: %f", s.Priority)
- s.Priority = 1
+func parseSitemap(input map[string]interface{}) Sitemap {
+ sitemap := Sitemap{Priority: -1}
+
+ for key, value := range input {
+ switch key {
+ case "changefreq":
+ sitemap.ChangeFreq = cast.ToString(value)
+ case "priority":
+ sitemap.Priority = cast.ToFloat64(value)
+ default:
+ jww.WARN.Printf("Unknown Sitemap field: %s\n", key)
+ }
}
+
+ return sitemap
}
--- a/hugolib/template_embedded.go
+++ b/hugolib/template_embedded.go
@@ -70,8 +70,8 @@
<url>
<loc>{{ .Permalink }}</loc>
<lastmod>{{ safeHtml ( .Date.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ with .Sitemap.ChangeFreq }}
- <changefreq>{{ . }}</changefreq>{{ end }}{{ with .Sitemap.Priority }}
- <priority>{{ . }}</priority>{{ end }}
+ <changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
+ <priority>{{ .Sitemap.Priority }}</priority>{{ end }}
</url>
{{ end }}
</urlset>`)