diff --git a/parser/parser.go b/parser/parser.go index 58fcd99..ce71ee5 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -26,9 +26,6 @@ func DetectType(markdownContent string) (string, error) { blocks := matcher.MatchBlocksFuzzy(markdownContent, defaultSchema, 0.3) - fmt.Printf("%+v\n", blocks[0]) - fmt.Printf("Content: '%q'\n", blocks[0].GetContent()) - result, err := decoders.Parse(blocks) if err != nil { return "", fmt.Errorf("failed to parse blocks -> %w", err) @@ -84,6 +81,12 @@ func ParseFile(markdownContent string) (any, error) { startMarkdown := time.Now() blocks := matcher.MatchBlocksFuzzy(markdownContent, tpl, 0.3) + fmt.Println("Blocks: ", len(blocks)) + for i, b := range blocks { + fmt.Printf("Block %d %+v\n", i, b) + fmt.Printf("Content %d: %q\n\n", i, b.GetContent()) + } + result, err := decoders.Parse(blocks) if err != nil { return nil, fmt.Errorf("failed to parse blocks -> %w", err) diff --git a/parser/parser_test.go b/parser/parser_test.go index f118c4b..48b092f 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -18,12 +18,14 @@ func TestParseRecipe_Golden(t *testing.T) { t.Fatalf("ParseFile: %v", err) } + gotMap := got.(map[string]any) + var want map[string]any if err := json.Unmarshal(output, &want); err != nil { t.Fatalf("unmarshal expected.json: %v", err) } - if diff := cmp.Diff(want, got); diff != "" { + if diff := cmp.Diff(want, gotMap["data"]); diff != "" { t.Fatalf("JSON mismatch (-want +got):\n%s", diff) } } diff --git a/playground/static/main.wasm b/playground/static/main.wasm index 863fd6b..dc730e1 100644 Binary files a/playground/static/main.wasm and b/playground/static/main.wasm differ diff --git a/template/compile.go b/template/compile.go index ea3a74c..b8cbe84 100644 --- a/template/compile.go +++ b/template/compile.go @@ -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 } diff --git a/template/compile_test.go b/template/compile_test.go index 3d66f09..b76a8b0 100644 --- a/template/compile_test.go +++ b/template/compile_test.go @@ -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 {