mirror of
https://github.com/danog/gojekyll.git
synced 2025-01-22 20:51:24 +01:00
Apply layouts (w/out inheritance)
This commit is contained in:
parent
3263183272
commit
1f80b485d2
51
page.go
51
page.go
@ -84,7 +84,6 @@ func ReadPage(path string, defaults VariableMap) (p Page, err error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
println(path, pattern, permalink)
|
||||
p.setPermalink(permalink)
|
||||
}
|
||||
return
|
||||
@ -242,28 +241,58 @@ func (p *DynamicPage) DebugVariables() VariableMap {
|
||||
return p.TemplateVariables()
|
||||
}
|
||||
|
||||
// Write applies Liquid and Markdown, as appropriate.
|
||||
func (p *DynamicPage) Write(w io.Writer) error {
|
||||
parsingLiquid := true
|
||||
// renderTemplate is a wrapper around liquid template.Render that turns panics into errors
|
||||
func renderTemplate(template *liquid.Template, variables VariableMap) (bs []byte, err error) {
|
||||
defer func() {
|
||||
if parsingLiquid {
|
||||
fmt.Println("While processing", p.Source())
|
||||
if r := recover(); r != nil {
|
||||
if e, ok := r.(error); ok {
|
||||
err = e
|
||||
} else {
|
||||
panic(r)
|
||||
}
|
||||
}
|
||||
}()
|
||||
template, err := liquid.Parse(p.Content, nil)
|
||||
parsingLiquid = false
|
||||
writer := new(bytes.Buffer)
|
||||
template.Render(writer, variables)
|
||||
return writer.Bytes(), nil
|
||||
}
|
||||
|
||||
// applyTemplate parses and then renders the template.
|
||||
func parseAndApplyTemplate(bs []byte, variables VariableMap) ([]byte, error) {
|
||||
template, err := liquid.Parse(bs, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return renderTemplate(template, variables)
|
||||
}
|
||||
|
||||
// Write applies Liquid and Markdown, as appropriate.
|
||||
func (p *DynamicPage) Write(w io.Writer) error {
|
||||
body, err := parseAndApplyTemplate(p.Content, p.TemplateVariables())
|
||||
if err != nil {
|
||||
err := &os.PathError{Op: "Liquid Error", Path: p.Source(), Err: err}
|
||||
return err
|
||||
}
|
||||
writer := new(bytes.Buffer)
|
||||
template.Render(writer, p.TemplateVariables())
|
||||
body := writer.Bytes()
|
||||
|
||||
if isMarkdown(p.path) {
|
||||
body = blackfriday.MarkdownCommon(body)
|
||||
}
|
||||
|
||||
layout := p.frontMatter.String("layout", "")
|
||||
if layout != "" {
|
||||
layout, err := site.FindLayout(layout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
vars := mergeVariableMaps(p.TemplateVariables(), VariableMap{
|
||||
"content": body,
|
||||
})
|
||||
body, err = renderTemplate(layout, vars)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
_, err = w.Write(body)
|
||||
return err
|
||||
}
|
||||
|
34
site.go
34
site.go
@ -1,11 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/acstech/liquid"
|
||||
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
@ -113,13 +116,42 @@ func (s *Site) readConfigBytes(bytes []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindLayout returns a template for the named layout.
|
||||
func (s *Site) FindLayout(name string) (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))
|
||||
}
|
||||
return liquid.Parse(content, nil)
|
||||
}
|
||||
|
||||
// KeepFile returns a boolean indicating that clean should leave the file in the destination directory.
|
||||
func (s *Site) KeepFile(path string) bool {
|
||||
// TODO
|
||||
return false
|
||||
}
|
||||
|
||||
// MarkdownExtensions returns a set of markdown extension.
|
||||
// MarkdownExtensions returns a set of markdown extension, without the final dots.
|
||||
func (s *Site) MarkdownExtensions() map[string]bool {
|
||||
extns := strings.SplitN(s.config.MarkdownExt, `,`, -1)
|
||||
return stringArrayToMap(extns)
|
||||
|
3
test/_layouts/default.html
Normal file
3
test/_layouts/default.html
Normal file
@ -0,0 +1,3 @@
|
||||
<h1>{{page.title}}</h1>
|
||||
{{content}}
|
||||
<footer/>
|
@ -1,8 +1,9 @@
|
||||
---
|
||||
permalink: /:name
|
||||
layout: default
|
||||
---
|
||||
|
||||
# Archive
|
||||
## Pages
|
||||
|
||||
{% assign pages = site.c1 | sort: 'weight' %}
|
||||
{% for p in pages %}
|
||||
|
Loading…
x
Reference in New Issue
Block a user