/
ot_test_stuff.go
149 lines (142 loc) · 3.19 KB
/
ot_test_stuff.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package ot
import (
"bufio"
"bytes"
"encoding/xml"
"fmt"
"github.com/kpmy/ot/ir"
"github.com/kpmy/ot/ir/types"
"github.com/kpmy/ot/otm"
"github.com/kpmy/ot/otm/conv"
"github.com/kpmy/ot/otp"
"github.com/kpmy/ot/ots"
"github.com/kpmy/trigo"
"github.com/kpmy/ypk/fn"
"github.com/kpmy/ypk/halt"
"github.com/tv42/zbase32"
"io"
"reflect"
"strconv"
"strings"
"testing"
)
func prettyPrint(t *ir.Template, log *testing.T) {
wr := bytes.NewBufferString("")
depth := 0
tab := func() {
for i := 0; i < depth; i++ {
fmt.Fprint(wr, " ")
}
}
tab()
for _, _s := range t.Stmt {
switch s := _s.(type) {
case *ir.Emit:
tab()
fmt.Fprint(wr, fn.MaybeString(s.Template, "~"), s.Class, fn.MaybeString("(", s.Ident, ")"))
if s.ChildCount == 0 {
fmt.Fprintln(wr)
}
case *ir.Dive:
if !s.Reuse {
fmt.Fprintln(wr, ":")
} else {
fmt.Fprintln(wr, " ::")
}
depth++
case *ir.Rise:
depth--
tab()
fmt.Fprintln(wr, ";")
case *ir.Put:
tab()
switch s.Type {
case types.STRING:
fmt.Fprint(wr, "`", s.Value, "`")
case types.REAL, types.INTEGER, types.TRILEAN:
fmt.Fprint(wr, s.Value)
case types.CHAR:
fmt.Fprint(wr, "0", strings.ToUpper(strconv.FormatUint(uint64(s.Value.(rune)), 16)), "U")
case types.LINK:
fmt.Fprint(wr, "@", s.Value)
default:
halt.As(100, s.Type)
}
fmt.Fprintln(wr)
default:
halt.As(100, reflect.TypeOf(s))
}
}
log.Log(wr.String())
}
func prettyPrintObject(o otm.Object, log *testing.T) {
parent := ""
for x := o.Parent(); !fn.IsNil(x); x = x.Parent() {
parent = fmt.Sprint(x.Qualident(), "<-", parent)
}
log.Log(parent, o.Qualident())
if o.ChildrenCount() > 0 {
log.Log(":")
for _x := range o.Children() {
switch x := _x.(type) {
case otm.Object:
prettyPrintObject(x, log)
case otm.Link:
log.Log("@", x.Object().Qualident())
case string, float64, int64, rune, tri.Trit:
log.Log(_x)
case []uint8:
s := zbase32.EncodeToString(x)
log.Log("zbase32(", s, ")", x)
default:
halt.As(100, reflect.TypeOf(x))
}
}
log.Log(";")
}
}
func renderHtml(o otm.Object, log *testing.T) {
buf := bytes.NewBufferString("<!DOCTYPE HTML>")
e := xml.NewEncoder(buf)
var obj func(otm.Object)
obj = func(o otm.Object) {
clazz := o.InstanceOf().Qualident()
if clazz.Template == "html" {
start := xml.StartElement{}
start.Name.Local = clazz.Class
if id := o.Qualident().Identifier; id != "" {
attr := xml.Attr{}
attr.Name.Local = "id"
attr.Value = id
start.Attr = append(start.Attr, attr)
}
e.EncodeToken(start)
for _x := range o.Children() {
switch x := _x.(type) {
case otm.Object:
obj(x)
case string:
e.EncodeToken(xml.CharData([]byte(x)))
default:
halt.As(100, reflect.TypeOf(x))
}
}
e.EncodeToken(start.End())
}
}
for x := range o.ChildrenObjects() {
if x.InstanceOf().Qualident().Template == "html" && x.InstanceOf().Qualident().Class == "html" {
obj(x)
}
}
e.Flush()
log.Log(buf.String())
}
func compile(tpl io.Reader) (otm.Object, error) {
p := otp.ConnectTo(ots.ConnectTo(bufio.NewReader(tpl)))
if tpl, err := p.Template(); err == nil {
return conv.Map(tpl), nil
} else {
return nil, err
}
}