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.
This commit is contained in:
Jeff Hodges 2015-08-01 23:02:20 -07:00 committed by Bjørn Erik Pedersen
parent 5b90b388cb
commit 311593bff0
2 changed files with 33 additions and 1 deletions

View file

@ -463,12 +463,15 @@ func (p *Page) RelPermalink() (string, error) {
return link.String(), nil 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 { func (p *Page) update(f interface{}) error {
if f == nil { if f == nil {
return fmt.Errorf("no metadata found") return fmt.Errorf("no metadata found")
} }
m := f.(map[string]interface{}) m := f.(map[string]interface{})
var err error var err error
var draft, published *bool
for k, v := range m { for k, v := range m {
loki := strings.ToLower(k) loki := strings.ToLower(k)
switch loki { 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()) jww.ERROR.Printf("Failed to parse publishdate '%v' in page %s", v, p.File.Path())
} }
case "draft": 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": case "layout":
p.layout = cast.ToString(v) p.layout = cast.ToString(v)
case "markup": 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() { if p.Lastmod.IsZero() {
p.Lastmod = p.Date p.Lastmod = p.Date
} }

View file

@ -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 { func listEqual(left, right []string) bool {
if len(left) != len(right) { if len(left) != len(right) {
return false return false