feat: added keyword codec (partially works)

This commit is contained in:
2025-08-19 19:06:08 +02:00
parent 210b31aef8
commit 69c2550f44
24 changed files with 964 additions and 80 deletions

View File

@@ -2,28 +2,15 @@
package renderer
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"git.max-richter.dev/max/marka/registry"
"git.max-richter.dev/max/marka/renderer/codec"
"git.max-richter.dev/max/marka/renderer/encoders"
"git.max-richter.dev/max/marka/template"
"git.max-richter.dev/max/marka/validator"
)
const emptyBlock = "\uE000"
func fixRenderedBlock(input string) string {
input = strings.ReplaceAll(input, "'@type':", "@type:")
input = strings.ReplaceAll(input, "'@context':", "@context:")
if len(input) == 0 {
return emptyBlock
}
return input
}
func RenderFile(rawJSON []byte) ([]byte, error) {
// 1) parse json
var data map[string]any
@@ -57,21 +44,9 @@ func RenderFile(rawJSON []byte) ([]byte, error) {
}
// 6) render the template with the blocks
var buffer bytes.Buffer
for _, block := range compiledTemplate {
renderedContent, err := codec.RenderBlock(block, data)
if err != nil {
return nil, fmt.Errorf("failed to render block for path '%s': %w", block.Path, err)
}
renderedContent = fixRenderedBlock(renderedContent)
buffer.WriteString(renderedContent)
}
outputString := buffer.String()
outputString = strings.ReplaceAll(outputString, "\n\n"+emptyBlock+"\n\n", "\n\n")
outputString = strings.ReplaceAll(outputString, emptyBlock, "")
if !strings.HasSuffix(outputString, "\n") {
outputString += "\n"
outputString, err := encoders.Render(data, compiledTemplate)
if err != nil {
return nil, fmt.Errorf("failed to render template: %w", err)
}
return []byte(outputString), nil