1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-02 14:37:50 +01:00
gojekyll/pages/file.go

60 lines
1.6 KiB
Go
Raw Normal View History

package pages
import (
"fmt"
"os"
"time"
"github.com/osteele/gojekyll/frontmatter"
"github.com/osteele/gojekyll/templates"
)
2017-07-02 19:46:05 +02:00
// file is embedded in StaticFile and page
type file struct {
2017-07-10 19:23:51 +02:00
site Site
filename string // target os filepath
relpath string // slash-separated path relative to site or container source
outputExt string
permalink string // cached permalink
fileModTime time.Time
2017-07-01 05:56:29 +02:00
frontMatter map[string]interface{}
}
func (f *file) String() string {
2017-07-10 20:14:42 +02:00
return fmt.Sprintf("%T{Path=%v, Permalink=%v}", f, f.relpath, f.permalink)
}
2017-07-09 01:57:41 +02:00
func (f *file) OutputExt() string { return f.outputExt }
func (f *file) Permalink() string { return f.permalink }
func (f *file) Published() bool { return templates.VariableMap(f.frontMatter).Bool("published", true) }
func (f *file) SourcePath() string { return f.filename }
2017-07-10 19:23:51 +02:00
// NewFile creates a Page or StaticFile.
//
// filename is the absolute filename. relpath is the path relative to the site or collection directory.
func NewFile(s Site, filename string, relpath string, defaults map[string]interface{}) (Document, error) {
fm, err := frontmatter.FileHasFrontMatter(filename)
if err != nil {
return nil, err
}
info, err := os.Stat(filename)
if err != nil {
return nil, err
}
fields := file{
2017-07-10 19:23:51 +02:00
site: s,
filename: filename,
frontMatter: defaults,
fileModTime: info.ModTime(),
relpath: relpath,
2017-07-10 19:23:51 +02:00
outputExt: s.OutputExt(relpath),
}
2017-07-24 18:18:24 +02:00
if fm || !s.Config().RequiresFrontMatter(relpath) {
2017-07-09 01:57:41 +02:00
return makePage(filename, fields)
}
2017-07-10 20:14:42 +02:00
fields.permalink = "/" + relpath
2017-07-09 01:57:41 +02:00
p := &StaticFile{fields}
return p, nil
}