2017-06-24 14:00:19 -04:00
|
|
|
package pipelines
|
2017-06-16 15:14:56 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2017-07-09 11:57:20 -04:00
|
|
|
"github.com/osteele/gojekyll/frontmatter"
|
2017-06-29 07:27:43 -04:00
|
|
|
"github.com/osteele/liquid"
|
2017-06-16 15:14:56 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// FindLayout returns a template for the named layout.
|
2017-07-09 11:57:20 -04:00
|
|
|
func (p *Pipeline) FindLayout(base string, fm *map[string]interface{}) (tpl liquid.Template, err error) {
|
2017-06-16 15:14:56 -04:00
|
|
|
exts := []string{"", ".html"}
|
2017-06-24 13:30:01 -04:00
|
|
|
for _, ext := range strings.SplitN(p.config.MarkdownExt, `,`, -1) {
|
2017-06-16 15:14:56 -04:00
|
|
|
exts = append(exts, "."+ext)
|
|
|
|
}
|
|
|
|
var (
|
2017-06-30 21:06:12 -04:00
|
|
|
filename string
|
|
|
|
content []byte
|
|
|
|
found bool
|
2017-06-16 15:14:56 -04:00
|
|
|
)
|
|
|
|
for _, ext := range exts {
|
|
|
|
// TODO respect layout config
|
2017-06-30 21:06:12 -04:00
|
|
|
filename = filepath.Join(p.LayoutsDir(), base+ext)
|
|
|
|
content, err = ioutil.ReadFile(filename)
|
2017-06-16 15:14:56 -04:00
|
|
|
if err == nil {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2017-06-29 07:41:14 -04:00
|
|
|
return nil, fmt.Errorf("no template for %s", base)
|
2017-06-16 15:14:56 -04:00
|
|
|
}
|
2017-07-09 11:57:20 -04:00
|
|
|
lineNo := 1
|
|
|
|
*fm, err = frontmatter.Read(&content, &lineNo)
|
2017-06-16 15:14:56 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-07-09 11:57:20 -04:00
|
|
|
tpl, err = p.liquidEngine.ParseTemplate(content)
|
2017-07-04 17:13:47 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-09 11:57:20 -04:00
|
|
|
tpl.SetSourceLocation(filename, lineNo)
|
2017-07-04 17:13:47 -04:00
|
|
|
return
|
2017-06-24 13:30:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// LayoutsDir returns the path to the layouts directory.
|
|
|
|
func (p *Pipeline) LayoutsDir() string {
|
2017-06-30 21:06:12 -04:00
|
|
|
return filepath.Join(p.SourceDir(), p.config.LayoutsDir)
|
2017-06-16 15:14:56 -04:00
|
|
|
}
|