Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
osteele committed Jul 7, 2017
1 parent 903acb8 commit c599761
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 63 deletions.
40 changes: 32 additions & 8 deletions engine.go
Expand Up @@ -8,19 +8,43 @@ import (
"github.com/osteele/liquid/tags"
)

type engine struct{ settings render.Config }
// An Engine parses template source into renderable text.
//
// An engine can be configured with additional filters and tags.
//
// Filters
//
// RegisterFilter defines a Liquid filter.
//
// A filter is any function that takes at least one input, and returns one or two outputs.
// If it returns two outputs, the second must be an error.
type Engine interface {
// RegisterFilter defines a filter function e.g. {{ value | filter: arg }}.
RegisterFilter(name string, fn interface{})
// RegisterTag defines a tag function e.g. {% tag %}.
RegisterTag(string, Renderer)
RegisterBlock(string, Renderer)

ParseTemplate([]byte) (Template, error)
// ParseAndRender parses and then renders the template.
ParseAndRender([]byte, Bindings) ([]byte, error)
// ParseAndRenderString is a convenience wrapper for ParseAndRender, that has string input and output.
ParseAndRenderString(string, Bindings) (string, error)
}

type engine struct{ cfg render.Config }

// NewEngine returns a new template engine.
func NewEngine() Engine {
e := engine{render.NewConfig()}
filters.AddStandardFilters(&e.settings.Config.Config)
tags.AddStandardTags(e.settings)
filters.AddStandardFilters(&e.cfg.Config.Config)
tags.AddStandardTags(e.cfg)
return e
}

// RegisterBlock is in the Engine interface.
func (e engine) RegisterBlock(name string, td Renderer) {
e.settings.AddBlock(name).Renderer(func(w io.Writer, ctx render.Context) error {
e.cfg.AddBlock(name).Renderer(func(w io.Writer, ctx render.Context) error {
s, err := td(ctx)
if err != nil {
return err
Expand All @@ -32,14 +56,14 @@ func (e engine) RegisterBlock(name string, td Renderer) {

// RegisterFilter is in the Engine interface.
func (e engine) RegisterFilter(name string, fn interface{}) {
e.settings.AddFilter(name, fn)
e.cfg.AddFilter(name, fn)
}

// RegisterTag is in the Engine 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, render.Context) error, error) {
e.cfg.AddTag(name, func(_ string) (func(io.Writer, render.Context) error, error) {
return func(w io.Writer, ctx render.Context) error {
s, err := td(ctx)
if err != nil {
Expand All @@ -53,11 +77,11 @@ func (e engine) RegisterTag(name string, td Renderer) {

// ParseTemplate is in the Engine interface.
func (e engine) ParseTemplate(text []byte) (Template, error) {
ast, err := e.settings.Compile(string(text))
ast, err := e.cfg.Compile(string(text))
if err != nil {
return nil, err
}
return &template{ast, &e.settings}, nil
return &template{ast, &e.cfg}, nil
}

// ParseAndRender is in the Engine interface.
Expand Down
4 changes: 2 additions & 2 deletions expression/config.go
Expand Up @@ -2,10 +2,10 @@ package expression

// Config holds configuration information for expression interpretation.
type Config struct {
filterDictionary
filters map[string]interface{}
}

// NewConfig creates a new Settings.
func NewConfig() Config {
return Config{newFilterDictionary()}
return Config{}
}
16 changes: 4 additions & 12 deletions expression/filters.go
Expand Up @@ -23,16 +23,8 @@ func (e UndefinedFilter) Error() string {

type valueFn func(Context) interface{}

type filterDictionary struct {
filters map[string]interface{}
}

func newFilterDictionary() filterDictionary {
return filterDictionary{map[string]interface{}{}}
}

// AddFilter adds a filter to the filter dictionary.
func (d *filterDictionary) AddFilter(name string, fn interface{}) {
func (c *Config) AddFilter(name string, fn interface{}) {
rf := reflect.ValueOf(fn)
switch {
case rf.Kind() != reflect.Func:
Expand All @@ -44,10 +36,10 @@ func (d *filterDictionary) AddFilter(name string, fn interface{}) {
// case rf.Type().Out(1).Implements(…):
// panic(typeError("a filter's second output must be type error"))
}
if len(d.filters) == 0 {
d.filters = make(map[string]interface{})
if len(c.filters) == 0 {
c.filters = make(map[string]interface{})
}
d.filters[name] = fn
c.filters[name] = fn
}

var closureType = reflect.TypeOf(closure{})
Expand Down
33 changes: 0 additions & 33 deletions liquid.go
Expand Up @@ -15,39 +15,6 @@ import (
"github.com/osteele/liquid/render"
)

// An Engine parses template source into renderable text.
//
// An engine can be configured with additional filters and tags.
//
// Filters
//
// RegisterFilter defines a Liquid filter.
//
// A filter is any function that takes at least one input, and returns one or two outputs.
// If it returns two outputs, the second must be an error.
type Engine interface {
// RegisterFilter defines a filter function e.g. {{ value | filter: arg }}.
RegisterFilter(name string, fn interface{})
// RegisterTag defines a tag function e.g. {% tag %}.
RegisterTag(string, Renderer)
RegisterBlock(string, Renderer)

ParseTemplate([]byte) (Template, error)
// ParseAndRender parses and then renders the template.
ParseAndRender([]byte, Bindings) ([]byte, error)
// ParseAndRenderString is a convenience wrapper for ParseAndRender, that has string input and output.
ParseAndRenderString(string, Bindings) (string, error)
}

// A Template renders a template according to scope.
type Template interface {
// Render executes the template with the specified bindings.
Render(Bindings) ([]byte, error)
// RenderString is a convenience wrapper for Render, that has string input and output.
RenderString(Bindings) (string, error)
SetSourcePath(string)
}

// Bindings is a map of variable names to values.
type Bindings map[string]interface{}

Expand Down
3 changes: 1 addition & 2 deletions parser/config.go
Expand Up @@ -5,11 +5,10 @@ import "github.com/osteele/liquid/expression"
// // Config holds configuration information for parsing and rendering.
type Config struct {
expression.Config
// Filename string
Grammar Grammar
}

// NewConfig creates a new Settings.
func NewConfig() Config {
return Config{Config: expression.NewConfig()}
return Config{}
}
6 changes: 0 additions & 6 deletions render/config.go
Expand Up @@ -15,15 +15,9 @@ type Config struct {
// NewConfig creates a new Settings.
func NewConfig() Config {
c := Config{
// Config: parser.NewConfig(),
tags: map[string]TagCompiler{},
blockDefs: map[string]*blockSyntax{},
}
c.Grammar = c
return c
}

// AddFilter adds a filter to settings.
// func (s Config) AddFilter(name string, fn interface{}) {
// s.Config.AddFilter(name, fn)
// }
9 changes: 9 additions & 0 deletions template.go
Expand Up @@ -6,6 +6,15 @@ import (
"github.com/osteele/liquid/render"
)

// A Template renders a template according to scope.
type Template interface {
// Render executes the template with the specified bindings.
Render(Bindings) ([]byte, error)
// RenderString is a convenience wrapper for Render, that has string input and output.
RenderString(Bindings) (string, error)
SetSourcePath(string)
}

type template struct {
root render.Node
config *render.Config
Expand Down

0 comments on commit c599761

Please sign in to comment.