diff --git a/hugofs/files/classifier.go b/hugofs/files/classifier.go index 9aa2476b7bf..a7d0c3cbf6c 100644 --- a/hugofs/files/classifier.go +++ b/hugofs/files/classifier.go @@ -52,11 +52,16 @@ func IsContentExt(ext string) bool { const ( ContentClassLeaf = "leaf" ContentClassBranch = "branch" + ContentClassData = "data" ContentClassFile = "zfile" // Sort below ContentClassContent = "zcontent" ) func ClassifyContentFile(filename string) string { + if strings.HasPrefix(filename, "_content.") { + return ContentClassData + } + if !IsContentFile(filename) { return ContentClassFile } diff --git a/hugolib/page__meta.go b/hugolib/page__meta.go index e8ef13bfde2..14e7f9a350e 100644 --- a/hugolib/page__meta.go +++ b/hugolib/page__meta.go @@ -258,19 +258,15 @@ func (p *pageMeta) Section() string { return "" } - if p.IsNode() { - if len(p.sections) == 0 { - // May be a sitemap or similar. - return "" - } - return p.sections[0] - } - - if !p.File().IsZero() { + if p.IsPage() && !p.File().IsZero() { return p.File().Section() } - panic("invalid page state") + if len(p.sections) == 0 { + // May be a sitemap or similar. + return "" + } + return p.sections[0] } diff --git a/hugolib/pages_capture.go b/hugolib/pages_capture.go index 591b8e31755..d47d3e634bb 100644 --- a/hugolib/pages_capture.go +++ b/hugolib/pages_capture.go @@ -16,11 +16,16 @@ package hugolib import ( "context" "fmt" + "io" "os" pth "path" "path/filepath" "strings" + "github.com/gohugoio/hugo/resources/page" + + yaml "gopkg.in/yaml.v2" + "github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/hugofs/files" @@ -631,6 +636,37 @@ func (proc *pagesProcessor) newPageFromBundle(b *fileinfoBundle) (*pageState, er return p, nil } +func (proc *pagesProcessor) newPagesFromData(fim hugofs.FileMetaInfo, send func(p *pageState, err error)) { + + meta := fim.Meta() + f, err := meta.Open() + if err != nil { + send(nil, err) + return + } + defer f.Close() + + s := proc.getSite(meta.Lang()) + + dec := yaml.NewDecoder(f) + for { + m := make(map[string]interface{}) + if err := dec.Decode(m); err != nil { + if err == io.EOF { + break + } + send(nil, err) + return + } + + send(newPageFromMeta(m, &pageMeta{ + kind: page.KindPage, + s: s, + })) + + } +} + func (proc *pagesProcessor) newPageFromFi(fim hugofs.FileMetaInfo, owner *pageState) (*pageState, error) { fi, err := newFileInfo(proc.sp, fim) if err != nil { @@ -753,6 +789,8 @@ func (proc *pagesProcessor) process(item interface{}) error { send(proc.newPageFromFi(v, nil)) case files.ContentClassFile: proc.sendError(proc.copyFile(v)) + case files.ContentClassData: + proc.newPagesFromData(v, send) default: panic(fmt.Sprintf("invalid classifier: %q", classifier)) } diff --git a/hugolib/pages_from_data_test.go b/hugolib/pages_from_data_test.go new file mode 100644 index 00000000000..60f97c3df3f --- /dev/null +++ b/hugolib/pages_from_data_test.go @@ -0,0 +1,36 @@ +// Copyright 2019 The Hugo Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package hugolib + +import ( + "testing" + + qt "github.com/frankban/quicktest" +) + +func TestPagesFromYAML(t *testing.T) { + b := newTestSitesBuilder(t) + + b.WithContent("_content.yaml", ` +title: Yaml Page 1 +--- +title: Yaml Page 2 +`) + + b.Build(BuildCfg{}) + + s := b.H.Sites[0] + + b.Assert(s.RegularPages(), qt.HasLen, 2) +}