fix: most of the template blocks
This commit is contained in:
@@ -5,7 +5,6 @@ package parser
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.max-richter.dev/max/marka/parser/decoders"
|
||||
"git.max-richter.dev/max/marka/parser/matcher"
|
||||
@@ -53,79 +52,33 @@ func MatchBlocks(markdownContent, templateContent string) ([]matcher.Block, erro
|
||||
}
|
||||
|
||||
func ParseFile(markdownContent string) (any, error) {
|
||||
timings := make(map[string]int64)
|
||||
|
||||
startDetectType := time.Now()
|
||||
markdownContent = strings.TrimSuffix(markdownContent, "\n")
|
||||
|
||||
contentType, err := DetectType(markdownContent)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not detect type -> %w", err)
|
||||
}
|
||||
timings["detect_type"] = time.Since(startDetectType).Milliseconds()
|
||||
|
||||
startGetTemplate := time.Now()
|
||||
templateContent, err := registry.GetTemplate(contentType)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not get schema -> %w", err)
|
||||
}
|
||||
timings["get_template"] = time.Since(startGetTemplate).Milliseconds()
|
||||
|
||||
startTemplate := time.Now()
|
||||
tpl, err := template.CompileTemplate(templateContent)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to compile template -> %w", err)
|
||||
}
|
||||
timings["template_compilation"] = time.Since(startTemplate).Milliseconds()
|
||||
|
||||
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())
|
||||
return nil, fmt.Errorf("could not get template -> %w", err)
|
||||
}
|
||||
|
||||
result, err := decoders.Parse(blocks)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse blocks -> %w", err)
|
||||
}
|
||||
timings["markdown_parsing"] = time.Since(startMarkdown).Milliseconds()
|
||||
|
||||
response := map[string]any{
|
||||
"data": result,
|
||||
"timings": timings,
|
||||
}
|
||||
|
||||
return response, nil
|
||||
return ParseFileWithTemplate(markdownContent, templateContent)
|
||||
}
|
||||
|
||||
func ParseFileWithTemplate(markdownContent string, templateContent string) (any, error) {
|
||||
timings := make(map[string]int64)
|
||||
|
||||
startTemplate := time.Now()
|
||||
markdownContent = strings.TrimSuffix(markdownContent, "\n")
|
||||
|
||||
tpl, err := template.CompileTemplate(templateContent)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to compile template -> %w", err)
|
||||
}
|
||||
timings["template_compilation"] = time.Since(startTemplate).Milliseconds()
|
||||
|
||||
startMarkdown := time.Now()
|
||||
blocks := matcher.MatchBlocksFuzzy(markdownContent, tpl, 0.3)
|
||||
|
||||
result, err := decoders.Parse(blocks)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse blocks -> %w", err)
|
||||
}
|
||||
timings["markdown_parsing"] = time.Since(startMarkdown).Milliseconds()
|
||||
|
||||
response := map[string]any{
|
||||
"data": result,
|
||||
"timings": timings,
|
||||
return nil, fmt.Errorf("failed to compile blocks -> %w", err)
|
||||
}
|
||||
|
||||
return response, nil
|
||||
return result, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user