1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 05:58:59 +01:00
gojekyll/plugins/default_layout.go

48 lines
870 B
Go
Raw Normal View History

2017-09-02 18:10:35 +02:00
package plugins
func init() {
register("jekyll-default-layout", jekyllDefaultLayout{})
}
type jekyllDefaultLayout struct{ plugin }
const (
2017-09-02 21:50:47 +02:00
pageLayoutName = "page"
postLayoutName = "post"
homeLayoutName = "home"
2017-09-02 18:10:35 +02:00
)
2017-09-02 21:50:47 +02:00
var layoutNames = []string{"default", postLayoutName, pageLayoutName, homeLayoutName}
2017-09-02 18:10:35 +02:00
2017-09-02 21:50:47 +02:00
func (p jekyllDefaultLayout) layoutNames(s Site) map[string]string {
var m = map[string]string{}
var n string
for _, k := range layoutNames {
if s.HasLayout(k) {
n = k
2017-09-02 18:10:35 +02:00
}
2017-09-02 21:50:47 +02:00
m[k] = n
2017-09-02 18:10:35 +02:00
}
2017-09-02 21:50:47 +02:00
return m
2017-09-02 18:10:35 +02:00
}
func (p jekyllDefaultLayout) PostInitPage(s Site, pg Page) error {
fm := pg.FrontMatter()
if fm["layout"] != nil {
return nil
}
layoutNames := p.layoutNames(s)
2017-09-02 21:50:47 +02:00
k := pageLayoutName
2017-09-02 18:10:35 +02:00
switch {
case pg.IsPost():
2017-09-02 21:50:47 +02:00
k = postLayoutName
2017-09-02 19:53:50 +02:00
case pg.URL() == "/":
2017-09-02 21:50:47 +02:00
k = homeLayoutName
2017-09-02 18:10:35 +02:00
}
2017-09-02 21:50:47 +02:00
n := layoutNames[k]
if n != "" {
fm["layout"] = n
2017-09-02 18:10:35 +02:00
}
return nil
}