2017-06-17 16:51:32 +02:00
|
|
|
package gojekyll
|
2017-06-16 21:14:56 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2017-06-17 18:31:03 +02:00
|
|
|
"github.com/osteele/gojekyll/liquid"
|
2017-06-16 21:14:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// FindLayout returns a template for the named layout.
|
2017-06-18 11:59:12 +02:00
|
|
|
func (s *Site) FindLayout(base string, fm *VariableMap) (t liquid.Template, err error) {
|
2017-06-16 21:14:56 +02:00
|
|
|
exts := []string{"", ".html"}
|
|
|
|
for _, ext := range strings.SplitN(s.config.MarkdownExt, `,`, -1) {
|
|
|
|
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-17 03:53:14 +02:00
|
|
|
name = filepath.Join(s.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-17 02:06:55 +02:00
|
|
|
panic(fmt.Errorf("no template for %s", base))
|
2017-06-16 21:14:56 +02:00
|
|
|
}
|
|
|
|
*fm, err = readFrontMatter(&content)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-06-18 21:37:21 +02:00
|
|
|
return s.LiquidEngine().Parse(content)
|
2017-06-16 21:14:56 +02:00
|
|
|
}
|
|
|
|
|
2017-06-19 04:26:49 +02:00
|
|
|
func (page *DynamicPage) applyLayout(frontMatter VariableMap, body []byte) ([]byte, error) {
|
2017-06-16 21:14:56 +02:00
|
|
|
for {
|
2017-06-19 04:26:49 +02:00
|
|
|
name := frontMatter.String("layout", "")
|
|
|
|
if name == "" {
|
|
|
|
return body, nil
|
2017-06-16 21:14:56 +02:00
|
|
|
}
|
2017-06-19 04:26:49 +02:00
|
|
|
template, err := page.site.FindLayout(name, &frontMatter)
|
2017-06-16 21:14:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-20 22:17:59 +02:00
|
|
|
vars := MergeVariableMaps(page.Variables(), VariableMap{
|
2017-06-18 21:37:21 +02:00
|
|
|
"content": string(body),
|
2017-06-16 21:14:56 +02:00
|
|
|
"layout": frontMatter,
|
|
|
|
})
|
2017-06-18 11:59:12 +02:00
|
|
|
body, err = template.Render(vars)
|
2017-06-16 21:14:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|