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:
parent
8c64cbe6c6
commit
dd7d072a85
@ -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()
|
||||
}
|
||||
|
2
page.go
2
page.go
@ -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())
|
||||
}
|
||||
|
3
site.go
3
site.go
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user