fix: trying to fix error in matching code

This commit is contained in:
Max Richter
2025-09-28 14:40:29 +02:00
parent fb0b80be78
commit 57ea1f6e3e
12 changed files with 39 additions and 14 deletions

View File

@@ -65,5 +65,5 @@ func ParseTemplateBlock(template Slice, blockType BlockType) (block Block, err e
return parseYamlTemplate(template)
}
return block, NewErrorf("invalid template").WithPosition(template.start, template.end)
return block, NewErrorf("invalid template: '%s'", template.String()).WithPosition(template.start, template.end)
}

View File

@@ -1,5 +1,7 @@
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)
@@ -33,6 +35,7 @@ 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)
@@ -42,6 +45,7 @@ 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)
if err != nil {

View File

@@ -3,6 +3,8 @@ package template_test
import (
"testing"
"fmt"
"git.max-richter.dev/max/marka/registry"
"git.max-richter.dev/max/marka/template"
)
@@ -10,13 +12,13 @@ import (
func TestExtractBlocks(t *testing.T) {
src, err := registry.GetTemplate("Recipe")
if err != nil {
t.Errorf("Failed to extract blocks: %s", err.Error())
t.Errorf("failed to load template 'Recipe' -> %s", err.Error())
t.FailNow()
}
templateBlocks, err := template.CompileTemplate(src)
if err != nil {
t.Errorf("Failed to extract blocks: %s", err.Error())
t.Errorf("failed to compile template -> %s", err.Error())
t.FailNow()
}
@@ -58,6 +60,8 @@ func TestExtractBlocks(t *testing.T) {
{Type: template.MatchingBlock},
{Type: template.DataBlock, Path: "name", Codec: "text"},
{Type: template.MatchingBlock},
{Type: template.DataBlock, Path: "keywords", Codec: "hashtags", Optional: true},
{Type: template.MatchingBlock},
{Type: template.DataBlock, Path: "description", Codec: "text"},
{Type: template.MatchingBlock},
{Type: template.DataBlock, Path: "recipeIngredient", Codec: "list", ListTemplate: "- { . }"},
@@ -69,6 +73,9 @@ func TestExtractBlocks(t *testing.T) {
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 {