...
1 package statements
2
3 import (
4 "fmt"
5
6 "github.com/noirbizarre/gonja/exec"
7 "github.com/noirbizarre/gonja/nodes"
8 "github.com/noirbizarre/gonja/parser"
9 "github.com/noirbizarre/gonja/tokens"
10 )
11
12 type RawStmt struct {
13 Data *nodes.Data
14
15 }
16
17 func (stmt *RawStmt) Position() *tokens.Token { return stmt.Data.Position() }
18 func (stmt *RawStmt) String() string {
19 t := stmt.Position()
20 return fmt.Sprintf("RawStmt(Line=%d Col=%d)", t.Line, t.Col)
21 }
22
23 func (stmt *RawStmt) Execute(r *exec.Renderer, tag *nodes.StatementBlock) error {
24 r.WriteString(stmt.Data.Data.Val)
25
26
27
28
29
30
31
32
33 return nil
34 }
35
36 func rawParser(p *parser.Parser, args *parser.Parser) (nodes.Statement, error) {
37 stmt := &RawStmt{}
38
39 wrapper, _, err := p.WrapUntil("endraw")
40 if err != nil {
41 return nil, err
42 }
43 node := wrapper.Nodes[0]
44 data, ok := node.(*nodes.Data)
45 if ok {
46 stmt.Data = data
47 } else {
48 return nil, p.Error("raw statement can only contains a single data node", node.Position())
49 }
50
51 if !args.End() {
52 return nil, args.Error("raw statement doesn't accept parameters.", args.Current())
53 }
54
55 return stmt, nil
56 }
57
58 func init() {
59 All.Register("raw", rawParser)
60 }
61
View as plain text