github.com/gofiber/pug@v1.0.1/node.go (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package jade
     6  
     7  import (
     8  	"bytes"
     9  	"io"
    10  )
    11  
    12  // var textFormat = "%s" // Changed to "%q" in tests for better error messages.
    13  
    14  // A Node is an element in the parse tree. The interface is trivial.
    15  // The interface contains an unexported method so that only
    16  // types local to this package can satisfy it.
    17  type Node interface {
    18  	Type() NodeType
    19  	String() string
    20  	WriteIn(io.Writer)
    21  	// Copy does a deep copy of the Node and all its components.
    22  	// To avoid type assertions, some XxxNodes also have specialized
    23  	// CopyXxx methods that return *XxxNode.
    24  	Copy() Node
    25  	Position() Pos // byte position of start of node in full original input string
    26  	// tree returns the containing *Tree.
    27  	// It is unexported so all implementations of Node are in this package.
    28  	tree() *Tree
    29  }
    30  
    31  // Pos represents a byte position in the original input text from which
    32  // this template was parsed.
    33  type Pos int
    34  
    35  func (p Pos) Position() Pos {
    36  	return p
    37  }
    38  
    39  // Nodes.
    40  
    41  // ListNode holds a sequence of nodes.
    42  type ListNode struct {
    43  	NodeType
    44  	Pos
    45  	tr    *Tree
    46  	Nodes []Node // The element nodes in lexical order.
    47  }
    48  
    49  func (t *Tree) newList(pos Pos) *ListNode {
    50  	return &ListNode{tr: t, NodeType: NodeList, Pos: pos}
    51  }
    52  
    53  func (l *ListNode) append(n Node) {
    54  	l.Nodes = append(l.Nodes, n)
    55  }
    56  
    57  func (l *ListNode) tree() *Tree {
    58  	return l.tr
    59  }
    60  
    61  func (l *ListNode) String() string {
    62  	b := new(bytes.Buffer)
    63  	l.WriteIn(b)
    64  	return b.String()
    65  }
    66  func (l *ListNode) WriteIn(b io.Writer) {
    67  	for _, n := range l.Nodes {
    68  		n.WriteIn(b)
    69  	}
    70  }
    71  
    72  func (l *ListNode) CopyList() *ListNode {
    73  	if l == nil {
    74  		return l
    75  	}
    76  	n := l.tr.newList(l.Pos)
    77  	for _, elem := range l.Nodes {
    78  		n.append(elem.Copy())
    79  	}
    80  	return n
    81  }
    82  
    83  func (l *ListNode) Copy() Node {
    84  	return l.CopyList()
    85  }