1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-02 17:17:49 +01:00
gojekyll/pages/page.go

106 lines
2.2 KiB
Go
Raw Normal View History

2017-06-22 17:02:32 +02:00
package pages
import (
"bytes"
2017-07-03 15:37:14 +02:00
"fmt"
"io"
"io/ioutil"
2017-07-03 15:37:14 +02:00
"time"
"github.com/osteele/gojekyll/templates"
2017-07-05 17:35:20 +02:00
"github.com/osteele/liquid/evaluator"
)
2017-07-02 19:46:05 +02:00
type page struct {
file
2017-07-01 15:35:54 +02:00
raw []byte
content *[]byte
}
// Static is in the File interface.
2017-07-02 19:46:05 +02:00
func (p *page) Static() bool { return false }
2017-07-02 19:46:05 +02:00
func newPage(filename string, f file) (*page, error) {
b, err := ioutil.ReadFile(filename)
2017-06-17 06:16:59 +02:00
if err != nil {
2017-06-22 16:37:31 +02:00
return nil, err
2017-06-17 06:16:59 +02:00
}
frontMatter, err := templates.ReadFrontMatter(&b)
2017-06-15 15:07:06 +02:00
if err != nil {
2017-06-22 16:37:31 +02:00
return nil, err
2017-06-15 15:07:06 +02:00
}
f.frontMatter = templates.MergeVariableMaps(f.frontMatter, frontMatter)
2017-07-02 19:46:05 +02:00
return &page{
file: f,
raw: b,
}, nil
}
// TemplateContext returns the local variables for template evaluation
2017-07-02 19:46:05 +02:00
func (p *page) TemplateContext(rc RenderingContext) map[string]interface{} {
2017-07-01 05:56:29 +02:00
return map[string]interface{}{
2017-07-03 15:37:14 +02:00
"page": p,
2017-07-03 16:39:55 +02:00
"site": rc.Site(),
}
}
2017-06-15 15:07:06 +02:00
2017-07-03 15:37:14 +02:00
// // Categories is part of the Page interface.
// func (p *page) Categories() []string {
// return []string{}
// }
// Tags is part of the Page interface.
func (p *page) Tags() []string {
return []string{}
}
// PostDate is part of the Page interface.
func (p *page) PostDate() time.Time {
switch value := p.frontMatter["date"].(type) {
case time.Time:
return value
case string:
2017-07-05 17:35:20 +02:00
t, err := evaluator.ParseTime(value)
2017-07-03 15:37:14 +02:00
if err == nil {
return t
}
default:
panic(fmt.Sprintf("expected a date %v", value))
}
panic("read posts should have set this")
}
// Write applies Liquid and Markdown, as appropriate.
2017-07-04 23:36:06 +02:00
func (p *page) Write(w io.Writer, rc RenderingContext) error {
rp := rc.RenderingPipeline()
b, err := rp.Render(w, p.raw, p.filename, p.TemplateContext(rc))
if err != nil {
return err
}
2017-07-04 23:13:47 +02:00
layout, ok := p.frontMatter["layout"].(string)
if ok && layout != "" {
b, err = rp.ApplyLayout(layout, b, p.TemplateContext(rc))
if err != nil {
return err
}
}
_, err = w.Write(b)
return err
2017-06-15 15:07:06 +02:00
}
2017-06-22 18:56:08 +02:00
2017-07-01 15:35:54 +02:00
// Content computes the page content.
2017-07-02 19:46:05 +02:00
func (p *page) Content(rc RenderingContext) ([]byte, error) {
2017-07-01 15:35:54 +02:00
if p.content == nil {
// TODO DRY w/ Page.Write
rp := rc.RenderingPipeline()
buf := new(bytes.Buffer)
b, err := rp.Render(buf, p.raw, p.filename, p.TemplateContext(rc))
if err != nil {
return nil, err
2017-06-22 18:56:08 +02:00
}
2017-07-01 15:35:54 +02:00
p.content = &b
2017-06-22 18:56:08 +02:00
}
2017-07-01 15:35:54 +02:00
return *p.content, nil
2017-06-22 18:56:08 +02:00
}