forked from travissimon/ghaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
view_writer.go
444 lines (381 loc) · 11.5 KB
/
view_writer.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package main
import (
"bytes"
"container/list"
"github.com/travissimon/formatting"
"io"
"strings"
)
type ViewWriter struct {
context *ParseContext
rootNode *Node
writer io.Writer
viewName string
properViewName string
writingCodeOutput bool
}
func NewViewWriter(wr io.Writer, context *ParseContext, rootNode *Node,
writerName string, properWriterName string) *ViewWriter {
vw := &ViewWriter{
context: context,
rootNode: rootNode,
writer: wr,
viewName: writerName,
properViewName: properWriterName,
}
return vw
}
func (w *ViewWriter) WriteView() {
src := formatting.NewIndentingWriter(w.writer)
htmlArr, srcOut, patterns := w.processNodes()
src.Printf("package %s\n", w.context.pkg)
src.Println("")
src.Println("// THIS IS A GENERATED FILE, EDITS WILL BE OVERWRITTEN")
src.Println("// EDIT THE .haml FILE INSTEAD")
src.Println("")
src.Println("import (")
src.IncrIndent()
src.Println("\"fmt\"")
src.Println("\"html/template\"")
src.Println("\"io\"")
src.Println("\"os\"")
for _, imp := range w.context.imports {
src.Printf("%q\n", imp)
}
src.DecrIndent()
src.Println(")")
src.Println("")
src.Printf("func New%sWriter() (*%sWriter) {\n", w.properViewName, w.properViewName)
src.IncrIndent()
src.Printf("wr := &%sWriter{\n", w.properViewName)
src.Printf(" templates: make([]*template.Template, 0, len(%sTemplatePatterns)),\n", w.viewName)
src.Println("}")
src.Println("")
src.Printf("for idx, pattern := range %sTemplatePatterns {\n", w.viewName)
src.IncrIndent()
src.Printf("tmpl, err := template.New(\"%sTemplates\" + string(idx)).Parse(pattern)\n", w.viewName)
src.Println("if err != nil {")
src.IncrIndent()
src.Println("fmt.Errorf(\"Could not parse template: %d\", idx)")
src.Println("panic(err)")
src.DecrIndent()
src.Println("}")
src.Println("wr.templates = append(wr.templates, tmpl)")
src.DecrIndent()
src.Println("}")
src.Println("return wr")
src.DecrIndent()
src.Println("}")
src.Println("")
src.Printf("type %sWriter struct {\n", w.properViewName)
src.IncrIndent()
src.Println("templates []*template.Template")
src.DecrIndent()
src.Println("}")
src.Println("")
src.Printf("var %sHtml = [...]string{\n", w.viewName)
src.Println(htmlArr)
src.Println("}")
src.Println("")
src.Printf("var %sTemplatePatterns = []string{\n", w.viewName)
src.Print(patterns)
src.Println("}")
src.Println("")
src.Printf("func (wr *%sWriter) Execute(w io.Writer", w.properViewName)
for _, name := range w.context.data {
typ := w.context.types[name]
src.Printf(",\n\t\t\t%s %s", name, typ)
}
src.Println(") {")
src.IncrIndent()
src.Printf("var err error = nil\n")
// output from processNodes
// This is calls to htmlArray and code generated Prints
src.Print(srcOut)
src.DecrIndent()
src.Println("")
src.IncrIndent()
src.Printf("if err != nil {\n")
src.IncrIndent()
src.Printf("err = nil\n")
src.DecrIndent()
src.Println("}")
src.DecrIndent()
src.Println("}")
src.Println("")
src.Printf("func handle%sError(err error) {\n", w.properViewName)
src.IncrIndent()
src.Printf("if err != nil {\n")
src.IncrIndent()
src.Printf("fmt.Fprintln(os.Stderr, err)\n")
src.DecrIndent()
src.Println("}")
src.DecrIndent()
src.Println("}")
}
// processNodes generates code from the parsed haml nodes
// htmlArray is a string array of the raw html parts.
// src is the go source that calls html array, and then user defined code.
// usually looks like this:
// fmt.Fprint(w, HtmlArray[0])
// fmt.Fprint(w, "Custom string: ", data)
// fmt.Fprint(w, HtmlArray[1]) ...
func (w *ViewWriter) processNodes() (htmlArray string, src string, patterns string) {
htmlBuffer := bytes.NewBuffer(make([]byte, 0))
srcBuffer := bytes.NewBuffer(make([]byte, 0))
patternBuffer := bytes.NewBuffer(make([]byte, 0))
w.processNodes2(formatting.NewIndentingWriter(htmlBuffer),
formatting.NewIndentingWriter(srcBuffer),
formatting.NewIndentingWriter(patternBuffer))
htmlArray = htmlBuffer.String()
src = srcBuffer.String()
patterns = patternBuffer.String()
return
}
func (w *ViewWriter) processNodes2(htmlWriter, srcWriter, patternWriter *formatting.IndentingWriter) {
// initialise opening quote for htmlArray
htmlWriter.Print("`")
// initialise starting indent for src
srcWriter.IncrIndent()
// initialise starting indent and openning quote
patternWriter.IncrIndent()
htmlIndex := 0
patternIndex := 0
for n := w.rootNode.children.Front(); n != nil; n = n.Next() {
htmlIndex, _ = w.writeNode(n, htmlWriter, srcWriter, patternWriter, htmlIndex, patternIndex)
}
// close quote for html Array
htmlWriter.Print("`,")
// Ensure final html is written
srcWriter.Printf("fmt.Fprint(w, %sHtml[%d])\n", w.viewName, htmlIndex)
// if our last op was writing code, we need to close pattern string
if w.writingCodeOutput {
patternWriter.Println("`,")
}
}
type CodeOutputType int
const (
Literal CodeOutputType = iota
EscapedValue
RawValue
Execution
)
// Recursive function to write parsed HAML Nodes
// We have to return a bool indicating if we have escaped any HTML (XSS protection)
// so that we know if we need to include the templating library for that function
func (w *ViewWriter) writeNode(el *list.Element,
haml, src, pattern *formatting.IndentingWriter,
currentHtmlIndex, currentPatternIndex int) (htmlIndex, patternIndex int) {
nd := el.Value.(*Node)
htmlIndex = currentHtmlIndex
patternIndex = currentPatternIndex
switch nd.name {
case "code_output_literal":
return w.writeCodeOutput(el, haml, src, pattern, htmlIndex, patternIndex, Literal)
case "code_output_value":
return w.writeCodeOutput(el, haml, src, pattern, htmlIndex, patternIndex, EscapedValue)
case "code_output_raw":
return w.writeCodeOutput(el, haml, src, pattern, htmlIndex, patternIndex, RawValue)
case "code_execution":
return w.writeCodeOutput(el, haml, src, pattern, htmlIndex, patternIndex, Execution)
}
if w.writingCodeOutput {
// we've finished writing code output and we're back to haml
// so close off our pattern string
pattern.Println("`,")
}
w.writingCodeOutput = false
if nd.name != "" {
haml.Printf("<%s", nd.name)
if nd.id != nil {
w.writeAttribute(nd.id, haml)
}
if nd.class != nil {
w.writeAttribute(nd.class, haml)
}
for attrEl := nd.attributes.Front(); attrEl != nil; attrEl = attrEl.Next() {
attr := attrEl.Value.(*nameValueStr)
w.writeAttribute(attr, haml)
}
if nd.selfClosing {
haml.Print(" />")
if nd.text != "" {
haml.Printf(" %s", nd.text)
}
haml.Println("")
return
} else {
haml.Print(">")
}
}
// Outputting text.
// If tag only contains short text, add it on same line
if w.canChildContentFitOnOneLine(nd) {
if nd.name == "" {
haml.Printf("%s\n", nd.text)
} else {
haml.Printf("%s</%s>\n", nd.text, nd.name)
}
return
}
// We either have long text, child tags or both
// so we add it as indented child content
haml.Println("")
haml.IncrIndent()
if len(nd.text) > 0 {
w.writeLongText(nd.text, haml)
}
for n := nd.children.Front(); n != nil; n = n.Next() {
htmlIndex, patternIndex = w.writeNode(n, haml, src, pattern, htmlIndex, patternIndex)
}
haml.DecrIndent()
if nd.name != "" {
haml.Printf("</%s>\n", nd.name)
}
return
}
var TEXT_BREAK_LENGTH = 100
func (vw *ViewWriter) writeLongText(text string, w *formatting.IndentingWriter) {
// create index of spaces in string
spaces := vw.getWhitespaceIndicies(text)
// split string on space less than MAX_STRING_LENGTH
start := 0
for _, idx := range spaces {
distance := idx - start
if distance > TEXT_BREAK_LENGTH {
w.Println(text[start:idx])
start = idx + 1
}
}
w.Println(text[start:])
}
func (vw *ViewWriter) getWhitespaceIndicies(text string) []int {
spaces := make([]int, 0, 255)
for i, c := range text {
switch c {
case ' ', '\t', '\n':
spaces = append(spaces, i)
}
}
return spaces
}
// Child content can only fit on one line when there is short
// text and no child nodes
func (w *ViewWriter) canChildContentFitOnOneLine(nd *Node) bool {
return len(nd.text) < TEXT_BREAK_LENGTH && nd.children.Len() == 0
}
func (w *ViewWriter) writeAttribute(attribute *nameValueStr, haml *formatting.IndentingWriter) {
haml.Printf(" %s=\"%s\"", attribute.name, attribute.value)
}
func (w *ViewWriter) writeCodeOutput(el *list.Element, haml, src, pattern *formatting.IndentingWriter,
currentHtmlIndex, currentPatternIndex int, nodeType CodeOutputType) (htmlIndex, patternIndex int) {
nd := el.Value.(*Node)
htmlIndex = currentHtmlIndex
patternIndex = currentPatternIndex
if !w.writingCodeOutput {
// First code output node - close off haml node output:
// end most recent haml output
haml.Println("`,")
// start next haml output (which will follow this code output
haml.Println("`")
// Add call to write html from array
src.Printf("fmt.Fprint(w, %sHtml[%d])\n", w.viewName, currentHtmlIndex)
if nodeType == Literal || nodeType == EscapedValue {
// Add calls to execute template. However, we need to know which
// object to inject into template. Usually this will be 'data', but
// can be something else inside a loop, for example, so we take the
// object from the first dynamic element
objectToInject := "data"
if nodeType == EscapedValue {
objectToInject = getObjectName(nd.text)
} else {
LookaheadLoop:
for n := el; n != nil; n = n.Next() {
node := n.Value.(*Node)
switch node.name {
case "code_output_dynamic":
objectToInject = getObjectName(node.text)
break LookaheadLoop
}
}
}
src.Printf("err = wr.templates[%d].Execute(w, %s)\n", currentPatternIndex, objectToInject)
src.Printf("handle%sError(err)\n", w.properViewName)
// start a new pattern string
pattern.Print("`")
w.writingCodeOutput = true
patternIndex++
}
htmlIndex++
}
// These stop writing patterns, so we need to close off pattern strings
if w.writingCodeOutput && (nodeType == RawValue || nodeType == Execution) {
pattern.Println("`,")
w.writingCodeOutput = false
}
// add call to print output
switch nodeType {
case Literal:
pattern.Print(nd.text)
case EscapedValue:
// print the path to the desired object for the template pattern
patternStr := "."
index := strings.Index(nd.text, ".")
if index > 0 {
patternStr = nd.text[index:]
}
pattern.Printf("{{%s}}", patternStr)
case RawValue:
src.Printf("fmt.Fprint(w, %s)\n", nd.text)
case Execution:
// attempt to keep formatting across user code.
// Here we're checking to see if this is the end of a block statement
// if so, we need to decrease indent
first := getFirstChar(nd.text)
if first == '}' {
src.DecrIndent()
}
// add user's code
src.Printf("%s\n", nd.text)
// If user code ends in {, incr indent as they started a block statement
last := getLastChar(nd.text)
if last == '{' {
src.IncrIndent()
}
}
for n := nd.children.Front(); n != nil; n = n.Next() {
htmlIndex, patternIndex = w.writeNode(n, haml, src, pattern, htmlIndex, patternIndex)
}
return
}
func getFirstChar(s string) byte {
trimmed := strings.TrimLeft(s, "\t ")
if len(trimmed) == 0 {
return byte(0)
}
return trimmed[0]
}
func getLastChar(s string) byte {
trimmed := strings.TrimRight(s, "\t ")
if len(trimmed) == 0 {
return byte(0)
}
return trimmed[len(trimmed)-1]
}
// returns the name of the object to inject into a template.
func getObjectName(s string) string {
index := strings.Index(s, ".")
if index < 0 {
return s
}
return s[0:index]
}
// returns the path to the property to inject
// into a template.
func getObjectPath(s string) string {
index := strings.Index(s, ".")
if index < 0 {
return "."
}
return s[index:]
}