feat: some experiments
This commit is contained in:
@@ -1,13 +1,9 @@
|
||||
package template
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// CompileTemplate scans once, emitting:
|
||||
// - data blocks: inner content between a line that's exactly "{" and a line that's exactly "}"
|
||||
// - matching blocks: gaps between data blocks (excluding the brace lines themselves)
|
||||
func CompileTemplate(template string) ([]Block, error) {
|
||||
func CompileTemplate(templateSource string) ([]Block, error) {
|
||||
var out []Block
|
||||
var curlyIndex int
|
||||
|
||||
@@ -16,14 +12,15 @@ func CompileTemplate(template string) ([]Block, error) {
|
||||
|
||||
var start int
|
||||
var blockType BlockType
|
||||
template := NewSlice(templateSource)
|
||||
|
||||
if len(template) > 0 && template[0] == OPENING {
|
||||
if template.Len() > 0 && template.At(0) == OPENING {
|
||||
blockType = DataBlock
|
||||
} else {
|
||||
blockType = MatchingBlock
|
||||
}
|
||||
|
||||
for i, r := range template {
|
||||
for i, r := range template.Chars() {
|
||||
|
||||
nextCurlyIndex := curlyIndex
|
||||
|
||||
@@ -36,27 +33,26 @@ func CompileTemplate(template string) ([]Block, error) {
|
||||
|
||||
if curlyIndex == 0 && nextCurlyIndex == 1 {
|
||||
if i > start {
|
||||
block, err := ParseTemplateBlock(template[start:i], blockType)
|
||||
block, err := ParseTemplateBlock(template.Slice(start, i), blockType)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse block: %w", err)
|
||||
return nil, NewErrorf("cannot parse block @pos -> %w", err).WithPosition(start, i)
|
||||
}
|
||||
out = append(out, block)
|
||||
}
|
||||
start = i
|
||||
blockType = DataBlock
|
||||
} else if curlyIndex == 1 && nextCurlyIndex == 0 {
|
||||
|
||||
if i > start {
|
||||
block, err := ParseTemplateBlock(template[start:i+1], blockType)
|
||||
block, err := ParseTemplateBlock(template.Slice(start, i), blockType)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot parse block: %w", err)
|
||||
return nil, NewErrorf("cannot parse block @pos -> %w", err).WithPosition(start, i)
|
||||
}
|
||||
out = append(out, block)
|
||||
}
|
||||
|
||||
nextChar := ' '
|
||||
if i+1 < len(template) {
|
||||
nextChar = rune(template[i+1])
|
||||
if i+1 < template.Len() {
|
||||
nextChar = rune(template.At(i + 1))
|
||||
}
|
||||
|
||||
if nextChar == OPENING {
|
||||
|
Reference in New Issue
Block a user