forked from Joker/jade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jade_node.go
259 lines (226 loc) · 6.62 KB
/
jade_node.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package jade
import (
"bytes"
"fmt"
"regexp"
"strings"
)
// NodeType identifies the type of a parse tree node.
type nodeType int
const (
nodeList nodeType = iota
nodeText
nodeTag
nodeAttr
nodeDoctype
)
func indentToString(nesting, indent int, fromConf bool) string {
if PrettyOutput {
idt := new(bytes.Buffer)
if fromConf {
for i := 0; i < nesting; i++ {
idt.WriteString(OutputIndent)
}
} else {
for i := 0; i < indent; i++ {
idt.WriteByte(' ')
}
}
return idt.String()
}
return ""
}
type nestNode struct {
nodeType
psn
tr *tree
Nodes []node
typ itemType
Tag string
Indent int
Nesting int
id string
class []string
}
func (t *tree) newNest(pos psn, tag string, tp itemType, idt, nst int) *nestNode {
return &nestNode{tr: t, nodeType: nodeTag, psn: pos, Tag: tag, typ: tp, Indent: idt, Nesting: nst}
}
func (nn *nestNode) append(n node) {
nn.Nodes = append(nn.Nodes, n)
}
func (nn *nestNode) tree() *tree {
return nn.tr
}
func (nn *nestNode) tp() itemType {
return nn.typ
}
func (nn *nestNode) String() string {
b := new(bytes.Buffer)
idt := indentToString(nn.Nesting, nn.Indent, nestIndent)
if PrettyOutput && nn.typ != itemInlineTag && nn.typ != itemInlineVoidTag {
idt = "\n" + idt
}
beginFormat := idt + "<%s"
endFormat := "</%s>"
switch nn.typ {
case itemDiv:
nn.Tag = "div"
case itemInlineTag, itemInlineVoidTag:
beginFormat = "<%s"
case itemComment:
nn.Tag = "--"
beginFormat = idt + "<!%s "
endFormat = " %s>"
case itemAction, itemActionEnd:
beginFormat = idt + "{{ %s }}"
case itemBlock:
beginFormat = idt + "{{ block \"%s\" . }}"
case itemDefine:
beginFormat = idt + "{{ define \"%s\" }}"
case itemTemplate:
beginFormat = idt + "{{ template \"%s\" }}"
}
if len(nn.Nodes) > 1 || (len(nn.Nodes) == 1 && nn.Nodes[0].tp() != itemEndAttr) {
endEl := nn.Nodes[len(nn.Nodes)-1].tp()
if endEl != itemInlineText && endEl != itemInlineAction && endEl != itemInlineTag {
endFormat = idt + endFormat
}
}
fmt.Fprintf(b, beginFormat, nn.Tag)
if len(nn.id) > 0 {
fmt.Fprintf(b, " id=\"%s\"", nn.id)
}
if len(nn.class) > 0 {
fmt.Fprintf(b, " class=\"%s\"", strings.Join(nn.class, " "))
}
for _, n := range nn.Nodes {
fmt.Fprint(b, n)
}
switch nn.typ {
case itemActionEnd, itemDefine, itemBlock:
fmt.Fprint(b, idt+"{{ end }}")
case itemTag, itemDiv, itemInlineTag, itemComment:
fmt.Fprintf(b, endFormat, nn.Tag)
}
return b.String()
}
func (nn *nestNode) CopyNest() *nestNode {
if nn == nil {
return nn
}
n := nn.tr.newNest(nn.psn, nn.Tag, nn.typ, nn.Indent, nn.Nesting)
for _, elem := range nn.Nodes {
n.append(elem.Copy())
}
return n
}
func (nn *nestNode) Copy() node {
return nn.CopyNest()
}
var doctype = map[string]string{
"xml": `<?xml version="1.0" encoding="utf-8" ?>`,
"html": `<!DOCTYPE html>`,
"5": `<!DOCTYPE html>`,
"1.1": `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">`,
"xhtml": `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">`,
"basic": `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">`,
"mobile": `<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">`,
"strict": `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">`,
"frameset": `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">`,
"transitional": `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">`,
"4": `<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">`,
"4strict": `<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">`,
"4frameset": `<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"> `,
"4transitional": `<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">`,
}
type doctypeNode struct {
nodeType
psn
tr *tree
Doctype string
}
func (t *tree) newDoctype(pos psn, dt string) *doctypeNode {
return &doctypeNode{tr: t, nodeType: nodeDoctype, psn: pos, Doctype: dt}
}
func (d *doctypeNode) String() string {
if dt, ok := doctype[d.Doctype]; ok {
return fmt.Sprintf("\n%s", dt)
}
return fmt.Sprintf("\n<!DOCTYPE html>")
}
func (d *doctypeNode) tp() itemType {
return itemDoctype
}
func (d *doctypeNode) tree() *tree {
return d.tr
}
func (d *doctypeNode) Copy() node {
return &doctypeNode{tr: d.tr, nodeType: nodeDoctype, psn: d.psn, Doctype: d.Doctype}
}
// lineNode holds plain text.
type lineNode struct {
nodeType
psn
tr *tree
Text []byte // The text; may span newlines.
typ itemType
Indent int
Nesting int
}
func (t *tree) newLine(pos psn, text string, tp itemType, idt, nst int) *lineNode {
return &lineNode{tr: t, nodeType: nodeText, psn: pos, Text: []byte(text), typ: tp, Indent: idt, Nesting: nst}
}
func (l *lineNode) String() string {
idt := indentToString(l.Nesting, l.Indent, lineIndent)
rex := regexp.MustCompile("[!#]{(.+?)}")
switch l.typ {
case itemInlineAction:
return fmt.Sprintf("{{%s }}", l.Text)
case itemInlineText:
return fmt.Sprintf("%s", rex.ReplaceAll(l.Text, []byte("{{$1}}")))
default:
return fmt.Sprintf("\n"+idt+"%s", rex.ReplaceAll(l.Text, []byte("{{$1}}")))
}
}
func (l *lineNode) tp() itemType {
return l.typ
}
func (l *lineNode) tree() *tree {
return l.tr
}
func (l *lineNode) Copy() node {
return &lineNode{tr: l.tr, nodeType: nodeText, psn: l.psn, Text: append([]byte{}, l.Text...), typ: l.typ, Indent: l.Indent, Nesting: l.Nesting}
}
type attrNode struct {
nodeType
psn
tr *tree
Attr string
typ itemType
}
func (t *tree) newAttr(pos psn, attr string, tp itemType) *attrNode {
return &attrNode{tr: t, nodeType: nodeAttr, psn: pos, Attr: attr, typ: tp}
}
func (a *attrNode) String() string {
switch a.typ {
case itemEndAttr:
return fmt.Sprintf("%s", a.Attr)
case itemAttr:
return fmt.Sprintf("=%s", a.Attr)
case itemAttrN:
return fmt.Sprintf("=\"%s\"", a.Attr)
case itemAttrVoid:
return fmt.Sprintf(" %s=\"%s\"", a.Attr, a.Attr)
default:
return fmt.Sprintf(" %s", a.Attr)
}
}
func (a *attrNode) tp() itemType {
return a.typ
}
func (a *attrNode) tree() *tree {
return a.tr
}
func (a *attrNode) Copy() node {
return &attrNode{tr: a.tr, nodeType: nodeAttr, psn: a.psn, Attr: a.Attr, typ: a.typ}
}