1
0
mirror of https://github.com/danog/liquid.git synced 2025-01-22 13:53:09 +01:00

Move chunks -> render

This commit is contained in:
Oliver Steele 2017-07-04 11:03:18 -04:00
parent 05597307bd
commit 6161e6df28
21 changed files with 52 additions and 52 deletions

View File

@ -3,16 +3,16 @@ package liquid
import (
"io"
"github.com/osteele/liquid/chunks"
"github.com/osteele/liquid/filters"
"github.com/osteele/liquid/render"
"github.com/osteele/liquid/tags"
)
type engine struct{ settings chunks.Settings }
type engine struct{ settings render.Settings }
// NewEngine returns a new template engine.
func NewEngine() Engine {
e := engine{chunks.NewSettings()}
e := engine{render.NewSettings()}
filters.AddStandardFilters(e.settings.ExpressionSettings)
tags.AddStandardTags(e.settings)
return e
@ -20,7 +20,7 @@ func NewEngine() Engine {
// RegisterBlock is in the Engine interface.
func (e engine) RegisterBlock(name string, td Renderer) {
e.settings.AddBlock(name).Renderer(func(w io.Writer, ctx chunks.RenderContext) error {
e.settings.AddBlock(name).Renderer(func(w io.Writer, ctx render.RenderContext) error {
s, err := td(ctx)
if err != nil {
return err
@ -39,8 +39,8 @@ func (e engine) RegisterFilter(name string, fn interface{}) {
func (e engine) RegisterTag(name string, td Renderer) {
// For simplicity, don't expose the two stage parsing/rendering process to clients.
// Client tags do everything at runtime.
e.settings.AddTag(name, func(_ string) (func(io.Writer, chunks.RenderContext) error, error) {
return func(w io.Writer, ctx chunks.RenderContext) error {
e.settings.AddTag(name, func(_ string) (func(io.Writer, render.RenderContext) error, error) {
return func(w io.Writer, ctx render.RenderContext) error {
s, err := td(ctx)
if err != nil {
return err

View File

@ -6,7 +6,7 @@ import (
"strings"
"testing"
"github.com/osteele/liquid/chunks"
"github.com/osteele/liquid/render"
"github.com/stretchr/testify/require"
)
@ -80,7 +80,7 @@ func Example_register_filter() {
func Example_register_tag() {
engine := NewEngine()
engine.RegisterTag("echo", func(c chunks.RenderContext) (string, error) {
engine.RegisterTag("echo", func(c render.RenderContext) (string, error) {
return c.TagArgs(), nil
})
@ -96,7 +96,7 @@ func Example_register_tag() {
func Example_register_block() {
engine := NewEngine()
engine.RegisterBlock("length", func(c chunks.RenderContext) (string, error) {
engine.RegisterBlock("length", func(c render.RenderContext) (string, error) {
s, err := c.InnerString()
if err != nil {
return "", err

View File

@ -6,7 +6,7 @@ See the project README https://github.com/osteele/liquid for additional informat
package liquid
import (
"github.com/osteele/liquid/chunks"
"github.com/osteele/liquid/render"
)
// Engine parses template source into renderable text.
@ -51,4 +51,4 @@ type Bindings map[string]interface{}
// type TagParser func(chunks.RenderContext) (string, error)
// Renderer returns the rendered string for a block.
type Renderer func(chunks.RenderContext) (string, error)
type Renderer func(render.RenderContext) (string, error)

View File

@ -1,4 +1,4 @@
package chunks
package render
import (
"io"

View File

@ -1,4 +1,4 @@
package chunks
package render
import (
"fmt"

View File

@ -2,7 +2,7 @@
//
// A template string is composed of a sequence of chunk.
// Each chunk is either an object {{a.b}}, a tag {%if a>b%}, or a text chunk (anything outside of {{}} and {%%}.)
package chunks
package render
import "fmt"

View File

@ -1,6 +1,6 @@
// Code generated by "stringer -type=ChunkType"; DO NOT EDIT.
package chunks
package render
import "fmt"

View File

@ -1,4 +1,4 @@
package chunks
package render
import (
"github.com/osteele/liquid/expressions"

View File

@ -1,4 +1,4 @@
package chunks
package render
import (
"fmt"

View File

@ -1,4 +1,4 @@
package chunks
package render
import (
"fmt"

View File

@ -1,4 +1,4 @@
package chunks
package render
import (
"fmt"

View File

@ -1,4 +1,4 @@
package chunks
package render
import (
"fmt"

View File

@ -1,4 +1,4 @@
package chunks
package render
import (
"bytes"

View File

@ -1,4 +1,4 @@
package chunks
package render
import (
"bytes"

View File

@ -1,4 +1,4 @@
package chunks
package render
import (
"regexp"

View File

@ -1,4 +1,4 @@
package chunks
package render
import (
"fmt"

View File

@ -1,4 +1,4 @@
package chunks
package render
import (
"io"

View File

@ -5,21 +5,21 @@ import (
"io"
"reflect"
"github.com/osteele/liquid/chunks"
"github.com/osteele/liquid/expressions"
"github.com/osteele/liquid/render"
)
var errLoopContinueLoop = fmt.Errorf("continue outside a loop")
var errLoopBreak = fmt.Errorf("break outside a loop")
func breakTag(parameters string) (func(io.Writer, chunks.RenderContext) error, error) {
return func(io.Writer, chunks.RenderContext) error {
func breakTag(parameters string) (func(io.Writer, render.RenderContext) error, error) {
return func(io.Writer, render.RenderContext) error {
return errLoopBreak
}, nil
}
func continueTag(parameters string) (func(io.Writer, chunks.RenderContext) error, error) {
return func(io.Writer, chunks.RenderContext) error {
func continueTag(parameters string) (func(io.Writer, render.RenderContext) error, error) {
return func(io.Writer, render.RenderContext) error {
return errLoopContinueLoop
}, nil
}
@ -32,12 +32,12 @@ func parseLoopExpression(source string) (expressions.Expression, error) {
return expr, nil
}
func loopTagParser(node chunks.ASTBlock) (func(io.Writer, chunks.RenderContext) error, error) { // nolint: gocyclo
func loopTagParser(node render.ASTBlock) (func(io.Writer, render.RenderContext) error, error) { // nolint: gocyclo
expr, err := parseLoopExpression(node.Args)
if err != nil {
return nil, err
}
return func(w io.Writer, ctx chunks.RenderContext) error {
return func(w io.Writer, ctx render.RenderContext) error {
val, err := ctx.Evaluate(expr)
if err != nil {
return err

View File

@ -4,13 +4,13 @@ package tags
import (
"io"
"github.com/osteele/liquid/chunks"
"github.com/osteele/liquid/expressions"
"github.com/osteele/liquid/generics"
"github.com/osteele/liquid/render"
)
// AddStandardTags defines the standard Liquid tags.
func AddStandardTags(settings chunks.Settings) {
func AddStandardTags(settings render.Settings) {
// The parser only recognize the comment and raw tags if they've been defined,
// but it ignores any syntax specified here.
loopTags := []string{"break", "continue", "cycle"}
@ -26,10 +26,10 @@ func AddStandardTags(settings chunks.Settings) {
settings.AddBlock("unless").SameSyntaxAs("if").Parser(ifTagParser(false))
}
func captureTagParser(node chunks.ASTBlock) (func(io.Writer, chunks.RenderContext) error, error) {
func captureTagParser(node render.ASTBlock) (func(io.Writer, render.RenderContext) error, error) {
// TODO verify syntax
varname := node.Args
return func(w io.Writer, ctx chunks.RenderContext) error {
return func(w io.Writer, ctx render.RenderContext) error {
s, err := ctx.InnerString()
if err != nil {
return err
@ -39,7 +39,7 @@ func captureTagParser(node chunks.ASTBlock) (func(io.Writer, chunks.RenderContex
}, nil
}
func caseTagParser(node chunks.ASTBlock) (func(io.Writer, chunks.RenderContext) error, error) {
func caseTagParser(node render.ASTBlock) (func(io.Writer, render.RenderContext) error, error) {
// TODO parse error on non-empty node.Body
// TODO case can include an else
expr, err := expressions.Parse(node.Args)
@ -48,7 +48,7 @@ func caseTagParser(node chunks.ASTBlock) (func(io.Writer, chunks.RenderContext)
}
type caseRec struct {
expr expressions.Expression
node *chunks.ASTBlock
node *render.ASTBlock
}
cases := []caseRec{}
for _, branch := range node.Branches {
@ -58,7 +58,7 @@ func caseTagParser(node chunks.ASTBlock) (func(io.Writer, chunks.RenderContext)
}
cases = append(cases, caseRec{bfn, branch})
}
return func(w io.Writer, ctx chunks.RenderContext) error {
return func(w io.Writer, ctx render.RenderContext) error {
value, err := ctx.Evaluate(expr)
if err != nil {
return err
@ -76,11 +76,11 @@ func caseTagParser(node chunks.ASTBlock) (func(io.Writer, chunks.RenderContext)
}, nil
}
func ifTagParser(polarity bool) func(chunks.ASTBlock) (func(io.Writer, chunks.RenderContext) error, error) { // nolint: gocyclo
return func(node chunks.ASTBlock) (func(io.Writer, chunks.RenderContext) error, error) {
func ifTagParser(polarity bool) func(render.ASTBlock) (func(io.Writer, render.RenderContext) error, error) { // nolint: gocyclo
return func(node render.ASTBlock) (func(io.Writer, render.RenderContext) error, error) {
type branchRec struct {
test expressions.Expression
body *chunks.ASTBlock
body *render.ASTBlock
}
expr, err := expressions.Parse(node.Args)
if err != nil {
@ -107,7 +107,7 @@ func ifTagParser(polarity bool) func(chunks.ASTBlock) (func(io.Writer, chunks.Re
}
branches = append(branches, branchRec{test, c})
}
return func(w io.Writer, ctx chunks.RenderContext) error {
return func(w io.Writer, ctx render.RenderContext) error {
for _, b := range branches {
value, err := ctx.Evaluate(b.test)
if err != nil {

View File

@ -5,7 +5,7 @@ import (
"fmt"
"testing"
"github.com/osteele/liquid/chunks"
"github.com/osteele/liquid/render"
"github.com/stretchr/testify/require"
)
@ -133,7 +133,7 @@ var bindings = map[string]interface{}{
}
func TestParseErrors(t *testing.T) {
settings := chunks.NewSettings()
settings := render.NewSettings()
AddStandardTags(settings)
for i, test := range parseErrorTests {
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
@ -145,15 +145,15 @@ func TestParseErrors(t *testing.T) {
}
}
func TestRender(t *testing.T) {
settings := chunks.NewSettings()
settings := render.NewSettings()
AddStandardTags(settings)
context := chunks.NewContext(bindings, settings)
context := render.NewContext(bindings, settings)
for i, test := range tagTests {
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
ast, err := settings.Parse(test.in)
require.NoErrorf(t, err, test.in)
buf := new(bytes.Buffer)
err = chunks.Render(ast, buf, context)
err = render.Render(ast, buf, context)
require.NoErrorf(t, err, test.in)
require.Equalf(t, test.expected, buf.String(), test.in)
})

View File

@ -3,18 +3,18 @@ package liquid
import (
"bytes"
"github.com/osteele/liquid/chunks"
"github.com/osteele/liquid/render"
)
type template struct {
ast chunks.ASTNode
settings chunks.Settings
ast render.ASTNode
settings render.Settings
}
// Render executes the template within the bindings environment.
func (t *template) Render(b Bindings) ([]byte, error) {
buf := new(bytes.Buffer)
err := chunks.Render(t.ast, buf, chunks.NewContext(b, t.settings))
err := render.Render(t.ast, buf, render.NewContext(b, t.settings))
if err != nil {
return nil, err
}