Skip to content

Commit

Permalink
Replace GetVariableMap -> UpdateBindings, RenderFile
Browse files Browse the repository at this point in the history
  • Loading branch information
osteele committed Jul 1, 2017
1 parent 7c48138 commit a7cbb9b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
20 changes: 14 additions & 6 deletions chunks/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ import (

// Context is the evaluation context for chunk AST rendering.
type Context struct {
vars map[string]interface{}
bindings map[string]interface{}
settings Settings
}

type Settings struct {
ExpressionSettings expressions.Settings
tags map[string]TagDefinition
controlTags map[string]*controlTagDefinition
tags map[string]TagDefinition
controlTags map[string]*controlTagDefinition
}

func (s Settings) AddFilter(name string, fn interface{}) {
s.ExpressionSettings.AddFilter(name, fn)
}

func NewSettings() Settings {
s:= Settings{
s := Settings{
expressions.NewSettings(),
map[string]TagDefinition{},
map[string]*controlTagDefinition{},
}
s.AddTag( "assign", assignTagDef)
s.AddTag("assign", assignTagDef)
return s
}

Expand All @@ -41,6 +41,14 @@ func NewContext(scope map[string]interface{}, s Settings) Context {
return Context{vars, s}
}

func (c Context) Clone() Context {
bindings := map[string]interface{}{}
for k, v := range c.bindings {
bindings[k] = v
}
return Context{bindings, c.settings}
}

// Evaluate evaluates an expression within the template context.
func (c Context) Evaluate(expr expressions.Expression) (out interface{}, err error) {
defer func() {
Expand All @@ -54,5 +62,5 @@ func (c Context) Evaluate(expr expressions.Expression) (out interface{}, err err
}
}
}()
return expr.Evaluate(expressions.NewContext(c.vars, c.settings.ExpressionSettings))
return expr.Evaluate(expressions.NewContext(c.bindings, c.settings.ExpressionSettings))
}
39 changes: 29 additions & 10 deletions chunks/render_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"strings"

"github.com/osteele/liquid/expressions"
)

// RenderContext provides the rendering context for a tag renderer.
type RenderContext interface {
Clone() RenderContext
Get(name string) interface{}
Set(name string, value interface{})
GetVariableMap() map[string]interface{}
Evaluate(expr expressions.Expression) (interface{}, error)
EvaluateString(source string) (interface{}, error)
EvaluateStatement(tag, source string) (interface{}, error)
InnerString() (string, error)
ParseTagArgs() (string, error)
RenderChild(io.Writer, *ASTControlTag) error
RenderChildren(io.Writer) error
RenderFile(w io.Writer, filename string) error
UpdateBindings(map[string]interface{})
}

type renderContext struct {
Expand All @@ -29,6 +32,10 @@ type renderContext struct {
cn *ASTControlTag
}

func (c renderContext) Clone() RenderContext {
return renderContext{c.ctx.Clone(), c.node, c.cn}
}

// Evaluate evaluates an expression within the template context.
func (c renderContext) Evaluate(expr expressions.Expression) (out interface{}, err error) {
return c.ctx.Evaluate(expr)
Expand All @@ -47,27 +54,21 @@ func (c renderContext) EvaluateString(source string) (out interface{}, err error
}
}
}()
return expressions.EvaluateString(source, expressions.NewContext(c.ctx.vars, c.ctx.settings.ExpressionSettings))
return expressions.EvaluateString(source, expressions.NewContext(c.ctx.bindings, c.ctx.settings.ExpressionSettings))
}

func (c renderContext) EvaluateStatement(tag, source string) (interface{}, error) {
return c.EvaluateString(fmt.Sprintf("%%%s %s", tag, source))
}

// GetVariableMap returns the variable map. This is required by some tangled code
// in Jekyll includes, that should hopefully get better.
func (c renderContext) GetVariableMap() map[string]interface{} {
return c.ctx.vars
}

// Get gets a variable value within an evaluation context.
func (c renderContext) Get(name string) interface{} {
return c.ctx.vars[name]
return c.ctx.bindings[name]
}

// Set sets a variable value from an evaluation context.
func (c renderContext) Set(name string, value interface{}) {
c.ctx.vars[name] = value
c.ctx.bindings[name] = value
}

// RenderChild renders a node.
Expand All @@ -83,6 +84,18 @@ func (c renderContext) RenderChildren(w io.Writer) error {
return c.ctx.RenderASTSequence(w, c.cn.Body)
}

func (c renderContext) RenderFile(w io.Writer, filename string) error {
source, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
ast, err := c.ctx.settings.Parse(string(source))
if err != nil {
return err
}
return ast.Render(w, c.ctx)
}

// InnerString renders the children to a string.
func (c renderContext) InnerString() (string, error) {
buf := new(bytes.Buffer)
Expand Down Expand Up @@ -114,3 +127,9 @@ func (c renderContext) ParseTagArgs() (string, error) {
}
return args, nil
}

func (c renderContext) UpdateBindings(bindings map[string]interface{}) {
for k, v := range bindings {
c.ctx.bindings[k] = v
}
}
9 changes: 4 additions & 5 deletions expressions/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ type Context interface {
}

type context struct {
vars map[string]interface{}
copied bool
bindings map[string]interface{}
Settings
}

Expand All @@ -27,7 +26,7 @@ func (s Settings) AddFilter(name string, fn interface{}) {

// NewContext makes a new expression evaluation context.
func NewContext(vars map[string]interface{}, s Settings) Context {
return &context{vars, false, s}
return &context{vars, s}
}

func (c *context) Filters() *FilterDictionary {
Expand All @@ -36,10 +35,10 @@ func (c *context) Filters() *FilterDictionary {

// Get looks up a variable value in the expression context.
func (c *context) Get(name string) interface{} {
return c.vars[name]
return c.bindings[name]
}

// Set sets a variable value in the expression context.
func (c *context) Set(name string, value interface{}) {
c.vars[name] = value
c.bindings[name] = value
}

0 comments on commit a7cbb9b

Please sign in to comment.