shithub: hugo

Download patch

ref: d4156e61277f4a006670a9eb1acba8b98140c5ef
parent: 2564f46a685704c459bec5d0100f5111c138c9b4
author: Hanchen Wang <[email protected]>
date: Wed May 11 06:04:53 EDT 2016

hugolib: Support an expiration date

--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -59,6 +59,7 @@
 	Truncated           bool
 	Draft               bool
 	PublishDate         time.Time
+	ExpiryDate          time.Time
 	Markup              string
 	extension           string
 	contentType         string
@@ -467,7 +468,8 @@
 }
 
 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,12 +482,13 @@
 }
 
 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) {
 	link, err := p.permalink()
 	if err != nil {
@@ -563,6 +566,11 @@
 			p.PublishDate, err = cast.ToTimeE(v)
 			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)
--- a/hugolib/site_test.go
+++ b/hugolib/site_test.go
@@ -296,6 +296,48 @@
 	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()