1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-02 20:47:46 +01:00
gojekyll/pages/drops.go

90 lines
2.3 KiB
Go
Raw Normal View History

2017-07-04 15:09:36 +02:00
package pages
import (
"path"
"path/filepath"
"github.com/osteele/gojekyll/templates"
2017-07-09 22:17:20 +02:00
"github.com/osteele/gojekyll/utils"
2017-08-16 00:55:18 +02:00
"github.com/osteele/liquid"
2017-07-04 15:09:36 +02:00
)
// ToLiquid is part of the liquid.Drop interface.
func (d *StaticFile) ToLiquid() interface{} {
2017-08-16 00:55:18 +02:00
return liquid.IterationKeyedMap(map[string]interface{}{
"name": path.Base(d.relpath),
"basename": utils.TrimExt(path.Base(d.relpath)),
"path": d.Permalink(),
"modified_time": d.fileModTime,
"extname": d.OutputExt(),
// de facto:
"collection": nil,
2017-08-16 00:55:18 +02:00
})
}
2017-07-04 15:09:36 +02:00
func (f *file) ToLiquid() interface{} {
var (
relpath = "/" + filepath.ToSlash(f.relpath)
base = path.Base(relpath)
ext = path.Ext(relpath)
)
2017-08-16 00:55:18 +02:00
return liquid.IterationKeyedMap(templates.MergeVariableMaps(f.frontMatter, map[string]interface{}{
2017-07-04 15:09:36 +02:00
"path": relpath,
"modified_time": f.fileModTime,
"name": base,
2017-07-09 22:17:20 +02:00
"basename": utils.TrimExt(base),
2017-07-04 15:09:36 +02:00
"extname": ext,
2017-08-16 00:55:18 +02:00
}))
2017-07-04 15:09:36 +02:00
}
// ToLiquid is in the liquid.Drop interface.
func (p *page) ToLiquid() interface{} {
var (
vars = templates.VariableMap(p.frontMatter)
2017-07-04 15:09:36 +02:00
relpath = p.relpath
ext = filepath.Ext(relpath)
)
data := map[string]interface{}{
2017-08-10 16:44:04 +02:00
"content": p.maybeContent(),
"excerpt": p.Excerpt(),
2017-07-07 01:31:36 +02:00
"path": relpath,
"url": p.Permalink(),
"slug": vars.String("slug", utils.Slugify(utils.TrimExt(filepath.Base(p.relpath)))),
2017-08-10 16:44:04 +02:00
// "output": // TODO; includes layouts
2017-07-04 15:09:36 +02:00
2017-08-10 16:44:04 +02:00
// TODO documented as present in all pages, but de facto only defined for collection pages
"id": utils.TrimExt(p.Permalink()),
2017-07-04 15:09:36 +02:00
"categories": p.Categories(),
"tags": p.Tags(),
2017-08-10 16:44:04 +02:00
// "title": base,
2017-07-04 15:09:36 +02:00
// TODO Only present in collection pages https://jekyllrb.com/docs/collections/#documents
2017-07-24 15:32:57 +02:00
"relative_path": filepath.ToSlash(p.site.RelativePath(p.filename)),
2017-07-04 15:09:36 +02:00
// TODO collection(name)
// de facto
2017-07-04 15:09:36 +02:00
"ext": ext,
}
for k, v := range p.frontMatter {
switch k {
// doc implies these aren't present, but they appear to be present in a collection page:
// case "layout", "published":
case "permalink":
// omit this, in order to use the value above
default:
data[k] = v
}
}
2017-08-16 00:55:18 +02:00
return liquid.IterationKeyedMap(data)
2017-07-04 15:09:36 +02:00
}
2017-08-10 16:44:04 +02:00
func (p *page) maybeContent() interface{} {
2017-08-10 15:06:53 +02:00
p.RLock()
defer p.RUnlock()
2017-08-10 16:44:04 +02:00
if p.rendered {
return p.content
2017-07-10 19:23:51 +02:00
}
2017-08-10 16:44:04 +02:00
return p.raw
2017-07-10 19:23:51 +02:00
}