-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtemplate.go
677 lines (562 loc) · 15.8 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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
package pongo
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"runtime/debug"
"strings"
)
const (
nContent = iota
nFilter
nTag
)
type contentNode struct {
line int
col int
content string
}
type filterNode struct {
line int
col int
content string
e *expr
}
type tagNode struct {
line int
col int
content string
tagname string
tagargs string
taghandler *TagHandler
ident string // tag identifier, like 'if'
args []string // string list of arguments
}
type node interface {
// A node must implement a execute() function which gets called when the template is executed
execute(*executionContext, *Context) (*string, error)
getLine() int
getCol() int
getContent() *string
}
// This context contains all running information; it's access
// is synchronized to ensure thread-safety
type executionContext struct {
template *Template
node_pos int
internal_context Context
}
type templateLocator func(*string) (*string, error)
type Template struct {
name string // e.g. the filename, used for error messages
// Parsing stuff
parsed bool
raw string
rawLen int
pos int
start int
length int
// Error handling for parsing
parseErr string // contains nothing if there was no parsing error
line int
col int
// Parsed stuff
autosafe bool
nodes []node
locator templateLocator
// Static content (doesn't change with execution)
cache map[string]interface{}
// Debugging
debug bool
}
type stateFunc func(*Template) stateFunc
func processComment(tpl *Template) stateFunc {
c, success := tpl.getChar(0)
if !success {
tpl.parseErr = "File end reached within comment"
return nil
}
if c == '#' {
// Check next char for }
nc, success := tpl.getChar(1) // curr + 1
if !success {
tpl.parseErr = "File end reached within comment"
return nil
}
if nc == '}' {
tpl.fastForward(2)
tpl.start = tpl.pos // Skip whole comment, start after comment
return processContent
}
}
tpl.fastForward(1)
return processComment
}
func processFilter(tpl *Template) stateFunc {
c, success := tpl.getChar(0)
if !success {
tpl.parseErr = "File end reached within filter"
return nil
}
if c == '}' {
// Check next char for }
nc, success := tpl.getChar(1) // curr + 1
if !success {
tpl.parseErr = "File end reached within filter"
return nil
}
if nc == '}' {
// Add new filter node
err := addFilterNode(tpl)
if err != nil {
tpl.parseErr = err.Error()
return nil
}
// Go back to content
tpl.fastForward(2) // Ignore }}
tpl.start = tpl.pos
return processContent
}
}
tpl.length++
tpl.fastForward(1)
return processFilter
}
func processTag(tpl *Template) stateFunc {
c, success := tpl.getChar(0)
if !success {
tpl.parseErr = "File end reached within tag"
return nil
}
if c == '%' {
// Check next char for }
nc, success := tpl.getChar(1) // curr + 1
if !success {
tpl.parseErr = "File end reached within tag"
return nil
}
if nc == '}' {
// Add new filter node
err := addTagNode(tpl)
if err != nil {
tpl.parseErr = err.Error()
return nil
}
// Go back to content
tpl.fastForward(2) // Ignore }}
tpl.start = tpl.pos
return processContent
}
}
tpl.length++
tpl.fastForward(1)
return processTag
}
func processContent(tpl *Template) stateFunc {
// Check if we reached the end
c, success := tpl.getChar(0)
if !success {
addContentNode(tpl)
return nil
}
if c == '{' {
// Get next char
nc, success := tpl.getChar(1)
if !success {
tpl.parseErr = "File end reached (after opening '{')"
return nil
}
switch nc {
case '#':
tpl.fastForward(2) // skip {#
addContentNode(tpl)
tpl.start = tpl.pos
return processComment
case '%':
tpl.fastForward(2) // skip {%
addContentNode(tpl)
tpl.start = tpl.pos
return processTag
case '{':
tpl.fastForward(2) // skip {{
addContentNode(tpl)
tpl.start = tpl.pos
return processFilter
default:
// Ignore this, because template could look like:
// <script>if (true) { ... } </script>
// See issue #1
}
}
tpl.length++
tpl.fastForward(1)
return processContent
}
func addContentNode(tpl *Template) {
if tpl.length == 0 {
return
}
cn := &contentNode{
line: tpl.line,
col: tpl.col,
content: tpl.raw[tpl.start : tpl.start+tpl.length],
}
tpl.start = tpl.pos
tpl.length = 0
tpl.nodes = append(tpl.nodes, cn)
}
func (cn *contentNode) getCol() int { return cn.col }
func (cn *contentNode) getLine() int { return cn.line }
func (cn *contentNode) getContent() *string { return &cn.content }
func (cn *contentNode) execute(execCtx *executionContext, ctx *Context) (*string, error) {
return &cn.content, nil
}
func addFilterNode(tpl *Template) error {
if tpl.length == 0 {
return errors.New("Empty filter")
}
fn := &filterNode{
line: tpl.line,
col: tpl.col,
content: strings.TrimSpace(tpl.raw[tpl.start : tpl.start+tpl.length]),
}
e, err := newExpr(&fn.content)
if err != nil {
return err
}
// Add 'safe' filter to those filter calls to make them
// safe
if tpl.autosafe {
e.addFilter("safe")
}
fn.e = e
tpl.start = tpl.pos
tpl.length = 0
tpl.nodes = append(tpl.nodes, fn)
return nil
}
func (fn *filterNode) getCol() int { return fn.col }
func (fn *filterNode) getLine() int { return fn.line }
func (fn *filterNode) getContent() *string { return &fn.content }
func (fn *filterNode) execute(execCtx *executionContext, ctx *Context) (*string, error) {
//fmt.Printf("<filter '%s' expr=%s>\n", fn.content, fn.e)
out, err := fn.e.evalString(ctx)
/*if err != nil {
return "", err, 0
}*/
//return out, nil, 1
return out, err
}
func addTagNode(tpl *Template) error {
if tpl.length == 0 {
return errors.New("Empty tag")
}
tn := &tagNode{
line: tpl.line,
col: tpl.col,
content: strings.TrimSpace(tpl.raw[tpl.start : tpl.start+tpl.length]),
}
// Split tagname from tagargs; example: <if> <name|lower == "florian">
args := strings.SplitN(tn.content, " ", 2)
if len(args) < 1 {
return errors.New("Tag must contain at least a name")
}
tagname := args[0]
var tagargs string
if len(args) == 2 {
tagargs = args[1]
}
tag, has_tag := Tags[tagname]
if !has_tag {
return errors.New(fmt.Sprintf("Tag '%s' does not exist", tagname))
}
tn.tagname = tagname
tn.tagargs = strings.TrimSpace(tagargs)
tn.taghandler = tag
tpl.start = tpl.pos
tpl.length = 0
tpl.nodes = append(tpl.nodes, tn)
if tn.taghandler != nil && tn.taghandler.Prepare != nil {
// OK, let's prepare this tag (e. g. pre-cache templates to extend)
if err := tn.taghandler.Prepare(tn, tpl); err != nil {
return errors.New(fmt.Sprintf("Error during preparation of tag '%s': %s", tagname, err))
}
}
return nil
}
func (tn *tagNode) getCol() int { return tn.col }
func (tn *tagNode) getLine() int { return tn.line }
func (tn *tagNode) getContent() *string { return &tn.content }
func (tn *tagNode) execute(execCtx *executionContext, ctx *Context) (*string, error) {
// Split tag from args and call it
// Examples:
// - If-clause: if name|lower == "florian"
// - For-clause: for friend in person.friends
// in general: <tagname> <payload>
if tn.taghandler == nil {
// We reached an unhandled placeholder (maybe 'else' of 'endif' for the if-clause)
return nil, errors.New(fmt.Sprintf("Unhandled placeholder (for example 'endif' for an if-clause): '%s'", tn.tagname))
}
out, err := tn.taghandler.Execute(&tn.tagargs, execCtx, ctx)
return out, err
//return fmt.Sprintf("<tag='%s'>", tn.content), nil, 1
}
// The Must function is a little helper to create a template instance from string/file.
// It checks whether FromString/FromFile returns an error; if so, it panics.
// If not, it returns the template instance. Is's primarily used like this:
// var tplExample = template.Must(template.FromFile("example.html", nil))
func Must(t *Template, err error) *Template {
if err != nil {
panic(err)
}
return t
}
// Reads a template from file. If there's no templateLocator provided,
// one will be created to search for files in the same directory the template
// file is located. file_path can either be an absolute filepath or a relative one.
func FromFile(file_path string, locator templateLocator) (*Template, error) {
var err error
// What is file_path?
if !filepath.IsAbs(file_path) {
file_path, err = filepath.Abs(file_path)
if err != nil {
return nil, err
}
}
buf, err := ioutil.ReadFile(file_path)
if err != nil {
return nil, err
}
file_base := filepath.Dir(file_path)
if locator == nil {
// Create a default locator
locator = func(name *string) (*string, error) {
filename := *name
if !filepath.IsAbs(filename) {
filename = filepath.Join(file_base, filename)
}
buf, err := ioutil.ReadFile(filename)
if err != nil {
return nil, errors.New(fmt.Sprintf("Could not find the template '%s' (default file locator): %v", filename, err))
}
bufstr := string(buf)
return &bufstr, nil
}
}
// Get file name from filepath
name := filepath.Base(file_path)
strbuf := string(buf)
tpl, err := newTemplate(name, &strbuf, locator)
if err != nil {
return nil, err
}
err = tpl.parse()
if err != nil {
return nil, err
}
return tpl, nil
}
// Creates a new template instance from string.
func FromString(name string, tplstr *string, locator templateLocator) (*Template, error) {
tpl, err := newTemplate(name, tplstr, locator)
if err != nil {
return nil, err
}
err = tpl.parse()
if err != nil {
return nil, err
}
return tpl, nil
}
func newTemplate(name string, tplstr *string, locator templateLocator) (*Template, error) {
tplLen := len(*tplstr)
if tplLen == 0 {
return nil, errors.New("Template has no content")
}
tpl := &Template{
name: name,
raw: *tplstr,
line: 1,
rawLen: tplLen,
nodes: make([]node, 0, 250),
autosafe: true,
locator: locator,
cache: make(map[string]interface{}),
}
return tpl, nil
}
func (tpl *Template) parse() error {
if tpl.parsed { // Already parsed?
return nil
}
// Check pos=0 charachter (maybe it's a newline!)
tpl.updatePosition()
state := processContent(tpl)
for state != nil {
state = state(tpl)
}
if len(tpl.parseErr) > 0 { // Parsing error occurred?
return errors.New(fmt.Sprintf("[Parsing error: %s] [Line %d, Column %d] %s", tpl.name, tpl.line, tpl.col, tpl.parseErr))
}
tpl.parsed = true
return nil
}
// Executes the template with the given context and writes to http.ResponseWriter
// on success. Context can be nil. Nothing is written on error; instead the error
// is being returned.
func (tpl *Template) ExecuteRW(w http.ResponseWriter, ctx *Context) error {
out, err := tpl.Execute(ctx)
if err != nil {
return err
}
w.Write([]byte(*out))
return nil
}
// Executes the template with the given context (can be nil).
func (tpl *Template) Execute(ctx *Context) (out *string, err error) {
defer func() {
rerr := recover()
if rerr != nil {
// Panic recovered
out = nil
err = errors.New(fmt.Sprintf("Pongo panicked with this error (please report this issue, see console output! You can see the stack trace when activating debugging: tpl.SetDebug(true)): %s", rerr))
if tpl.debug {
fmt.Println("*************************************************************************")
fmt.Println("Due to panicking of pongo, I'm printing the error message and stack here.")
fmt.Printf("Panic message: %s\n", rerr)
fmt.Println("*************************************************************************")
debug.PrintStack()
fmt.Println("*************************************************************************")
}
}
}()
return tpl.execute(ctx, nil)
}
// pongo will print out a stacktrace whenever it panics if set to true.
func (tpl *Template) SetDebug(d bool) {
tpl.debug = d
}
func newExecutionContext(tpl *Template, internalContext *Context) *executionContext {
var ctx Context
if internalContext == nil {
ctx = make(Context)
} else {
ctx = *internalContext
}
return &executionContext{
internal_context: ctx,
template: tpl,
}
}
func (tpl *Template) execute(ctx *Context, execCtx *executionContext) (*string, error) {
if execCtx == nil {
execCtx = newExecutionContext(tpl, nil)
}
if ctx == nil {
ctx = &Context{}
}
return execCtx.execute(ctx)
}
func (execCtx *executionContext) execute(ctx *Context) (*string, error) {
renderedStrings := make([]string, 0, len(execCtx.template.nodes))
// TODO: We could replace this code by executeUntilAnyTagNode(ctx), but
// it then includes some more interface checks which could hurt performance.
// Not sure about this.
execCtx.node_pos = 0
for execCtx.node_pos < len(execCtx.template.nodes) {
node := execCtx.template.nodes[execCtx.node_pos]
str, err := node.execute(execCtx, ctx)
if err != nil {
return nil, errors.New(fmt.Sprintf("[Error: %s] [Line %d Col %d (%s)] %s", execCtx.template.name, node.getLine(), node.getCol(), *node.getContent(), err))
}
renderedStrings = append(renderedStrings, *str)
execCtx.node_pos++
}
outputString := strings.Join(renderedStrings, "")
return &outputString, nil
}
func (execCtx *executionContext) executeUntilAnyTagNode(ctx *Context, nodenames ...string) (*tagNode, *[]string, error) {
renderedStrings := make([]string, 0, len(execCtx.template.nodes)-execCtx.node_pos)
// To avoid recursion, we first increase tpl.node_pos by one
// (because the current node pos might point to the tag which calls executeUntilAnyTagNode)
execCtx.node_pos++
for execCtx.node_pos < len(execCtx.template.nodes) {
node := execCtx.template.nodes[execCtx.node_pos]
if tn, is_tag := node.(*tagNode); is_tag {
for _, name := range nodenames {
if tn.tagname == name {
// We have found one of the end-nodes, so generate the template result string and return it to
// the caller
return tn, &renderedStrings, nil
}
}
}
str, err := node.execute(execCtx, ctx)
if err != nil {
return nil, nil, errors.New(fmt.Sprintf("[Error in block-execution: %s] [Line %d Col %d (%s)] %s", execCtx.template.name, node.getLine(), node.getCol(), *node.getContent(), err))
}
renderedStrings = append(renderedStrings, *str)
execCtx.node_pos++
}
// One nodename MUST be executed! Otherwise error.
return nil, nil, errors.New(fmt.Sprintf("No end-node (possible nodes: %v) found.", nodenames))
}
func (execCtx *executionContext) ignoreUntilAnyTagNode(nodenames ...string) (*tagNode, error) {
// To avoid recursion, we first increase tpl.node_pos by one
// (because the current node pos might point to the tag which calls executeUntilAnyTagNode)
execCtx.node_pos++
for execCtx.node_pos < len(execCtx.template.nodes) {
node := execCtx.template.nodes[execCtx.node_pos]
if tn, is_tag := node.(*tagNode); is_tag {
for _, name := range nodenames {
if tn.tagname == name {
// We have found one of the end-nodes, so return now
return tn, nil
}
}
// Is not in nodenames, so ignore the tag!
if tn.taghandler != nil && tn.taghandler.Ignore != nil {
tn.taghandler.Ignore(&tn.tagargs, execCtx)
}
}
execCtx.node_pos++
}
// One nodename MUST be executed! Otherwise error.
return nil, errors.New(fmt.Sprintf("No end-node (possible nodes: %v) found.", nodenames))
}
func (tpl *Template) getChar(rel int) (byte, bool) {
if tpl.hasReachedEnd(rel) {
return 0, false
}
return tpl.raw[tpl.pos+rel], true
}
func (tpl *Template) hasReachedEnd(rel int) bool {
if tpl.pos+rel >= tpl.rawLen {
return true
}
return false
}
func (tpl *Template) fastForward(rel int) bool {
for x := 0; x < rel; x++ {
tpl.pos++
if !tpl.updatePosition() {
return false
}
}
return true
}
// Must be called after every change of pos
func (tpl *Template) updatePosition() bool {
if tpl.hasReachedEnd(0) {
return false
}
if tpl.raw[tpl.pos] == '\n' {
tpl.line++
tpl.col = 0
} else {
tpl.col++
}
return true
}