hugolib: Support an expiration date
authorHanchen Wang <hanchen.wang@mail.utoronto.ca>
Wed, 11 May 2016 14:04:53 +0000 (10:04 -0400)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 14 Jun 2016 13:45:25 +0000 (15:45 +0200)
hugolib/page.go
hugolib/site_test.go

index 68f62e8f44f9514df09a0ad309282918dffbc2a5..5c81d79ee8d8ce6a86b97a478b0d29ed9ba01473 100644 (file)
@@ -59,6 +59,7 @@ type Page struct {
        Truncated           bool
        Draft               bool
        PublishDate         time.Time
+       ExpiryDate          time.Time
        Markup              string
        extension           string
        contentType         string
@@ -467,7 +468,8 @@ func (p *Page) LinkTitle() string {
 }
 
 func (p *Page) ShouldBuild() bool {
-       if viper.GetBool("BuildFuture") || p.PublishDate.IsZero() || p.PublishDate.Before(time.Now()) {
+       if (viper.GetBool("BuildFuture") || p.PublishDate.IsZero() || p.PublishDate.Before(time.Now())) &&
+               (viper.GetBool("BuildExpired") || p.ExpiryDate.IsZero() || p.ExpiryDate.After(time.Now())) {
                if viper.GetBool("BuildDrafts") || !p.Draft {
                        return true
                }
@@ -480,10 +482,11 @@ func (p *Page) IsDraft() bool {
 }
 
 func (p *Page) IsFuture() bool {
-       if p.PublishDate.Before(time.Now()) {
-               return false
-       }
-       return true
+       return p.PublishDate.After(time.Now())
+}
+
+func (p *Page) IsExpired() bool {
+       return p.ExpiryDate.Before(time.Now())
 }
 
 func (p *Page) Permalink() (string, error) {
@@ -564,6 +567,11 @@ func (p *Page) update(f interface{}) error {
                        if err != nil {
                                jww.ERROR.Printf("Failed to parse publishdate '%v' in page %s", v, p.File.Path())
                        }
+               case "expirydate", "unpublishdate":
+                       p.ExpiryDate, err = cast.ToTimeE(v)
+                       if err != nil {
+                               jww.ERROR.Printf("Failed to parse expirydate '%v' in page %s", v, p.File.Path())
+                       }
                case "draft":
                        draft = new(bool)
                        *draft = cast.ToBool(v)
index f0c7a8e2d9ed375f555b57e494e92157d813b5d3..4e68b5ff81a0c4ce506bae19ff39a6aac9029596 100644 (file)
@@ -296,6 +296,48 @@ func TestDraftAndFutureRender(t *testing.T) {
        viper.Set("BuildFuture", false)
 }
 
+func TestFutureExpirationRender(t *testing.T) {
+       viper.Reset()
+       defer viper.Reset()
+
+       hugofs.InitMemFs()
+       sources := []source.ByteSource{
+               {filepath.FromSlash("sect/doc3.md"), []byte("---\ntitle: doc1\nexpirydate: \"2400-05-29\"\n---\n# doc1\n*some content*")},
+               {filepath.FromSlash("sect/doc4.md"), []byte("---\ntitle: doc2\nexpirydate: \"2000-05-29\"\n---\n# doc2\n*some content*")},
+       }
+
+       siteSetup := func() *Site {
+               s := &Site{
+                       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")
+
+       s := siteSetup()
+
+       if len(s.Pages) != 1 {
+               if len(s.Pages) > 1 {
+                       t.Fatal("Expired content published unexpectedly")
+               }
+
+               if len(s.Pages) < 1 {
+                       t.Fatal("Valid content expired unexpectedly")
+               }
+       }
+
+       if s.Pages[0].Title == "doc2" {
+               t.Fatal("Expired content published unexpectedly")
+       }
+}
+
 // Issue #957
 func TestCrossrefs(t *testing.T) {
        hugofs.InitMemFs()