fix: some bug
This commit is contained in:
@@ -26,9 +26,6 @@ func DetectType(markdownContent string) (string, error) {
|
|||||||
|
|
||||||
blocks := matcher.MatchBlocksFuzzy(markdownContent, defaultSchema, 0.3)
|
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)
|
result, err := decoders.Parse(blocks)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("failed to parse blocks -> %w", err)
|
return "", fmt.Errorf("failed to parse blocks -> %w", err)
|
||||||
@@ -84,6 +81,12 @@ func ParseFile(markdownContent string) (any, error) {
|
|||||||
startMarkdown := time.Now()
|
startMarkdown := time.Now()
|
||||||
blocks := matcher.MatchBlocksFuzzy(markdownContent, tpl, 0.3)
|
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)
|
result, err := decoders.Parse(blocks)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to parse blocks -> %w", err)
|
return nil, fmt.Errorf("failed to parse blocks -> %w", err)
|
||||||
|
@@ -18,12 +18,14 @@ func TestParseRecipe_Golden(t *testing.T) {
|
|||||||
t.Fatalf("ParseFile: %v", err)
|
t.Fatalf("ParseFile: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gotMap := got.(map[string]any)
|
||||||
|
|
||||||
var want map[string]any
|
var want map[string]any
|
||||||
if err := json.Unmarshal(output, &want); err != nil {
|
if err := json.Unmarshal(output, &want); err != nil {
|
||||||
t.Fatalf("unmarshal expected.json: %v", err)
|
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)
|
t.Fatalf("JSON mismatch (-want +got):\n%s", diff)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
@@ -1,7 +1,5 @@
|
|||||||
package template
|
package template
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
// CompileTemplate scans once, emitting:
|
// CompileTemplate scans once, emitting:
|
||||||
// - data blocks: inner content between a line that's exactly "{" and a line that's exactly "}"
|
// - 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)
|
// - 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 curlyIndex == 0 && nextCurlyIndex == 1 {
|
||||||
if i > start {
|
if i > start {
|
||||||
fmt.Printf("BLockContent 1: %q\n", template.Slice(start, i).String())
|
|
||||||
block, err := ParseTemplateBlock(template.Slice(start, i), blockType)
|
block, err := ParseTemplateBlock(template.Slice(start, i), blockType)
|
||||||
if err != nil {
|
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)
|
||||||
@@ -45,11 +42,10 @@ func CompileTemplate(templateSource string) ([]Block, error) {
|
|||||||
start = i
|
start = i
|
||||||
blockType = DataBlock
|
blockType = DataBlock
|
||||||
} else if curlyIndex == 1 && nextCurlyIndex == 0 {
|
} else if curlyIndex == 1 && nextCurlyIndex == 0 {
|
||||||
fmt.Printf("BLockContent 2: %q\n", template.Slice(start, i).String())
|
|
||||||
if i > start {
|
if i > start {
|
||||||
block, err := ParseTemplateBlock(template.Slice(start, i), blockType)
|
block, err := ParseTemplateBlock(template.Slice(start, i+1), blockType)
|
||||||
if err != nil {
|
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)
|
out = append(out, block)
|
||||||
}
|
}
|
||||||
@@ -71,5 +67,17 @@ func CompileTemplate(templateSource string) ([]Block, error) {
|
|||||||
curlyIndex = nextCurlyIndex
|
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
|
return out, nil
|
||||||
}
|
}
|
||||||
|
@@ -3,8 +3,6 @@ package template_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"git.max-richter.dev/max/marka/registry"
|
"git.max-richter.dev/max/marka/registry"
|
||||||
"git.max-richter.dev/max/marka/template"
|
"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.DataBlock, Path: "recipeIngredient", Codec: "list", ListTemplate: "- { . }"},
|
||||||
{Type: template.MatchingBlock},
|
{Type: template.MatchingBlock},
|
||||||
{Type: template.DataBlock, Path: "recipeInstructions", Codec: "list", ListTemplate: "{ @index }. { . }"},
|
{Type: template.DataBlock, Path: "recipeInstructions", Codec: "list", ListTemplate: "{ @index }. { . }"},
|
||||||
|
{Type: template.MatchingBlock},
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(templateBlocks) != len(expected) {
|
if len(templateBlocks) != len(expected) {
|
||||||
t.Fatalf("expected %d blocks, got %d", len(expected), len(templateBlocks))
|
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 {
|
for i, b := range templateBlocks {
|
||||||
exp := expected[i]
|
exp := expected[i]
|
||||||
if b.Type != exp.Type {
|
if b.Type != exp.Type {
|
||||||
|
Reference in New Issue
Block a user