mirror of
https://github.com/danog/liquid.git
synced 2025-01-22 12:51:23 +01:00
Create top-level interface to liquid package
This commit is contained in:
parent
b367592cd3
commit
514559e442
@ -48,7 +48,7 @@ var chunkTestContext = Context{map[string]interface{}{
|
||||
func TestChunkParser(t *testing.T) {
|
||||
for i, test := range chunkTests {
|
||||
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
|
||||
tokens := ScanChunks(test.in, "")
|
||||
tokens := Scan(test.in, "")
|
||||
// fmt.Println(tokens)
|
||||
ast, err := Parse(tokens)
|
||||
require.NoErrorf(t, err, test.in)
|
||||
|
@ -42,7 +42,7 @@ func (c Chunk) MarshalYAML() (interface{}, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func ScanChunks(data string, pathname string) []Chunk {
|
||||
func Scan(data string, pathname string) []Chunk {
|
||||
var (
|
||||
sourceInfo = SourceInfo{pathname, 0}
|
||||
out = make([]Chunk, 0)
|
||||
|
57
liquid.go
Normal file
57
liquid.go
Normal file
@ -0,0 +1,57 @@
|
||||
package liquid
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/osteele/liquid/chunks"
|
||||
)
|
||||
|
||||
type Engine interface {
|
||||
ParseAndRender(text []byte, scope map[string]interface{}) ([]byte, error)
|
||||
ParseAndRenderString(text string, scope map[string]interface{}) ([]byte, error)
|
||||
}
|
||||
|
||||
type Template interface {
|
||||
Render(scope map[string]interface{}) ([]byte, error)
|
||||
}
|
||||
|
||||
type engine struct{}
|
||||
|
||||
type template struct {
|
||||
ast chunks.AST
|
||||
}
|
||||
|
||||
func NewEngine() Engine {
|
||||
return engine{}
|
||||
}
|
||||
|
||||
func (e engine) Parse(text []byte) (Template, error) {
|
||||
tokens := chunks.Scan(string(text), "")
|
||||
ast, err := chunks.Parse(tokens)
|
||||
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) {
|
||||
t, err := e.Parse(text)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return t.Render(scope)
|
||||
}
|
||||
|
||||
func (e engine) ParseAndRenderString(text string, scope map[string]interface{}) ([]byte, error) {
|
||||
return e.ParseAndRender([]byte(text), scope)
|
||||
}
|
||||
|
||||
func (t *template) Render(scope map[string]interface{}) ([]byte, error) {
|
||||
buf := new(bytes.Buffer)
|
||||
err := t.ast.Render(buf, chunks.Context{scope})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
32
liquid_test.go
Normal file
32
liquid_test.go
Normal file
@ -0,0 +1,32 @@
|
||||
package liquid
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var liquidTests = []struct{ in, expected string }{
|
||||
{"{{page.title}}", "Introduction"},
|
||||
{"{%if x%}true{%endif%}", "true"},
|
||||
}
|
||||
|
||||
var liquidTestScope = map[string]interface{}{
|
||||
"x": 123,
|
||||
"ar": []string{"first", "second", "third"},
|
||||
"page": map[string]interface{}{
|
||||
"title": "Introduction",
|
||||
},
|
||||
}
|
||||
|
||||
func TestChunkParser(t *testing.T) {
|
||||
engine := NewEngine()
|
||||
for i, test := range liquidTests {
|
||||
t.Run(fmt.Sprint(i), func(t *testing.T) {
|
||||
out, err := engine.ParseAndRenderString(test.in, liquidTestScope)
|
||||
require.NoErrorf(t, err, test.in)
|
||||
require.Equalf(t, test.expected, string(out), test.in)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user