-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.go
92 lines (81 loc) · 2.19 KB
/
text.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
package html5
import (
"bytes"
"fmt"
"io"
"reflect"
"strings"
"github.com/the80srobot/html5/bindings"
"github.com/the80srobot/html5/safe"
)
type TextNode struct {
Value Value
}
func Text(contents ...Value) Node {
switch len(contents) {
case 0:
return &TextNode{}
case 1:
return &TextNode{Value: contents[0]}
default:
m := &MultiNode{Contents: make([]Node, 0, len(contents))}
for _, c := range contents {
m.Contents = append(m.Contents, &TextNode{Value: c})
}
return m
}
}
func (t *TextNode) Apply(n Node) error {
switch n := n.(type) {
case *ElementNode:
n.Contents = append(n.Contents, t)
case *MultiNode:
n.Contents = append(n.Contents, t)
default:
return fmt.Errorf("TextNode can only be applied to ElementNode or MultiNode, got %v", n)
}
return nil
}
func (t *TextNode) String() string {
return fmt.Sprintf("&TextNode{value=%v}", t.Value)
}
func (t *TextNode) compile(tc *templateCompiler, depth int, opts *CompileOptions) error {
switch v := t.Value.(type) {
case safe.String:
s, err := safe.Check(v, safe.TextSafe)
if err != nil {
return err
}
return fprintBlockText(tc, depth, opts.TextWidth, opts.Indent, strings.NewReader(s))
case bindings.Var:
v = tc.bindings.Attach(v, safe.TextSafe)
tc.appendChunk(textBindingChunk{TextNode: *t, depth: depth, indent: opts.Indent, binding: v, width: opts.TextWidth})
return nil
default:
return fmt.Errorf("value must be safe.String or *bindings.Var, %v (%v) is neither", v, reflect.TypeOf(v))
}
}
type textBindingChunk struct {
TextNode
binding bindings.Var
indent string
depth int
width int
}
func (tc textBindingChunk) build(w io.Writer, vm *bindings.ValueMap) error {
// An optimization: if we don't need to break up the lines then we can just
// print the binding value as is.
if tc.width <= 0 {
_, err := io.WriteString(w, vm.GetString(tc.binding))
return err
}
var b bytes.Buffer
if _, err := io.WriteString(&b, vm.GetString(tc.binding)); err != nil {
return err
}
return fprintBlockText(w, tc.depth, tc.width, tc.indent, &b)
}
func (tc textBindingChunk) String() string {
return fmt.Sprintf("textBinding{%v, tag=%v, indent=%q, depth=%d}",
&tc.TextNode, tc.binding, tc.indent, tc.depth)
}