From c502f078bc964a683f05fb4e877b3a1b50894837 Mon Sep 17 00:00:00 2001 From: spf13 Date: Thu, 29 May 2014 00:48:40 -0400 Subject: [PATCH] Add handling for publishDates (which will be ignored if in the future). Fixed #260 --- commands/hugo.go | 8 +++++- hugolib/page.go | 14 ++++++++++- hugolib/site.go | 2 +- hugolib/site_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 3 deletions(-) diff --git a/commands/hugo.go b/commands/hugo.go index 573a405d0..9e5d18645 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -51,7 +51,7 @@ Complete documentation is available at http://hugo.spf13.com`, var hugoCmdV *cobra.Command -var BuildWatch, Draft, UglyUrls, Verbose, Logging, VerboseLog, DisableRSS, DisableSitemap bool +var BuildWatch, Draft, Future, UglyUrls, Verbose, Logging, VerboseLog, DisableRSS, DisableSitemap bool var Source, Destination, Theme, BaseUrl, CfgFile, LogFile string func Execute() { @@ -70,6 +70,7 @@ func AddCommands() { func init() { HugoCmd.PersistentFlags().BoolVarP(&Draft, "buildDrafts", "D", false, "include content marked as draft") + HugoCmd.PersistentFlags().BoolVarP(&Future, "buildFuture", "F", false, "include content with datePublished in the future") 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") @@ -108,6 +109,7 @@ func InitializeConfig() { viper.SetDefault("PublishDir", "public") viper.SetDefault("DefaultLayout", "post") viper.SetDefault("BuildDrafts", false) + viper.SetDefault("BuildFuture", false) viper.SetDefault("UglyUrls", false) viper.SetDefault("Verbose", false) viper.SetDefault("CanonifyUrls", false) @@ -122,6 +124,10 @@ func InitializeConfig() { viper.Set("BuildDrafts", Draft) } + if hugoCmdV.PersistentFlags().Lookup("buildFuture").Changed { + viper.Set("BuildFuture", Future) + } + if hugoCmdV.PersistentFlags().Lookup("uglyUrls").Changed { viper.Set("UglyUrls", UglyUrls) } diff --git a/hugolib/page.go b/hugolib/page.go index ca0ba630a..94c9b928a 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -44,6 +44,7 @@ type Page struct { Params map[string]interface{} contentType string Draft bool + PublishDate time.Time Aliases []string Tmpl Template Markup string @@ -276,6 +277,15 @@ func (p *Page) LinkTitle() string { } } +func (page *Page) ShouldBuild() bool { + if viper.GetBool("BuildFuture") || page.PublishDate.IsZero() || page.PublishDate.Before(time.Now()) { + if viper.GetBool("BuildDrafts") || !page.Draft { + return true + } + } + return false +} + func (p *Page) Permalink() (string, error) { link, err := p.permalink() if err != nil { @@ -323,8 +333,10 @@ func (page *Page) update(f interface{}) error { page.contentType = cast.ToString(v) case "keywords": page.Keywords = cast.ToStringSlice(v) - case "date", "pubdate": + case "date": page.Date = cast.ToTime(v) + case "publishdate", "pubdate": + page.PublishDate = cast.ToTime(v) case "draft": page.Draft = cast.ToBool(v) case "layout": diff --git a/hugolib/site.go b/hugolib/site.go index f2ea335aa..86b4e07b9 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -344,7 +344,7 @@ func (s *Site) CreatePages() (err error) { return err } - if viper.GetBool("BuildDrafts") || !page.Draft { + if page.ShouldBuild() { s.Pages = append(s.Pages, page) } diff --git a/hugolib/site_test.go b/hugolib/site_test.go index d98efa0ea..ecb81ebf8 100644 --- a/hugolib/site_test.go +++ b/hugolib/site_test.go @@ -219,6 +219,66 @@ func TestTargetPath(t *testing.T) { } } +func TestDraftAndFutureRender(t *testing.T) { + files := make(map[string][]byte) + target := &target.InMemoryTarget{Files: files} + sources := []source.ByteSource{ + {"sect/doc1.md", []byte("---\ntitle: doc1\ndraft: true\npublishdate: \"2414-05-29\"\n---\n# doc1\n*some content*"), "sect"}, + {"sect/doc2.md", []byte("---\ntitle: doc2\ndraft: true\npublishdate: \"2012-05-29\"\n---\n# doc2\n*some content*"), "sect"}, + {"sect/doc3.md", []byte("---\ntitle: doc3\ndraft: false\npublishdate: \"2414-05-29\"\n---\n# doc3\n*some content*"), "sect"}, + {"sect/doc4.md", []byte("---\ntitle: doc4\ndraft: false\npublishdate: \"2012-05-29\"\n---\n# doc4\n*some content*"), "sect"}, + } + + siteSetup := func() *Site { + s := &Site{ + Target: target, + Source: &source.InMemorySource{ByteSource: sources}, + } + + s.initializeSiteInfo() + + if err := s.CreatePages(); err != nil { + t.Fatalf("Unable to create pages: %s", err) + } + return s + } + + viper.Set("baseurl", "http://auth/bub") + + // Testing Defaults.. Only draft:true and publishDate in the past should be rendered + s := siteSetup() + if len(s.Pages) != 1 { + t.Fatal("Draft or Future dated content published unexpectedly") + } + + // only publishDate in the past should be rendered + viper.Set("BuildDrafts", true) + s = siteSetup() + if len(s.Pages) != 2 { + t.Fatal("Future Dated Posts published unexpectedly") + } + + // drafts should not be rendered, but all dates should + viper.Set("BuildDrafts", false) + viper.Set("BuildFuture", true) + s = siteSetup() + if len(s.Pages) != 2 { + t.Fatal("Draft posts published unexpectedly") + } + + // all 4 should be included + viper.Set("BuildDrafts", true) + viper.Set("BuildFuture", true) + s = siteSetup() + if len(s.Pages) != 4 { + t.Fatal("Drafts or Future posts not included as expected") + } + + //setting defaults back + viper.Set("BuildDrafts", false) + viper.Set("BuildFuture", false) +} + func TestSkipRender(t *testing.T) { files := make(map[string][]byte) target := &target.InMemoryTarget{Files: files}