template: add tests, fix up easy holes
should cope with double-braced blocks now (treating them the same as single-braced. templateEmail now returns an error, which should not be seen as catastrophic, but reports likely mistakes.
This commit is contained in:
+65
-34
@@ -1,6 +1,9 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
)
|
||||
|
||||
func truthy(val interface{}) bool {
|
||||
switch v := val.(type) {
|
||||
@@ -18,43 +21,72 @@ func truthy(val interface{}) bool {
|
||||
// Variables should be written as {varName}.
|
||||
// If statements should be written as {if (!)varName}...{endif}.
|
||||
// Strings are true if != "", ints are true if != 0.
|
||||
func templateEmail(content string, variables []string, conditionals []string, values map[string]interface{}) string {
|
||||
// Errors returned are likely warnings only.
|
||||
func templateEmail(content string, variables []string, conditionals []string, values map[string]interface{}) (string, error) {
|
||||
// minimum length for templatable content (albeit just "{}" -> "")
|
||||
if len(content) < 2 {
|
||||
return content, nil
|
||||
}
|
||||
ifStart, ifEnd := -1, -1
|
||||
ifTrue := false
|
||||
invalidIf := false
|
||||
previousEnd := -2
|
||||
cStart, cEnd := -1, -1
|
||||
blockRawStart := -1
|
||||
blockContentStart, blockContentEnd := -1, -1
|
||||
varStart, varEnd := -1, -1
|
||||
varName := ""
|
||||
out := ""
|
||||
var err error = nil
|
||||
|
||||
oob := func(i int) bool { return i < 0 || i >= len(content) }
|
||||
|
||||
for i, c := range content {
|
||||
if c == '{' {
|
||||
cStart = i + 1
|
||||
for content[cStart] == ' ' {
|
||||
cStart++
|
||||
blockContentStart = i + 1
|
||||
blockRawStart = i
|
||||
if content[i+1] == '{' {
|
||||
err = fmt.Errorf(`double braces ("{{") at position %d, use single brace only`, i)
|
||||
blockContentStart++
|
||||
}
|
||||
if content[cStart:cStart+3] == "if " {
|
||||
varStart = cStart + 3
|
||||
for !oob(blockContentStart) && content[blockContentStart] == ' ' {
|
||||
blockContentStart++
|
||||
}
|
||||
if oob(blockContentStart) {
|
||||
continue
|
||||
}
|
||||
if !oob(blockContentStart+3) && content[blockContentStart:blockContentStart+3] == "if " {
|
||||
varStart = blockContentStart + 3
|
||||
for content[varStart] == ' ' {
|
||||
varStart++
|
||||
}
|
||||
}
|
||||
if ifStart == -1 {
|
||||
if ifStart == -1 && (oob(i-1) || content[i-1] != '{') {
|
||||
out += content[previousEnd+2 : i]
|
||||
}
|
||||
if content[cStart:cStart+5] != "endif" || invalidIf {
|
||||
if invalidIf || oob(blockContentStart+5) || content[blockContentStart:blockContentStart+5] != "endif" {
|
||||
continue
|
||||
}
|
||||
ifEnd = i - 1
|
||||
if ifTrue {
|
||||
out += templateEmail(content[ifStart:ifEnd+1], variables, conditionals, values)
|
||||
toAppend, subErr := templateEmail(content[ifStart:ifEnd+1], variables, conditionals, values)
|
||||
out += toAppend
|
||||
if subErr != nil {
|
||||
err = subErr
|
||||
}
|
||||
ifTrue = false
|
||||
}
|
||||
} else if c == '}' {
|
||||
doubleBraced := !oob(i+1) && content[i+1] == '}'
|
||||
if doubleBraced {
|
||||
err = fmt.Errorf(`double braces ("}}") at position %d, use single brace only`, i)
|
||||
}
|
||||
if !oob(i-1) && content[i-1] == '}' {
|
||||
continue
|
||||
}
|
||||
if varStart != -1 {
|
||||
ifStart = i + 1
|
||||
varEnd = i - 1
|
||||
for content[varEnd] == ' ' {
|
||||
for !oob(varEnd) && content[varEnd] == ' ' {
|
||||
varEnd--
|
||||
}
|
||||
varName = content[varStart : varEnd+1]
|
||||
@@ -63,14 +95,8 @@ func templateEmail(content string, variables []string, conditionals []string, va
|
||||
positive = false
|
||||
varName = varName[1:]
|
||||
}
|
||||
validVar := false
|
||||
wrappedVarName := "{" + varName + "}"
|
||||
for _, v := range conditionals {
|
||||
if v == wrappedVarName {
|
||||
validVar = true
|
||||
break
|
||||
}
|
||||
}
|
||||
validVar := slices.Contains(conditionals, wrappedVarName)
|
||||
if validVar {
|
||||
ifTrue = positive == truthy(values[varName])
|
||||
} else {
|
||||
@@ -79,27 +105,26 @@ func templateEmail(content string, variables []string, conditionals []string, va
|
||||
}
|
||||
varStart, varEnd = -1, -1
|
||||
}
|
||||
cEnd = i - 1
|
||||
for content[cEnd] == ' ' {
|
||||
cEnd--
|
||||
blockContentEnd = i - 1
|
||||
for content[blockContentEnd] == ' ' {
|
||||
blockContentEnd--
|
||||
}
|
||||
previousEnd = i - 1
|
||||
if content[cEnd-4:cEnd+1] == "endif" && !invalidIf {
|
||||
// Skip the extra brace
|
||||
if doubleBraced {
|
||||
previousEnd++
|
||||
}
|
||||
if !oob(blockContentEnd-4) && !oob(blockContentEnd+1) && content[blockContentEnd-4:blockContentEnd+1] == "endif" && !invalidIf {
|
||||
continue
|
||||
}
|
||||
validVar := false
|
||||
varName = content[cStart : cEnd+1]
|
||||
cStart, cEnd = -1, -1
|
||||
varName = content[blockContentStart : blockContentEnd+1]
|
||||
blockContentStart, blockContentEnd = -1, -1
|
||||
blockRawStart = -1
|
||||
if ifStart != -1 {
|
||||
continue
|
||||
}
|
||||
wrappedVarName := "{" + varName + "}"
|
||||
for _, v := range variables {
|
||||
if v == wrappedVarName {
|
||||
validVar = true
|
||||
break
|
||||
}
|
||||
}
|
||||
validVar := slices.Contains(variables, wrappedVarName)
|
||||
if !validVar {
|
||||
out += wrappedVarName
|
||||
continue
|
||||
@@ -107,11 +132,17 @@ func templateEmail(content string, variables []string, conditionals []string, va
|
||||
out += fmt.Sprint(values[varName])
|
||||
}
|
||||
}
|
||||
if blockContentStart != -1 && blockContentEnd == -1 {
|
||||
err = fmt.Errorf(`incomplete block (single "{") near position %d`, blockContentStart)
|
||||
// Include the brace, maybe the user wants it.
|
||||
previousEnd = blockRawStart - 2
|
||||
}
|
||||
if previousEnd+1 != len(content)-1 {
|
||||
out += content[previousEnd+2:]
|
||||
|
||||
}
|
||||
if out == "" {
|
||||
return content
|
||||
return content, err
|
||||
}
|
||||
return out
|
||||
return out, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user