shithub: hugo

Download patch

ref: 311593bff013d5b99c68f498861ffde810be71b1
parent: 5b90b388cb5042ee7ee729e70a55228004f0def6
author: Jeff Hodges <[email protected]>
date: Sat Aug 1 19:02:20 EDT 2015

Add undocumented published setting to front matter

A new "published" setting that is the opposite of "draft" is added and
left intentionally undocumented.

This setting comes from jekyll and eases the transition to hugo
greatly. We leave it undocumented so that folks don't rely on it, but
also don't shoot themselves in the foot during a jekyll migration.

The foot-shooting occurs if they have only a few documents that were
drafts ("published: false") in the jekyll version of their site and
don't notice that they were published in the migration to hugo.

--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -463,6 +463,8 @@
 	return link.String(), nil
 }
 
+var ErrHasDraftAndPublished = errors.New("both draft and published parameters were found in page's frontmatter")
+
 func (p *Page) update(f interface{}) error {
 	if f == nil {
 		return fmt.Errorf("no metadata found")
@@ -469,6 +471,7 @@
 	}
 	m := f.(map[string]interface{})
 	var err error
+	var draft, published *bool
 	for k, v := range m {
 		loki := strings.ToLower(k)
 		switch loki {
@@ -507,7 +510,11 @@
 				jww.ERROR.Printf("Failed to parse publishdate '%v' in page %s", v, p.File.Path())
 			}
 		case "draft":
-			p.Draft = cast.ToBool(v)
+			draft = new(bool)
+			*draft = cast.ToBool(v)
+		case "published": // Intentionally undocumented
+			published = new(bool)
+			*published = !cast.ToBool(v)
 		case "layout":
 			p.layout = cast.ToString(v)
 		case "markup":
@@ -563,6 +570,16 @@
 				}
 			}
 		}
+	}
+
+	if draft != nil && published != nil {
+		p.Draft = *draft
+		jww.ERROR.Printf("page %s has both draft and published settings in its frontmatter. Using draft.", p.File.Path())
+		return ErrHasDraftAndPublished
+	} else if draft != nil {
+		p.Draft = *draft
+	} else if published != nil {
+		p.Draft = !*published
 	}
 
 	if p.Lastmod.IsZero() {
--- a/hugolib/page_test.go
+++ b/hugolib/page_test.go
@@ -854,6 +854,21 @@
 	}
 }
 
+var PAGE_WITH_DRAFT_AND_PUBLISHED = `---
+title: broken
+published: false
+draft: true
+---
+some content
+`
+
+func TestDraftAndPublishedFrontMatterError(t *testing.T) {
+	_, err := NewPageFrom(strings.NewReader(PAGE_WITH_DRAFT_AND_PUBLISHED), "content/post/broken.md")
+	if err != ErrHasDraftAndPublished {
+		t.Errorf("expected ErrHasDraftAndPublished, was %#v", err)
+	}
+}
+
 func listEqual(left, right []string) bool {
 	if len(left) != len(right) {
 		return false