Skip to content

Commit

Permalink
Pages from data
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Sep 6, 2019
1 parent 77b23fe commit 2fbc944
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 10 deletions.
5 changes: 5 additions & 0 deletions hugofs/files/classifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 6 additions & 10 deletions hugolib/page__meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]

}

Expand Down
38 changes: 38 additions & 0 deletions hugolib/pages_capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
}
Expand Down
36 changes: 36 additions & 0 deletions hugolib/pages_from_data_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 2fbc944

Please sign in to comment.