From 311593bff013d5b99c68f498861ffde810be71b1 Mon Sep 17 00:00:00 2001 From: Jeff Hodges Date: Sat, 1 Aug 2015 23:02:20 -0700 Subject: [PATCH] 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. --- hugolib/page.go | 19 ++++++++++++++++++- hugolib/page_test.go | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/hugolib/page.go b/hugolib/page.go index b139bbb79..f281053a2 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -463,12 +463,15 @@ func (p *Page) RelPermalink() (string, error) { 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") } 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 @@ func (p *Page) update(f interface{}) error { 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": @@ -565,6 +572,16 @@ func (p *Page) update(f interface{}) error { } } + 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() { p.Lastmod = p.Date } diff --git a/hugolib/page_test.go b/hugolib/page_test.go index 9b613d05f..7a9c12321 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -854,6 +854,21 @@ func TestPagePaths(t *testing.T) { } } +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