mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-29 06:42:08 -05:00
Add base Sitemap support
This commit is contained in:
parent
179225449c
commit
f8e675d064
5 changed files with 71 additions and 1 deletions
|
@ -48,7 +48,7 @@ Complete documentation is available at http://hugo.spf13.com`,
|
||||||
}
|
}
|
||||||
var hugoCmdV *cobra.Command
|
var hugoCmdV *cobra.Command
|
||||||
|
|
||||||
var BuildWatch, Draft, UglyUrls, Verbose, Logging, VerboseLog, DisableRSS bool
|
var BuildWatch, Draft, UglyUrls, Verbose, Logging, VerboseLog, DisableRSS, DisableSitemap bool
|
||||||
var Source, Destination, Theme, BaseUrl, CfgFile, LogFile string
|
var Source, Destination, Theme, BaseUrl, CfgFile, LogFile string
|
||||||
|
|
||||||
func Execute() {
|
func Execute() {
|
||||||
|
@ -68,6 +68,7 @@ func AddCommands() {
|
||||||
func init() {
|
func init() {
|
||||||
HugoCmd.PersistentFlags().BoolVarP(&Draft, "build-drafts", "D", false, "include content marked as draft")
|
HugoCmd.PersistentFlags().BoolVarP(&Draft, "build-drafts", "D", false, "include content marked as draft")
|
||||||
HugoCmd.PersistentFlags().BoolVar(&DisableRSS, "disableRSS", false, "Do not build RSS files")
|
HugoCmd.PersistentFlags().BoolVar(&DisableRSS, "disableRSS", false, "Do not build RSS files")
|
||||||
|
HugoCmd.PersistentFlags().BoolVar(&DisableSitemap, "disableSitemap", false, "Do not build Sitemap file")
|
||||||
HugoCmd.PersistentFlags().StringVarP(&Source, "source", "s", "", "filesystem path to read files relative from")
|
HugoCmd.PersistentFlags().StringVarP(&Source, "source", "s", "", "filesystem path to read files relative from")
|
||||||
HugoCmd.PersistentFlags().StringVarP(&Destination, "destination", "d", "", "filesystem path to write files to")
|
HugoCmd.PersistentFlags().StringVarP(&Destination, "destination", "d", "", "filesystem path to write files to")
|
||||||
HugoCmd.PersistentFlags().StringVarP(&Theme, "theme", "t", "", "theme to use (located in /themes/THEMENAME/)")
|
HugoCmd.PersistentFlags().StringVarP(&Theme, "theme", "t", "", "theme to use (located in /themes/THEMENAME/)")
|
||||||
|
@ -95,6 +96,7 @@ func InitializeConfig() {
|
||||||
|
|
||||||
viper.SetDefault("MetadataFormat", "toml")
|
viper.SetDefault("MetadataFormat", "toml")
|
||||||
viper.SetDefault("DisableRSS", false)
|
viper.SetDefault("DisableRSS", false)
|
||||||
|
viper.SetDefault("DisableSitemap", false)
|
||||||
viper.SetDefault("ContentDir", "content")
|
viper.SetDefault("ContentDir", "content")
|
||||||
viper.SetDefault("LayoutDir", "layouts")
|
viper.SetDefault("LayoutDir", "layouts")
|
||||||
viper.SetDefault("StaticDir", "static")
|
viper.SetDefault("StaticDir", "static")
|
||||||
|
@ -120,6 +122,10 @@ func InitializeConfig() {
|
||||||
viper.Set("DisableRSS", DisableRSS)
|
viper.Set("DisableRSS", DisableRSS)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if hugoCmdV.PersistentFlags().Lookup("disableSitemap").Changed {
|
||||||
|
viper.Set("DisableSitemap", DisableSitemap)
|
||||||
|
}
|
||||||
|
|
||||||
if hugoCmdV.PersistentFlags().Lookup("verbose").Changed {
|
if hugoCmdV.PersistentFlags().Lookup("verbose").Changed {
|
||||||
viper.Set("Verbose", Verbose)
|
viper.Set("Verbose", Verbose)
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,7 @@ type Node struct {
|
||||||
Keywords []string
|
Keywords []string
|
||||||
Params map[string]interface{}
|
Params map[string]interface{}
|
||||||
Date time.Time
|
Date time.Time
|
||||||
|
Sitemap Sitemap
|
||||||
UrlPath
|
UrlPath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -220,6 +220,10 @@ func (s *Site) Render() (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.timerStep("render and write homepage")
|
s.timerStep("render and write homepage")
|
||||||
|
if err = s.RenderSitemap(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.timerStep("render and write Sitemap")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -740,6 +744,36 @@ func (s *Site) RenderHomePage() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Site) RenderSitemap() error {
|
||||||
|
if viper.GetBool("DisableSitemap") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
optChanged := false
|
||||||
|
|
||||||
|
n := s.NewNode()
|
||||||
|
n.Data["Pages"] = s.Pages
|
||||||
|
|
||||||
|
// Force `UglyUrls` option to force `sitemap.xml` file name
|
||||||
|
switch s.Target.(type) {
|
||||||
|
case *target.Filesystem:
|
||||||
|
s.Target.(*target.Filesystem).UglyUrls = true
|
||||||
|
optChanged = true
|
||||||
|
}
|
||||||
|
|
||||||
|
smLayouts := []string{"sitemap.xml", "_default/sitemap.xml", "_internal/_default/sitemap.xml"}
|
||||||
|
err := s.render(n, "sitemap.xml", s.appendThemeTemplates(smLayouts)...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if optChanged {
|
||||||
|
s.Target.(*target.Filesystem).UglyUrls = viper.GetBool("UglyUrls")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Site) Stats() {
|
func (s *Site) Stats() {
|
||||||
jww.FEEDBACK.Printf("%d pages created \n", len(s.Pages))
|
jww.FEEDBACK.Printf("%d pages created \n", len(s.Pages))
|
||||||
|
|
||||||
|
|
18
hugolib/sitemap.go
Normal file
18
hugolib/sitemap.go
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
package hugolib
|
||||||
|
|
||||||
|
import jww "github.com/spf13/jwalterweatherman"
|
||||||
|
|
||||||
|
type Sitemap struct {
|
||||||
|
ChangeFreq string
|
||||||
|
Priority float32
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
|
@ -65,6 +65,17 @@ func (t *GoHtmlTemplate) EmbedTemplates() {
|
||||||
</channel>
|
</channel>
|
||||||
</rss>`)
|
</rss>`)
|
||||||
|
|
||||||
|
t.AddInternalTemplate("_default", "sitemap.xml", `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||||
|
{{ range .Data.Pages }}
|
||||||
|
<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 }}
|
||||||
|
</url>
|
||||||
|
{{ end }}
|
||||||
|
</urlset>`)
|
||||||
|
|
||||||
t.AddInternalTemplate("", "disqus.html", `{{ if .Site.DisqusShortname }}<div id="disqus_thread"></div>
|
t.AddInternalTemplate("", "disqus.html", `{{ if .Site.DisqusShortname }}<div id="disqus_thread"></div>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
var disqus_shortname = '{{ .Site.DisqusShortname }}';
|
var disqus_shortname = '{{ .Site.DisqusShortname }}';
|
||||||
|
|
Loading…
Reference in a new issue