1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 09:49:00 +01:00
gojekyll/pipelines/layouts.go

51 lines
1.1 KiB
Go
Raw Normal View History

2017-06-24 20:00:19 +02:00
package pipelines
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/osteele/gojekyll/templates"
2017-06-29 13:27:43 +02:00
"github.com/osteele/liquid"
)
// FindLayout returns a template for the named layout.
2017-06-24 19:30:01 +02:00
func (p *Pipeline) FindLayout(base string, fm *templates.VariableMap) (t liquid.Template, err error) {
exts := []string{"", ".html"}
2017-06-24 19:30:01 +02:00
for _, ext := range strings.SplitN(p.config.MarkdownExt, `,`, -1) {
exts = append(exts, "."+ext)
}
var (
2017-06-17 02:06:55 +02:00
name string
content []byte
found bool
)
for _, ext := range exts {
// TODO respect layout config
2017-06-24 19:30:01 +02:00
name = filepath.Join(p.LayoutsDir(), base+ext)
2017-06-17 02:06:55 +02:00
content, err = ioutil.ReadFile(name)
if err == nil {
found = true
break
}
if !os.IsNotExist(err) {
return nil, err
}
}
if !found {
return nil, fmt.Errorf("no template for %s", base)
}
2017-06-24 20:00:19 +02:00
*fm, err = templates.ReadFrontMatter(&content)
if err != nil {
return
}
2017-07-01 01:37:31 +02:00
return p.liquidEngine.ParseTemplate(content)
2017-06-24 19:30:01 +02:00
}
// LayoutsDir returns the path to the layouts directory.
func (p *Pipeline) LayoutsDir() string {
return filepath.Join(p.SourceDir, p.config.LayoutsDir)
}