Beispiel #1
0
// cleanupSlot removes slot and fill nodes.
//
// May contain child actions:
// SlotNode:  n.List
// DefineNode: n.List
// FillNode:   n.List
// IfNode:     n.List, n.ElseList
// ListNode:   n.Nodes
// RangeNode:  n.List, n.ElseList
// WithNode:   n.List, n.ElseList
func cleanupSlot(n parse.Node) {
	switch n := n.(type) {
	case *parse.IfNode:
		cleanupSlot(n.List)
		cleanupSlot(n.ElseList)
	case *parse.ListNode:
		if n == nil {
			return
		}
		k := 0
		for k < len(n.Nodes) {
			v := n.Nodes[k]
			switch v := v.(type) {
			case *parse.SlotNode:
				// Replace the slot by its list of nodes.
				n.Nodes[k] = v.List
				continue
			case *parse.FillNode:
				// Remove the filler.
				n.Nodes = append(n.Nodes[:k], n.Nodes[k+1:]...)
				continue
			default:
				cleanupSlot(v)
			}
			k++
		}
	case *parse.RangeNode:
		cleanupSlot(n.List)
		cleanupSlot(n.ElseList)
	case *parse.WithNode:
		cleanupSlot(n.List)
		cleanupSlot(n.ElseList)
	}
}
Beispiel #2
0
// escape escapes a template node.
func (e *escaper) escape(c context, n parse.Node) context {
	switch n := n.(type) {
	case *parse.ActionNode:
		return e.escapeAction(c, n)
	case *parse.IfNode:
		return e.escapeBranch(c, &n.BranchNode, "if")
	case *parse.ListNode:
		return e.escapeList(c, n)
	case *parse.RangeNode:
		return e.escapeBranch(c, &n.BranchNode, "range")
	case *parse.TemplateNode:
		return e.escapeTemplate(c, n)
	case *parse.TextNode:
		return e.escapeText(c, n)
	case *parse.WithNode:
		return e.escapeBranch(c, &n.BranchNode, "with")
	}
	panic("escaping " + n.String() + " is unimplemented")
}