2023-10-29 01:10:17 +00:00
|
|
|
package salix
|
|
|
|
|
2023-12-18 00:55:13 +00:00
|
|
|
import "go.elara.ws/salix/ast"
|
2023-10-29 01:10:17 +00:00
|
|
|
|
|
|
|
// ifTag represents a #if tag within a Salix template
|
|
|
|
type ifTag struct{}
|
|
|
|
|
|
|
|
func (it ifTag) Run(tc *TagContext, block, args []ast.Node) error {
|
|
|
|
if len(args) != 1 {
|
2023-12-18 00:55:13 +00:00
|
|
|
return tc.PosError(tc.Tag, "expected one argument, got %d", len(args))
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
|
|
|
|
2023-12-18 00:55:13 +00:00
|
|
|
inner, err := it.findInner(tc, block)
|
2023-10-29 01:10:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := tc.GetValue(args[0], nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cond, ok := val.(bool)
|
|
|
|
if !ok {
|
2023-12-18 00:55:13 +00:00
|
|
|
return tc.PosError(args[0], "expected boolean argument, got %T", val)
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if cond {
|
|
|
|
return tc.Execute(block[:inner.endRoot], nil)
|
|
|
|
} else {
|
|
|
|
if len(inner.elifTags) > 0 {
|
|
|
|
for i, elifTag := range inner.elifTags {
|
|
|
|
val, err := tc.GetValue(elifTag.value, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cond, ok := val.(bool)
|
|
|
|
if !ok {
|
2023-12-18 00:55:13 +00:00
|
|
|
return tc.PosError(elifTag.value, "expected boolean argument, got %T", val)
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nextIndex := len(block)
|
|
|
|
if i < len(inner.elifTags)-1 {
|
|
|
|
nextIndex = inner.elifTags[i+1].index
|
|
|
|
} else if inner.elseIndex != 0 {
|
|
|
|
nextIndex = inner.elseIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
if cond {
|
|
|
|
return tc.Execute(block[elifTag.index+1:nextIndex], nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if inner.elseIndex != 0 {
|
|
|
|
return tc.Execute(block[inner.elseIndex+1:], nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type innerTags struct {
|
|
|
|
endRoot int
|
|
|
|
elifTags []elif
|
|
|
|
elseIndex int
|
|
|
|
}
|
|
|
|
|
|
|
|
type elif struct {
|
|
|
|
index int
|
|
|
|
value ast.Node
|
|
|
|
}
|
|
|
|
|
|
|
|
// findInner finds the inner elif and else tags in a block
|
|
|
|
// passed to the if tag.
|
2023-12-18 00:55:13 +00:00
|
|
|
func (it ifTag) findInner(tc *TagContext, block []ast.Node) (innerTags, error) {
|
2023-12-23 03:15:56 +00:00
|
|
|
// Depth keeps track of nested if statements. We only want to look for
|
|
|
|
// else/elif tags within the current if tag, not any nested ones.
|
|
|
|
depth := 0
|
2023-10-29 01:10:17 +00:00
|
|
|
var out innerTags
|
|
|
|
for i, node := range block {
|
2023-12-23 03:15:56 +00:00
|
|
|
switch tag := node.(type) {
|
|
|
|
case ast.Tag:
|
2023-10-29 01:10:17 +00:00
|
|
|
switch tag.Name.Value {
|
2023-12-23 03:15:56 +00:00
|
|
|
case "if":
|
|
|
|
depth++
|
2023-10-29 01:10:17 +00:00
|
|
|
case "elif":
|
2023-12-23 03:15:56 +00:00
|
|
|
if depth != 0 {
|
|
|
|
continue
|
|
|
|
}
|
2023-10-29 01:10:17 +00:00
|
|
|
if out.endRoot == 0 {
|
|
|
|
out.endRoot = i
|
|
|
|
}
|
|
|
|
if len(tag.Params) > 1 {
|
2023-12-18 00:55:13 +00:00
|
|
|
return innerTags{}, tc.PosError(tag.Params[1], "expected one argument, got %d", len(tag.Params))
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
|
|
|
out.elifTags = append(out.elifTags, elif{
|
|
|
|
index: i,
|
|
|
|
value: tag.Params[0],
|
|
|
|
})
|
|
|
|
case "else":
|
2023-12-23 03:15:56 +00:00
|
|
|
if depth != 0 {
|
|
|
|
continue
|
|
|
|
}
|
2023-10-29 01:10:17 +00:00
|
|
|
if out.elseIndex != 0 {
|
2023-12-18 00:55:13 +00:00
|
|
|
return innerTags{}, tc.PosError(tag, "cannot have more than one else tag in an if tag")
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
|
|
|
if out.endRoot == 0 {
|
|
|
|
out.endRoot = i
|
|
|
|
}
|
|
|
|
out.elseIndex = i
|
|
|
|
break
|
|
|
|
}
|
2023-12-23 03:15:56 +00:00
|
|
|
case ast.EndTag:
|
|
|
|
if tag.Name.Value == "if" {
|
|
|
|
depth--
|
|
|
|
}
|
2023-10-29 01:10:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if out.endRoot == 0 {
|
|
|
|
out.endRoot = len(block)
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|