2017-06-24 20:00:19 +02:00
|
|
|
package pipelines
|
2017-06-16 21:14:56 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2017-06-22 16:42:57 +02:00
|
|
|
"github.com/osteele/gojekyll/templates"
|
2017-06-29 13:27:43 +02:00
|
|
|
"github.com/osteele/liquid"
|
2017-06-16 21:14:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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) {
|
2017-06-16 21:14:56 +02:00
|
|
|
exts := []string{"", ".html"}
|
2017-06-24 19:30:01 +02:00
|
|
|
for _, ext := range strings.SplitN(p.config.MarkdownExt, `,`, -1) {
|
2017-06-16 21:14:56 +02:00
|
|
|
exts = append(exts, "."+ext)
|
|
|
|
}
|
|
|
|
var (
|
2017-06-17 02:06:55 +02:00
|
|
|
name string
|
2017-06-16 21:14:56 +02:00
|
|
|
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)
|
2017-06-16 21:14:56 +02:00
|
|
|
if err == nil {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2017-06-29 13:41:14 +02:00
|
|
|
return nil, fmt.Errorf("no template for %s", base)
|
2017-06-16 21:14:56 +02:00
|
|
|
}
|
2017-06-24 20:00:19 +02:00
|
|
|
*fm, err = templates.ReadFrontMatter(&content)
|
2017-06-16 21:14:56 +02:00
|
|
|
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 {
|
2017-06-29 13:41:14 +02:00
|
|
|
return filepath.Join(p.SourceDir, p.config.LayoutsDir)
|
2017-06-16 21:14:56 +02:00
|
|
|
}
|