feat: renderer
This commit is contained in:
34
renderer/codec/codec.go
Normal file
34
renderer/codec/codec.go
Normal file
@@ -0,0 +1,34 @@
|
||||
// Package codec contains functions for rendering template.Block to a string.
|
||||
package codec
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.max-richter.dev/max/marka/renderer/utils"
|
||||
"git.max-richter.dev/max/marka/template"
|
||||
)
|
||||
|
||||
// RenderBlock renders a single template block using the provided data.
|
||||
func RenderBlock(block template.Block, data map[string]any) (string, error) {
|
||||
if block.Type == template.MatchingBlock {
|
||||
return block.GetContent(), nil
|
||||
}
|
||||
|
||||
value, found := utils.GetValueFromPath(data, block.Path)
|
||||
if !found {
|
||||
return "", nil // If not found and not required, return empty string
|
||||
}
|
||||
|
||||
switch block.Codec {
|
||||
case template.CodecText:
|
||||
return fmt.Sprintf("%v", value), nil
|
||||
case template.CodecYaml:
|
||||
return RenderYaml(data, block)
|
||||
case template.CodecList:
|
||||
return RenderList(value, block)
|
||||
case template.CodecConst:
|
||||
return fmt.Sprintf("%v", block.Value), nil
|
||||
default:
|
||||
return "", fmt.Errorf("unknown codec: %s for path '%s'", block.Codec, block.Path)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user