fix: some bug

This commit is contained in:
Max Richter
2025-09-28 20:08:21 +02:00
parent 57ea1f6e3e
commit d35f3e5e2e
5 changed files with 24 additions and 15 deletions

View File

@@ -1,7 +1,5 @@
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)
@@ -35,7 +33,6 @@ func CompileTemplate(templateSource string) ([]Block, error) {
if curlyIndex == 0 && nextCurlyIndex == 1 {
if i > start {
fmt.Printf("BLockContent 1: %q\n", template.Slice(start, i).String())
block, err := ParseTemplateBlock(template.Slice(start, i), blockType)
if err != nil {
return nil, NewErrorf("cannot parse block @pos -> %w", err).WithPosition(start, i)
@@ -45,11 +42,10 @@ func CompileTemplate(templateSource string) ([]Block, error) {
start = i
blockType = DataBlock
} else if curlyIndex == 1 && nextCurlyIndex == 0 {
fmt.Printf("BLockContent 2: %q\n", template.Slice(start, i).String())
if i > start {
block, err := ParseTemplateBlock(template.Slice(start, i), blockType)
block, err := ParseTemplateBlock(template.Slice(start, i+1), blockType)
if err != nil {
return nil, NewErrorf("cannot parse block @pos -> %w", err).WithPosition(start, i)
return nil, NewErrorf("cannot parse block @pos -> %w", err).WithPosition(start, i+1)
}
out = append(out, block)
}
@@ -71,5 +67,17 @@ func CompileTemplate(templateSource string) ([]Block, error) {
curlyIndex = nextCurlyIndex
}
if curlyIndex != 0 {
return nil, NewErrorf("unclosed block").WithPosition(start, template.Len())
}
if start < template.Len() {
block, err := ParseTemplateBlock(template.Slice(start, template.Len()), blockType)
if err != nil {
return nil, NewErrorf("cannot parse final block @pos -> %w", err).WithPosition(start, template.Len())
}
out = append(out, block)
}
return out, nil
}

View File

@@ -3,8 +3,6 @@ package template_test
import (
"testing"
"fmt"
"git.max-richter.dev/max/marka/registry"
"git.max-richter.dev/max/marka/template"
)
@@ -67,15 +65,13 @@ func TestExtractBlocks(t *testing.T) {
{Type: template.DataBlock, Path: "recipeIngredient", Codec: "list", ListTemplate: "- { . }"},
{Type: template.MatchingBlock},
{Type: template.DataBlock, Path: "recipeInstructions", Codec: "list", ListTemplate: "{ @index }. { . }"},
{Type: template.MatchingBlock},
}
if len(templateBlocks) != len(expected) {
t.Fatalf("expected %d blocks, got %d", len(expected), len(templateBlocks))
}
fmt.Printf("%+v\n", templateBlocks[0]);
fmt.Printf("Content: %q\n", templateBlocks[0].GetContent());
for i, b := range templateBlocks {
exp := expected[i]
if b.Type != exp.Type {