1
0
mirror of https://github.com/danog/gojekyll.git synced 2025-01-22 23:11:26 +01:00
gojekyll/pages/file.go

69 lines
1.7 KiB
Go
Raw Normal View History

package pages
import (
"fmt"
"os"
"time"
"github.com/osteele/gojekyll/frontmatter"
)
2017-07-02 13:46:05 -04:00
// file is embedded in StaticFile and page
type file struct {
2017-07-10 13:23:51 -04:00
site Site
2017-07-25 11:08:53 -04:00
filename string // target filepath
relpath string // slash-separated path relative to site or container source
outputExt string
permalink string // cached permalink
fileModTime time.Time
frontMatter frontmatter.FrontMatter
}
2017-07-10 13:23:51 -04: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, fm frontmatter.FrontMatter) (Document, error) {
hasFM, 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 13:23:51 -04:00
site: s,
filename: filename,
frontMatter: fm,
fileModTime: info.ModTime(),
relpath: relpath,
outputExt: s.Config().OutputExt(relpath),
}
if hasFM || !s.Config().RequiresFrontMatter(relpath) {
2017-07-08 19:57:41 -04:00
return makePage(filename, fields)
}
2017-07-10 14:14:42 -04:00
fields.permalink = "/" + relpath
2017-07-08 19:57:41 -04:00
p := &StaticFile{fields}
return p, nil
}
2017-07-25 11:08:53 -04:00
func (f *file) String() string {
return fmt.Sprintf("%T{Path=%v, Permalink=%v}", f, f.relpath, f.permalink)
}
func (f *file) OutputExt() string { return f.outputExt }
func (f *file) Permalink() string { return f.permalink }
func (f *file) Published() bool { return f.frontMatter.Bool("published", true) }
2017-07-25 11:08:53 -04:00
func (f *file) SourcePath() string { return f.filename }
// const requiresReloadError = error.Error("requires reload")
func (f *file) Reload() error {
info, err := os.Stat(f.filename)
if err != nil {
return err
}
f.fileModTime = info.ModTime()
return nil
}