wip
This commit is contained in:
@@ -5,6 +5,7 @@ package parser
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.max-richter.dev/max/marka/parser/decoders"
|
||||
"git.max-richter.dev/max/marka/parser/matcher"
|
||||
@@ -61,29 +62,73 @@ func MatchBlocks(markdownContent string) ([]matcher.Block, error) {
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
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()
|
||||
|
||||
return result, nil
|
||||
response := map[string]any{
|
||||
"data": result,
|
||||
"timings": timings,
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
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 response, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user