35 lines
956 B
Go
35 lines
956 B
Go
// 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)
|
|
}
|
|
}
|