Add Sitemaps config values handling

This commit is contained in:
Vincent Batoufflet 2014-05-06 17:02:56 +02:00 committed by spf13
parent f8e675d064
commit 2a902bbca6
5 changed files with 49 additions and 13 deletions

View file

@ -109,6 +109,7 @@ func InitializeConfig() {
viper.SetDefault("CanonifyUrls", false) viper.SetDefault("CanonifyUrls", false)
viper.SetDefault("Indexes", map[string]string{"tag": "tags", "category": "categories"}) viper.SetDefault("Indexes", map[string]string{"tag": "tags", "category": "categories"})
viper.SetDefault("Permalinks", make(hugolib.PermalinkOverrides, 0)) viper.SetDefault("Permalinks", make(hugolib.PermalinkOverrides, 0))
viper.SetDefault("Sitemap", hugolib.Sitemap{"", -1})
if hugoCmdV.PersistentFlags().Lookup("build-drafts").Changed { if hugoCmdV.PersistentFlags().Lookup("build-drafts").Changed {
viper.Set("BuildDrafts", Draft) viper.Set("BuildDrafts", Draft)

View file

@ -141,7 +141,7 @@ func renderBytes(content []byte, pagefmt string) []byte {
func newPage(filename string) *Page { func newPage(filename string) *Page {
page := Page{contentType: "", page := Page{contentType: "",
File: File{FileName: filename, Extension: "html"}, File: File{FileName: filename, Extension: "html"},
Node: Node{Keywords: []string{}}, Node: Node{Keywords: []string{}, Sitemap: Sitemap{Priority: -1}},
Params: make(map[string]interface{})} Params: make(map[string]interface{})}
jww.DEBUG.Println("Reading from", page.File.FileName) jww.DEBUG.Println("Reading from", page.File.FileName)
@ -342,6 +342,8 @@ func (page *Page) update(f interface{}) error {
} }
case "status": case "status":
page.Status = cast.ToString(v) page.Status = cast.ToString(v)
case "sitemap":
page.Sitemap = parseSitemap(cast.ToStringMap(v))
default: default:
// If not one of the explicit values, store in Params // If not one of the explicit values, store in Params
switch vv := v.(type) { switch vv := v.(type) {

View file

@ -749,10 +749,33 @@ func (s *Site) RenderSitemap() error {
return nil return nil
} }
sitemapDefault := parseSitemap(viper.GetStringMap("Sitemap"))
optChanged := false optChanged := false
n := s.NewNode() 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 // Force `UglyUrls` option to force `sitemap.xml` file name
switch s.Target.(type) { switch s.Target.(type) {

View file

@ -1,18 +1,28 @@
package hugolib package hugolib
import jww "github.com/spf13/jwalterweatherman" import (
"github.com/spf13/cast"
jww "github.com/spf13/jwalterweatherman"
)
type Sitemap struct { type Sitemap struct {
ChangeFreq string ChangeFreq string
Priority float32 Priority float64
} }
func (s Sitemap) Validate() { func parseSitemap(input map[string]interface{}) Sitemap {
if s.Priority < 0 { sitemap := Sitemap{Priority: -1}
jww.WARN.Printf("Sitemap priority should be greater than 0, found: %f", s.Priority)
s.Priority = 0 for key, value := range input {
} else if s.Priority > 1 { switch key {
jww.WARN.Printf("Sitemap priority should be lesser than 1, found: %f", s.Priority) case "changefreq":
s.Priority = 1 sitemap.ChangeFreq = cast.ToString(value)
case "priority":
sitemap.Priority = cast.ToFloat64(value)
default:
jww.WARN.Printf("Unknown Sitemap field: %s\n", key)
}
} }
return sitemap
} }

View file

@ -70,8 +70,8 @@ func (t *GoHtmlTemplate) EmbedTemplates() {
<url> <url>
<loc>{{ .Permalink }}</loc> <loc>{{ .Permalink }}</loc>
<lastmod>{{ safeHtml ( .Date.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ with .Sitemap.ChangeFreq }} <lastmod>{{ safeHtml ( .Date.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ with .Sitemap.ChangeFreq }}
<changefreq>{{ . }}</changefreq>{{ end }}{{ with .Sitemap.Priority }} <changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
<priority>{{ . }}</priority>{{ end }} <priority>{{ .Sitemap.Priority }}</priority>{{ end }}
</url> </url>
{{ end }} {{ end }}
</urlset>`) </urlset>`)