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("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)

View file

@ -141,7 +141,7 @@ func renderBytes(content []byte, pagefmt string) []byte {
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 @@ func (page *Page) update(f interface{}) error {
}
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) {

View file

@ -749,10 +749,33 @@ func (s *Site) RenderSitemap() error {
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) {

View file

@ -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
}

View file

@ -70,8 +70,8 @@ func (t *GoHtmlTemplate) EmbedTemplates() {
<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>`)