2017-06-26 16:36:53 +02:00
|
|
|
/*
|
|
|
|
Package liquid is a very early-stage pure Go library that implements Shopify Liquid <https://shopify.github.io/liquid> templates.
|
|
|
|
|
|
|
|
It's intended for use in for use in https://github.com/osteele/gojekyll.
|
|
|
|
*/
|
2017-06-26 16:15:01 +02:00
|
|
|
package liquid
|
|
|
|
|
|
|
|
import (
|
2017-06-27 13:43:42 +02:00
|
|
|
"io"
|
2017-06-26 16:15:01 +02:00
|
|
|
|
|
|
|
"github.com/osteele/liquid/chunks"
|
|
|
|
)
|
|
|
|
|
2017-06-26 16:36:53 +02:00
|
|
|
// Engine parses template source into renderable text.
|
|
|
|
//
|
|
|
|
// In the future, it will be configured with additional tags, filters, and the {%include%} search path.
|
2017-06-26 16:15:01 +02:00
|
|
|
type Engine interface {
|
2017-06-27 13:43:42 +02:00
|
|
|
DefineTag(string, func(form string) (func(io.Writer, chunks.Context) error, error))
|
|
|
|
|
2017-06-26 21:36:05 +02:00
|
|
|
ParseTemplate(text []byte) (Template, error)
|
2017-06-26 16:15:01 +02:00
|
|
|
ParseAndRender(text []byte, scope map[string]interface{}) ([]byte, error)
|
2017-06-26 16:36:53 +02:00
|
|
|
ParseAndRenderString(text string, scope map[string]interface{}) (string, error)
|
2017-06-26 16:15:01 +02:00
|
|
|
}
|
|
|
|
|
2017-06-27 13:43:42 +02:00
|
|
|
type TagDefinition func(expr string) (func(io.Writer, chunks.Context) error, error)
|
2017-06-26 16:15:01 +02:00
|
|
|
|
|
|
|
type engine struct{}
|
|
|
|
|
|
|
|
type template struct {
|
2017-06-26 18:41:41 +02:00
|
|
|
ast chunks.ASTNode
|
2017-06-26 16:15:01 +02:00
|
|
|
}
|
|
|
|
|
2017-06-26 16:36:53 +02:00
|
|
|
// NewEngine makes a new engine.
|
2017-06-26 16:15:01 +02:00
|
|
|
func NewEngine() Engine {
|
|
|
|
return engine{}
|
|
|
|
}
|
|
|
|
|
2017-06-27 13:43:42 +02:00
|
|
|
func (e engine) DefineTag(name string, td func(form string) (func(io.Writer, chunks.Context) error, error)) {
|
|
|
|
chunks.DefineTag(name, chunks.TagDefinition(td))
|
|
|
|
}
|
|
|
|
|
2017-06-26 21:36:05 +02:00
|
|
|
func (e engine) ParseTemplate(text []byte) (Template, error) {
|
2017-06-26 16:15:01 +02:00
|
|
|
tokens := chunks.Scan(string(text), "")
|
|
|
|
ast, err := chunks.Parse(tokens)
|
2017-06-26 21:36:05 +02:00
|
|
|
// fmt.Println(chunks.MustYAML(ast))
|
2017-06-26 16:15:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &template{ast}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseAndRender parses and then renders the template.
|
|
|
|
func (e engine) ParseAndRender(text []byte, scope map[string]interface{}) ([]byte, error) {
|
2017-06-26 21:36:05 +02:00
|
|
|
t, err := e.ParseTemplate(text)
|
2017-06-26 16:15:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return t.Render(scope)
|
|
|
|
}
|
|
|
|
|
2017-06-26 16:36:53 +02:00
|
|
|
// ParseAndRenderString is a convenience wrapper for ParseAndRender, that has string input and output.
|
|
|
|
func (e engine) ParseAndRenderString(text string, scope map[string]interface{}) (string, error) {
|
|
|
|
b, err := e.ParseAndRender([]byte(text), scope)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(b), nil
|
2017-06-26 16:15:01 +02:00
|
|
|
}
|