2017-07-06 14:07:53 +02:00
|
|
|
package render
|
|
|
|
|
2017-07-06 14:59:10 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
2017-07-07 11:41:37 +02:00
|
|
|
|
|
|
|
"github.com/osteele/liquid/parser"
|
2017-07-06 14:59:10 +02:00
|
|
|
)
|
|
|
|
|
2017-07-06 14:07:53 +02:00
|
|
|
// Compile parses a source template. It returns an AST root, that can be evaluated.
|
2017-07-12 12:58:45 +02:00
|
|
|
func (c Config) Compile(source string) (parser.ASTNode, parser.Error) {
|
2017-07-06 14:59:10 +02:00
|
|
|
root, err := c.Parse(source)
|
2017-07-06 14:07:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-06 14:59:10 +02:00
|
|
|
return c.compileNode(root)
|
2017-07-06 14:07:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// nolint: gocyclo
|
2017-07-12 12:58:45 +02:00
|
|
|
func (c Config) compileNode(n parser.ASTNode) (Node, parser.Error) {
|
2017-07-06 14:07:53 +02:00
|
|
|
switch n := n.(type) {
|
2017-07-07 11:41:37 +02:00
|
|
|
case *parser.ASTBlock:
|
2017-07-06 14:59:10 +02:00
|
|
|
body, err := c.compileNodes(n.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-07-06 14:07:53 +02:00
|
|
|
}
|
2017-07-06 14:59:10 +02:00
|
|
|
branches, err := c.compileBlocks(n.Branches)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cd, ok := c.findBlockDef(n.Name)
|
|
|
|
if !ok {
|
2017-07-12 12:58:45 +02:00
|
|
|
return nil, parser.Errorf(n, "undefined tag %q", n.Name)
|
2017-07-06 14:07:53 +02:00
|
|
|
}
|
2017-07-06 15:21:26 +02:00
|
|
|
node := BlockNode{
|
2017-07-09 17:18:35 +02:00
|
|
|
Token: n.Token,
|
2017-07-06 15:21:26 +02:00
|
|
|
Body: body,
|
|
|
|
Branches: branches,
|
|
|
|
}
|
2017-07-06 14:59:10 +02:00
|
|
|
if cd.parser != nil {
|
2017-07-06 15:21:26 +02:00
|
|
|
r, err := cd.parser(node)
|
2017-07-06 14:07:53 +02:00
|
|
|
if err != nil {
|
2017-07-12 12:58:45 +02:00
|
|
|
return nil, parser.WrapError(err, n)
|
2017-07-06 14:07:53 +02:00
|
|
|
}
|
2017-07-06 15:21:26 +02:00
|
|
|
node.renderer = r
|
2017-07-06 14:07:53 +02:00
|
|
|
}
|
2017-07-06 15:21:26 +02:00
|
|
|
return &node, nil
|
2017-07-07 11:41:37 +02:00
|
|
|
case *parser.ASTRaw:
|
2017-07-10 17:49:14 +02:00
|
|
|
return &RawNode{n.Slices, sourcelessNode{}}, nil
|
2017-07-07 11:41:37 +02:00
|
|
|
case *parser.ASTSeq:
|
2017-07-06 14:59:10 +02:00
|
|
|
children, err := c.compileNodes(n.Children)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-10 17:49:14 +02:00
|
|
|
return &SeqNode{children, sourcelessNode{}}, nil
|
2017-07-07 11:41:37 +02:00
|
|
|
case *parser.ASTTag:
|
2017-07-06 15:35:12 +02:00
|
|
|
if td, ok := c.FindTagDefinition(n.Name); ok {
|
|
|
|
f, err := td(n.Args)
|
|
|
|
if err != nil {
|
2017-07-12 12:58:45 +02:00
|
|
|
return nil, parser.Errorf(n, "%s", err)
|
2017-07-06 15:35:12 +02:00
|
|
|
}
|
2017-07-09 17:18:35 +02:00
|
|
|
return &TagNode{n.Token, f}, nil
|
2017-07-06 15:35:12 +02:00
|
|
|
}
|
2017-07-12 12:58:45 +02:00
|
|
|
return nil, parser.Errorf(n, "unknown tag: %s", n.Name)
|
2017-07-07 11:41:37 +02:00
|
|
|
case *parser.ASTText:
|
2017-07-09 17:18:35 +02:00
|
|
|
return &TextNode{n.Token}, nil
|
2017-07-07 11:41:37 +02:00
|
|
|
case *parser.ASTObject:
|
2017-07-09 17:18:35 +02:00
|
|
|
return &ObjectNode{n.Token, n.Expr}, nil
|
2017-07-06 14:59:10 +02:00
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("un-compilable node type %T", n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-12 12:58:45 +02:00
|
|
|
func (c Config) compileBlocks(blocks []*parser.ASTBlock) ([]*BlockNode, parser.Error) {
|
2017-07-06 15:21:26 +02:00
|
|
|
out := make([]*BlockNode, 0, len(blocks))
|
2017-07-06 14:59:10 +02:00
|
|
|
for _, child := range blocks {
|
|
|
|
compiled, err := c.compileNode(child)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-06 15:21:26 +02:00
|
|
|
out = append(out, compiled.(*BlockNode))
|
2017-07-06 14:59:10 +02:00
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2017-07-12 12:58:45 +02:00
|
|
|
func (c Config) compileNodes(nodes []parser.ASTNode) ([]Node, parser.Error) {
|
2017-07-06 15:21:26 +02:00
|
|
|
out := make([]Node, 0, len(nodes))
|
2017-07-06 14:59:10 +02:00
|
|
|
for _, child := range nodes {
|
|
|
|
compiled, err := c.compileNode(child)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-07-06 14:07:53 +02:00
|
|
|
}
|
2017-07-06 14:59:10 +02:00
|
|
|
out = append(out, compiled)
|
2017-07-06 14:07:53 +02:00
|
|
|
}
|
2017-07-06 14:59:10 +02:00
|
|
|
return out, nil
|
2017-07-06 14:07:53 +02:00
|
|
|
}
|