1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-27 08:24:38 +01:00
liquid/chunks/chunk.go

55 lines
1.7 KiB
Go
Raw Normal View History

2017-06-29 19:08:25 +02:00
// Package chunks parses and evaluates template strings.
//
// 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 {%%}.)
2017-06-29 18:20:16 +02:00
package chunks
import "fmt"
// Chunk is a chunk of a template source. It is one of an object "{{…}}", a tag "{%…%}", or the text between objects and tags.
type Chunk struct {
Type ChunkType
SourceInfo SourceInfo
Name string // Name is the tag name of a tag Chunk. E.g. the tag name of "{% if 1 %}" is "if".
Parameters string // Parameters is the tag arguments of a tag Chunk. E.g. the tag arguments of "{% if 1 %}" is "1".
Source string // Source is the entirety of the chunk, including the "{{", "{%", etc. markers.
}
2017-06-30 14:42:11 +02:00
// ChunkType is the type of a Chunk
type ChunkType int
const (
// TextChunkType is the type of a text Chunk
TextChunkType ChunkType = iota
// TagChunkType is the type of a tag Chunk "{%…%}"
TagChunkType
// ObjChunkType is the type of an object Chunk "{{…}}"
ObjChunkType
)
// SourceInfo contains a Chunk's source information
type SourceInfo struct {
Pathname string
lineNo int
}
2017-06-29 18:20:16 +02:00
func (c Chunk) String() string {
switch c.Type {
case TextChunkType:
return fmt.Sprintf("%s{%#v}", c.Type, c.Source)
case TagChunkType:
return fmt.Sprintf("%s{Tag:%#v, Args:%#v}", c.Type, c.Name, c.Parameters)
case ObjChunkType:
return fmt.Sprintf("%s{%#v}", c.Type, c.Parameters)
default:
return fmt.Sprintf("%s{%#v}", c.Type, c.Source)
}
}
2017-06-30 14:42:11 +02:00
func (s SourceInfo) String() string {
if s.Pathname != "" {
return fmt.Sprintf("%s:%d", s.Pathname, s.lineNo)
}
return fmt.Sprintf("line %d", s.lineNo)
2017-06-29 18:20:16 +02:00
}