1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-27 14:44:43 +01:00
gojekyll/pages/file.go

72 lines
1.9 KiB
Go
Raw Normal View History

package pages
import (
"fmt"
"os"
"reflect"
"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 {
container Container
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 {
return fmt.Sprintf("%s{Path=%v, Permalink=%v}", reflect.TypeOf(f).Name(), f.relpath, f.permalink)
}
2017-07-09 01:57:41 +02:00
func (f *file) OutputExt() string { return f.outputExt }
func (f *file) Path() string { return f.relpath }
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.relpath }
// NewFile creates a Post or StaticFile.
2017-07-01 05:56:29 +02:00
func NewFile(filename string, c Container, 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{
container: c,
filename: filename,
frontMatter: defaults,
fileModTime: info.ModTime(),
relpath: relpath,
outputExt: c.OutputExt(relpath),
}
if fm {
2017-07-09 01:57:41 +02:00
return makePage(filename, fields)
}
2017-07-09 01:57:41 +02:00
p := &StaticFile{fields}
2017-07-03 15:37:14 +02:00
if err = p.setPermalink(); err != nil {
return nil, err
}
return p, nil
}
2017-07-03 15:37:14 +02:00
// Categories is in the File interface
func (f *file) Categories() []string {
return frontmatter.FrontMatter(f.frontMatter).SortedStringArray("categories")
2017-07-03 15:37:14 +02:00
}
// Tags is in the File interface
2017-07-03 15:37:14 +02:00
func (f *file) Tags() []string {
return frontmatter.FrontMatter(f.frontMatter).SortedStringArray("tags")
}