...

Source file src/github.com/noirbizarre/gonja/builtins/statements/autoescape.go

Documentation: github.com/noirbizarre/gonja/builtins/statements

     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 AutoescapeStmt struct {
    13  	Wrapper    *nodes.Wrapper
    14  	Autoescape bool
    15  }
    16  
    17  func (stmt *AutoescapeStmt) Position() *tokens.Token { return stmt.Wrapper.Position() }
    18  func (stmt *AutoescapeStmt) String() string {
    19  	t := stmt.Position()
    20  	return fmt.Sprintf("AutoescapeStmt(Line=%d Col=%d)", t.Line, t.Col)
    21  }
    22  
    23  func (stmt *AutoescapeStmt) Execute(r *exec.Renderer, tag *nodes.StatementBlock) error {
    24  	sub := r.Inherit()
    25  	sub.Autoescape = stmt.Autoescape
    26  
    27  	err := sub.ExecuteWrapper(stmt.Wrapper)
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	return nil
    33  }
    34  
    35  func autoescapeParser(p *parser.Parser, args *parser.Parser) (nodes.Statement, error) {
    36  	stmt := &AutoescapeStmt{}
    37  
    38  	wrapper, _, err := p.WrapUntil("endautoescape")
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	stmt.Wrapper = wrapper
    43  
    44  	modeToken := args.Match(tokens.Name)
    45  	if modeToken == nil {
    46  		return nil, args.Error("A mode is required for autoescape statement.", nil)
    47  	}
    48  	if modeToken.Val == "true" {
    49  		stmt.Autoescape = true
    50  	} else if modeToken.Val == "false" {
    51  		stmt.Autoescape = false
    52  	} else {
    53  		return nil, args.Error("Only 'true' or 'false' is valid as an autoescape statement.", nil)
    54  	}
    55  
    56  	if !args.Stream.End() {
    57  		return nil, args.Error("Malformed autoescape statement args.", nil)
    58  	}
    59  
    60  	return stmt, nil
    61  }
    62  
    63  func init() {
    64  	All.Register("autoescape", autoescapeParser)
    65  }
    66  

View as plain text