feat: some experiments

This commit is contained in:
Max Richter
2025-09-26 17:32:16 +02:00
parent ea978b48f6
commit 8ca5152c3c
12 changed files with 147 additions and 66 deletions

View File

@@ -1,8 +1,7 @@
// Package blocks contains the logic for parsing template blocks.
// Package template contains the logic for parsing templates.
package template
import (
"fmt"
"strings"
)
@@ -16,8 +15,8 @@ const (
)
// DetectTemplateType checks if the template is short or long.
func DetectTemplateType(tmpl string) TemplateType {
trimmed := strings.TrimSpace(tmpl)
func DetectTemplateType(tmpl Slice) TemplateType {
trimmed := strings.TrimSpace(tmpl.String())
// Short type: starts with "{" and ends with "}" on a single line,
// and contains "|" or "," inside for inline definition
@@ -43,15 +42,15 @@ func DetectTemplateType(tmpl string) TemplateType {
return InvalidTemplate
}
func cleanTemplate(input string) string {
s := strings.TrimSpace(input)
func cleanTemplate(input Slice) string {
s := strings.TrimSpace(input.String())
s = strings.TrimPrefix(s, "{")
s = strings.TrimSuffix(s, "}")
s = strings.Trim(s, "\n")
return s
}
func ParseTemplateBlock(template string, blockType BlockType) (block Block, err error) {
func ParseTemplateBlock(template Slice, blockType BlockType) (block Block, err error) {
if blockType == MatchingBlock {
return Block{
Type: MatchingBlock,
@@ -66,5 +65,5 @@ func ParseTemplateBlock(template string, blockType BlockType) (block Block, err
return parseYamlTemplate(template)
}
return block, fmt.Errorf("invalid template")
return block, NewErrorf("invalid template").WithPosition(template.start, template.end)
}