-
Notifications
You must be signed in to change notification settings - Fork 136
/
template.go
126 lines (101 loc) · 3.25 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package texttemplate
import (
"fmt"
"strings"
"unicode"
"github.com/k14s/ytt/pkg/template"
)
type Template struct {
name string
}
func NewTemplate(name string) *Template {
return &Template{name: name}
}
func (e *Template) CompileInline(rootNode *NodeRoot,
instructions *template.InstructionSet, nodes *template.Nodes) ([]template.TemplateLine, error) {
return e.compile(rootNode, instructions, nodes)
}
func (e *Template) Compile(rootNode *NodeRoot) (*template.CompiledTemplate, error) {
instructions := template.NewInstructionSet()
nodes := template.NewNodes()
code, err := e.compile(rootNode, instructions, nodes)
if err != nil {
return nil, err
}
return template.NewCompiledTemplate(
e.name, code, instructions, nodes,
template.EvaluationCtxDialects{
EvaluationCtxDialectName: EvaluationCtx{},
},
), nil
}
func (e *Template) compile(rootNode *NodeRoot,
instructions *template.InstructionSet, nodes *template.Nodes) ([]template.TemplateLine, error) {
code := []template.TemplateLine{}
rootNodeTag := nodes.AddRootNode(&NodeRoot{}) // fresh copy to avoid leaking out NodeCode
code = append(code, []template.TemplateLine{
{Instruction: instructions.NewSetCtxType(EvaluationCtxDialectName)},
{Instruction: instructions.NewStartCtx(EvaluationCtxDialectName)},
{Instruction: instructions.NewStartNode(rootNodeTag)},
}...)
var trimSpaceRight bool
for i, node := range rootNode.Items {
switch typedNode := node.(type) {
case *NodeText:
if trimSpaceRight {
typedNode.Content = strings.TrimLeftFunc(typedNode.Content, unicode.IsSpace)
trimSpaceRight = false
}
nodeTag := nodes.AddNode(typedNode, rootNodeTag)
code = append(code, template.TemplateLine{
Instruction: instructions.NewStartNode(nodeTag),
SourceLine: &template.SourceLine{
Position: typedNode.Position,
Content: typedNode.Content,
},
})
code = append(code, template.TemplateLine{
Instruction: instructions.NewSetNode(nodeTag),
SourceLine: &template.SourceLine{
Position: typedNode.Position,
Content: typedNode.Content,
},
})
case *NodeCode:
meta := NodeCodeMeta{typedNode}
trimSpaceRight = meta.ShouldTrimSpaceRight()
if meta.ShoudTrimSpaceLeft() && i != 0 {
if typedLastNode, ok := rootNode.Items[i-1].(*NodeText); ok {
typedLastNode.Content = strings.TrimRightFunc(
typedLastNode.Content, unicode.IsSpace)
}
}
if meta.ShouldPrint() {
nodeTag := nodes.AddNode(&NodeText{}, rootNodeTag)
code = append(code, template.TemplateLine{
Instruction: instructions.NewStartNode(nodeTag),
SourceLine: &template.SourceLine{
Position: typedNode.Position,
Content: typedNode.Content,
},
})
code = append(code, template.TemplateLine{
Instruction: instructions.NewSetNodeValue(nodeTag, meta.Code()),
SourceLine: &template.SourceLine{
Position: typedNode.Position,
Content: typedNode.Content,
},
})
} else {
code = append(code, template.NewCodeFromBytesAtPosition(
[]byte(meta.Code()), typedNode.Position, instructions)...)
}
default:
panic(fmt.Sprintf("unknown string template node %T", typedNode))
}
}
code = append(code, template.TemplateLine{
Instruction: instructions.NewEndCtx(),
})
return code, nil
}