2017-06-16 21:14:56 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2017-06-17 01:34:39 +02:00
|
|
|
. "github.com/osteele/gojekyll/helpers"
|
|
|
|
|
2017-06-16 21:14:56 +02:00
|
|
|
"github.com/acstech/liquid"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FindLayout returns a template for the named layout.
|
|
|
|
func (s *Site) FindLayout(name string, fm *VariableMap) (t *liquid.Template, err error) {
|
|
|
|
exts := []string{"", ".html"}
|
|
|
|
for _, ext := range strings.SplitN(s.config.MarkdownExt, `,`, -1) {
|
|
|
|
exts = append(exts, "."+ext)
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
path string
|
|
|
|
content []byte
|
|
|
|
found bool
|
|
|
|
)
|
|
|
|
for _, ext := range exts {
|
|
|
|
// TODO respect layout config
|
|
|
|
path = filepath.Join(s.Source, "_layouts", name+ext)
|
|
|
|
content, err = ioutil.ReadFile(path)
|
|
|
|
if err == nil {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Errorf("no template for %s", name))
|
|
|
|
}
|
|
|
|
*fm, err = readFrontMatter(&content)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return liquid.Parse(content, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *DynamicPage) applyLayout(frontMatter VariableMap, body []byte) ([]byte, error) {
|
|
|
|
for {
|
|
|
|
layoutName := frontMatter.String("layout", "")
|
|
|
|
if layoutName == "" {
|
|
|
|
break
|
|
|
|
}
|
2017-06-16 22:44:09 +02:00
|
|
|
template, err := p.site.FindLayout(layoutName, &frontMatter)
|
2017-06-16 21:14:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-16 21:47:41 +02:00
|
|
|
vars := MergeVariableMaps(p.TemplateVariables(), VariableMap{
|
2017-06-16 21:14:56 +02:00
|
|
|
"content": body,
|
|
|
|
"layout": frontMatter,
|
|
|
|
})
|
2017-06-17 01:34:39 +02:00
|
|
|
body, err = RenderTemplate(template, vars)
|
2017-06-16 21:14:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return body, nil
|
|
|
|
}
|