big tings
This commit is contained in:
@@ -2,16 +2,72 @@
|
||||
// structured JSON objects that conform to a JSON Schema.
|
||||
package parser
|
||||
|
||||
func ParseFile(markdownContent string) (map[string]any, error) {
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
// _schema, err := registry.GetTemplate("Recipe")
|
||||
// if err != nil {
|
||||
// return nil, fmt.Errorf("could not get schema: %w", err)
|
||||
// }
|
||||
"git.max-richter.dev/max/marka/parser/decoders"
|
||||
"git.max-richter.dev/max/marka/parser/matcher"
|
||||
"git.max-richter.dev/max/marka/registry"
|
||||
"git.max-richter.dev/max/marka/template"
|
||||
)
|
||||
|
||||
// Idea is to split the template into blocks, either "matching" blocks which are simple strings.
|
||||
// Or "data" blocks which match the content. Then i want to soft match the "matching" blocks and "data" blocks to the template.
|
||||
// The "matching" blocks should soft match with a levenshtein distance
|
||||
func DetectType(markdownContent string) (string, error) {
|
||||
defaultSchemaContent, err := registry.GetTemplate("_default")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("could not get schema: %w", err)
|
||||
}
|
||||
|
||||
return map[string]any{}, nil
|
||||
defaultSchema, err := template.CompileTemplate(defaultSchemaContent)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to compile template: %w", err)
|
||||
}
|
||||
|
||||
blocks := matcher.MatchBlocksFuzzy(markdownContent, defaultSchema, 0.3)
|
||||
|
||||
result, err := decoders.Parse(blocks)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to parse blocks: %w", err)
|
||||
}
|
||||
|
||||
if result, ok := result.(map[string]any); ok {
|
||||
if contentType, ok := result["@type"]; ok {
|
||||
return contentType.(string), nil
|
||||
} else {
|
||||
return "", fmt.Errorf("frontmatter did not contain '@type'")
|
||||
}
|
||||
} else {
|
||||
return "", fmt.Errorf("could not parse frontmatter")
|
||||
}
|
||||
}
|
||||
|
||||
func ParseFile(markdownContent string) (any, error) {
|
||||
markdownContent = strings.TrimSuffix(
|
||||
strings.ReplaceAll(markdownContent, "@type:", `"@type":`),
|
||||
"\n",
|
||||
)
|
||||
|
||||
contentType, err := DetectType(markdownContent)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not detect type: %w", err)
|
||||
}
|
||||
|
||||
templateContent, err := registry.GetTemplate(contentType)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not get schema: %w", err)
|
||||
}
|
||||
|
||||
template, err := template.CompileTemplate(templateContent)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to compile template: %w", err)
|
||||
}
|
||||
|
||||
blocks := matcher.MatchBlocksFuzzy(markdownContent, template, 0.3)
|
||||
|
||||
result, err := decoders.Parse(blocks)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse blocks: %w", err)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user