1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-27 05:24:40 +01:00

Site data, pt. 2

This commit is contained in:
Oliver Steele 2017-06-21 09:11:09 -04:00
parent 8c64cbe6c6
commit dd7d072a85
4 changed files with 30 additions and 22 deletions

View File

@ -6,28 +6,10 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"syscall"
)
// VisitCreatedFile calls os.Create to create a file, and applies w to it.
func VisitCreatedFile(name string, w func(io.Writer) error) error {
f, err := os.Create(name)
if err != nil {
return err
}
close := true
defer func() {
if close {
_ = f.Close() // nolint: gas
}
}()
if err := w(f); err != nil {
return err
}
close = false
return f.Close()
}
// CopyFileContents copies the file contents from src to dst.
// It's not atomic and doesn't copy permissions or metadata.
func CopyFileContents(dst, src string, perm os.FileMode) error {
@ -143,3 +125,27 @@ func RemoveEmptyDirectories(root string) error {
}
return PostfixWalk(root, walkFn)
}
// TrimExt returns a path without its extension, e.g. "/a/b.c" -> "/a/b"
func TrimExt(filename string) string {
return strings.TrimSuffix(filename, filepath.Ext(filename))
}
// VisitCreatedFile calls os.Create to create a file, and applies w to it.
func VisitCreatedFile(name string, w func(io.Writer) error) error {
f, err := os.Create(name)
if err != nil {
return err
}
close := true
defer func() {
if close {
_ = f.Close() // nolint: gas
}
}()
if err := w(f); err != nil {
return err
}
close = false
return f.Close()
}

View File

@ -144,7 +144,7 @@ type StaticPage struct {
// Static returns a bool indicating that the page is a static page.
func (page *StaticPage) Static() bool { return true }
// TemplateObject returns metadata for use in the representation of the page as a collection item
// Variables returns metadata for use in the representation of the page as a collection item
func (page *StaticPage) Variables() VariableMap {
return MergeVariableMaps(page.frontMatter, page.pageFields.Variables())
}

View File

@ -196,7 +196,8 @@ func (site *Site) ReadCollections() error {
return nil
}
func (site *Site) CreateCollectionContent() error {
// CollectionVariable creates the value of the site.[collectionName] variable
func (site *Site) CollectionVariable() error {
for _, coll := range site.Collections {
for _, p := range coll.Pages() {
if err := p.Write(ioutil.Discard); err != nil {

View File

@ -59,7 +59,8 @@ func (site *Site) readDataFiles() (VariableMap, error) {
if err != nil {
return nil, helpers.PathError(err, "read YAML", filename)
}
data[filepath.Base(f.Name())] = fileData
basename := helpers.TrimExt(filepath.Base(f.Name()))
data[basename] = fileData
}
}
}