jinja2

package module
v0.0.0-...-341e3a0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 10, 2019 License: GPL-3.0 Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-jinja2

Jinja2, in Go!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	PythonLexer = lexer.Unquote(lexer.Must(lexer.Regexp(`(\s+)`+
		`|(?P<Bool>[Tt]rue|[Ff]alse)`+
		`|(?P<Ident>[a-zA-Z_][a-zA-Z0-9_]*)`+
		`|(?P<String>'[^']*'|"[^"]*")`+
		`|(?P<Operators>\||<>|==|!=|<=|>=|[-+*/%,.=<>])`+
		`|(?P<Delimiters>[()\[\]{}:])`+
		`|(?P<None>None)`+
		`|(?P<Float>[-+]?\d*\.\d+([eE][-+]?\d+)?)`+
		`|(?P<Int>[-+]?\d*)`+
		`|(?P<Keyword>or|and|is|in|not|if|elif|else)`,
	)), "String")
)

Functions

func GetNextId

func GetNextId(input string, start int) (string, int, error)

func PeekToken

func PeekToken(token Token) string

func PrettyPrint

func PrettyPrint(v interface{})

func PyTypeToString

func PyTypeToString(v PyType) string

func VariableResToString

func VariableResToString(res VariableType) (string, error)

Types

type AndTest

type AndTest struct {
	Nots []*NotTest `@@ { "and" @@ }`
}

-------------------------------------------------------------------------------------------------

func (*AndTest) Eval

func (self *AndTest) Eval(c *Context) (VariableType, error)

type AnonArgument

type AnonArgument struct {
	ArgValue *Expr `@@`
}

-------------------------------------------------------------------------------------------------

func (*AnonArgument) Eval

func (self *AnonArgument) Eval(c *Context) (VariableType, error)

type ArgList

type ArgList struct {
	Arguments []*Argument `"(" [ @@ { "," @@ }[","] ] ")"`
}

-------------------------------------------------------------------------------------------------

func (*ArgList) Eval

func (self *ArgList) Eval(c *Context) (VariableType, error)

type Argument

type Argument struct {
	//NamedArg  *NamedArgument  `  @@`
	//AnonArg   *AnonArgument   `| @@`
	AnonValue *Test   `@@ |`
	Name      *string `( @Ident`
	Value     *Test   `"=" @@ )`
}

-------------------------------------------------------------------------------------------------

func (*Argument) Eval

func (self *Argument) Eval(c *Context) (VariableType, error)

type ArithExpr

type ArithExpr struct {
	Lhs *Term     `@@`
	Rhs []*OpTerm `{ @@ }`
}

-------------------------------------------------------------------------------------------------

func (*ArithExpr) Eval

func (self *ArithExpr) Eval(c *Context) (VariableType, error)

type Atom

type Atom struct {
	Name  *string      `  @Ident`
	Str   *string      `| @String`
	Float *float64     `| @Float`
	Int   *int64       `| @Int`
	Bool  *string      `| @Bool`
	None  *string      `| @None`
	List  *ListDisplay `| @@`
	Dict  *DictDisplay `| @@`
}

-------------------------------------------------------------------------------------------------

func (*Atom) Eval

func (self *Atom) Eval(c *Context) (VariableType, error)

type AtomExpr

type AtomExpr struct {
	Atom     *Atom      `@@`
	Trailers []*Trailer `{ @@ }`
}

-------------------------------------------------------------------------------------------------

func (*AtomExpr) Eval

func (self *AtomExpr) Eval(c *Context) (VariableType, error)

type CallableArg

type CallableArg struct {
	Name  string
	Value VariableType
}

------------------------------------------------------------------------------------------------- The PyCallable describes:

  1. A method used to run something natively in go
  2. A mapping of python variable types. If set to a value other than PY_TYPE_UNDEFINED, this will become the default value when the call is made.

func CreateArgumentList

func CreateArgumentList(a *ArgList, c *Context) ([]CallableArg, error)

type Comparison

type Comparison struct {
	Expr   *Expr     `@@`
	OpExpr []*OpExpr `{ @@ }`
}

-------------------------------------------------------------------------------------------------

func (*Comparison) Eval

func (self *Comparison) Eval(c *Context) (VariableType, error)

type Context

type Context struct {
	Variables map[string]VariableType
	Filters   map[string]PyCallable
	Tests     map[string]PyCallable
	PyCalls   map[string]PyCallable
}

func NewContext

func NewContext(vars map[string]interface{}) *Context

func (*Context) AddVariables

func (self *Context) AddVariables(vars map[string]interface{}) error

func (*Context) LoadDefaultFilters

func (self *Context) LoadDefaultFilters()

func (*Context) LoadDefaultTests

func (self *Context) LoadDefaultTests()

type DictDisplay

type DictDisplay struct {
	Entries []*KeyDatum `"{" [ @@ {"," @@ }[","] ] "}"`
}

-------------------------------------------------------------------------------------------------

func (*DictDisplay) Eval

func (self *DictDisplay) Eval(c *Context) (VariableType, error)

type DummyChunk

type DummyChunk struct {
}

func (*DummyChunk) Render

func (self *DummyChunk) Render(c *Context) (string, error)

type ElifChunk

type ElifChunk struct {
	ElifAst    *ElifStatement
	ElifChunks []Renderable
}

func ParseElif

func ParseElif(tokens []Token, pos int) (int, ElifChunk, error)

type ElifStatement

type ElifStatement struct {
	Test *Test `"elif" @@`
}

-------------------------------------------------------------------------------------------------

func ParseElifStatement

func ParseElifStatement(statement string) (*ElifStatement, error)

func (*ElifStatement) Eval

func (self *ElifStatement) Eval(c *Context) (VariableType, error)

type ElifToken

type ElifToken struct {
	TokenBase
	ElifStatement string
}

type ElseToken

type ElseToken struct {
	TokenBase
}

type EndforToken

type EndforToken struct {
	TokenBase
}

type EndifToken

type EndifToken struct {
	TokenBase
}

type EndrawToken

type EndrawToken struct {
	TokenBase
}

type Expr

type Expr struct {
	Lhs *ArithExpr     `@@`
	Rhs []*OpArithExpr `{ @@ }`
}

-------------------------------------------------------------------------------------------------

func (*Expr) Eval

func (self *Expr) Eval(c *Context) (VariableType, error)

type ExprList

type ExprList struct {
	Exprs []*Expr `@@ {"," @@ }[","]`
}

-------------------------------------------------------------------------------------------------

type Factor

type Factor struct {
	ModFactor *ModFactor `  @@`
	Power     *Power     `| @@`
}

-------------------------------------------------------------------------------------------------

func (*Factor) Eval

func (self *Factor) Eval(c *Context) (VariableType, error)

type ForChunk

type ForChunk struct {
	ForAst     *ForStatement
	IfAst      *IfStatement
	Chunks     []Renderable
	ElseChunks []Renderable
}

func (*ForChunk) Render

func (self *ForChunk) Render(c *Context) (string, error)

type ForStatement

type ForStatement struct {
	TargetList  *TargetList  `"for" @@ `
	TestList    *TestList    `"in" @@`
	IfStatement *IfStatement `[ @@ ]`
	Recursive   bool         `[@"recursive"]`
}

-------------------------------------------------------------------------------------------------

func ParseForStatement

func ParseForStatement(statement string) (*ForStatement, error)

type ForToken

type ForToken struct {
	TokenBase
	ForStatement string
}

type IfChunk

type IfChunk struct {
	IfAst      *IfStatement
	IfChunks   []Renderable
	ElifChunks []ElifChunk
	ElseChunks []Renderable
}

func (*IfChunk) Render

func (self *IfChunk) Render(c *Context) (string, error)

type IfStatement

type IfStatement struct {
	Test *Test `"if" @@`
}

-------------------------------------------------------------------------------------------------

func ParseIfStatement

func ParseIfStatement(statement string) (*IfStatement, error)

func (*IfStatement) Eval

func (self *IfStatement) Eval(c *Context) (VariableType, error)

type IfToken

type IfToken struct {
	TokenBase
	IfStatement string
}

type J2Filter

type J2Filter struct {
	Name *string  `@Ident`
	Args *ArgList `[ @@ ]`
}

-------------------------------------------------------------------------------------------------

type J2Test

type J2Test struct {
	Negated *string  `"is" @[ "not" ]`
	Name    *string  `@Ident`
	Args    *ArgList `[ @@ ]`
}

type KeyDatum

type KeyDatum struct {
	Key   *Expr `@@ ":"`
	Value *Expr `@@`
}

-------------------------------------------------------------------------------------------------

type ListDisplay

type ListDisplay struct {
	Items []*Expr `"[" [ @@ {"," @@ }[","] ] "]"`
}

-------------------------------------------------------------------------------------------------

func (*ListDisplay) Eval

func (self *ListDisplay) Eval(c *Context) (VariableType, error)

type ModFactor

type ModFactor struct {
	Mod    *string `@("+"|"-"|"~")`
	Factor *Factor `@@`
}

-------------------------------------------------------------------------------------------------

func (*ModFactor) Eval

func (self *ModFactor) Eval(c *Context) (VariableType, error)

type NamedArgument

type NamedArgument struct {
	ArgName  *string ` ( @Ident "=" `
	ArgValue *Expr   ` @@ )`
}

-------------------------------------------------------------------------------------------------

func (*NamedArgument) Eval

func (self *NamedArgument) Eval(c *Context) (VariableType, error)

type NotTest

type NotTest struct {
	Negated    *NotTest    `"not" @@`
	Comparison *Comparison `| @@`
}

-------------------------------------------------------------------------------------------------

func (*NotTest) Eval

func (self *NotTest) Eval(c *Context) (VariableType, error)

type OpArithExpr

type OpArithExpr struct {
	Op   *string `@("+"|"-")`
	Term *Term   `@@`
}

-------------------------------------------------------------------------------------------------

func (*OpArithExpr) Eval

func (self *OpArithExpr) Eval(c *Context) (VariableType, error)

type OpExpr

type OpExpr struct {
	Op        *string    `@("<"|">"|"=="|">="|"<="|"<>"|"!="|"in"|"is"|"not" "in"|"is" "not")`
	ArithExpr *ArithExpr `@@`
}

-------------------------------------------------------------------------------------------------

func (*OpExpr) Eval

func (self *OpExpr) Eval(c *Context) (VariableType, error)

type OpTerm

type OpTerm struct {
	Op     *string `@("*"|"/"|"%"|"//")`
	Factor *Factor `@@`
}

-------------------------------------------------------------------------------------------------

func (*OpTerm) Eval

func (self *OpTerm) Eval(c *Context) (VariableType, error)

type OrTest

type OrTest struct {
	Ands []*AndTest `@@ { "or" @@ }`
}

-------------------------------------------------------------------------------------------------

func (*OrTest) Eval

func (self *OrTest) Eval(c *Context) (VariableType, error)

type Power

type Power struct {
	AtomExpr *AtomExpr   `@@`
	Factor   *Factor     `[ "**" @@ ]`
	Filters  []*J2Filter `{ "|" @@ }`
	Test     *J2Test     `[ @@ ]`
}

-------------------------------------------------------------------------------------------------

func (*Power) Eval

func (self *Power) Eval(c *Context) (VariableType, error)

type PyCallable

type PyCallable struct {
	Method func([]VariableType) (VariableType, error)
	Args   []CallableArg
}

type PyType

type PyType int
const (
	PY_TYPE_UNDEFINED PyType = 0
	PY_TYPE_NONE      PyType = 1
	PY_TYPE_STRING    PyType = 2
	PY_TYPE_INT       PyType = 3
	PY_TYPE_FLOAT     PyType = 4
	PY_TYPE_BOOL      PyType = 5
	PY_TYPE_LIST      PyType = 6
	PY_TYPE_TUPLE     PyType = 7
	PY_TYPE_DICT      PyType = 8
	PY_TYPE_IDENT     PyType = 10
)

func InterfaceToPyType

func InterfaceToPyType(v interface{}) PyType

type RawChunk

type RawChunk struct {
	Content string
}

func (*RawChunk) Render

func (self *RawChunk) Render(c *Context) (string, error)

type RawToken

type RawToken struct {
	TokenBase
	Content string
}

type Renderable

type Renderable interface {
	Render(*Context) (string, error)
}

func ParseBlocks

func ParseBlocks(tokens []Token, pos int, inside string) (int, []Renderable, error)

func ParseElse

func ParseElse(tokens []Token, pos int, in string) (int, []Renderable, error)

func ParseFor

func ParseFor(tokens []Token, pos int) (int, Renderable, error)

func ParseIf

func ParseIf(tokens []Token, pos int) (int, Renderable, error)

func ParseRaw

func ParseRaw(tokens []Token, pos int) (int, Renderable, error)

func ParseText

func ParseText(tokens []Token, pos int) (int, Renderable, error)

func ParseVariable

func ParseVariable(tokens []Token, pos int) (int, Renderable, error)

type Target

type Target struct {
	Name *string `@Ident`
}

-------------------------------------------------------------------------------------------------

type TargetList

type TargetList struct {
	Targets []*Target ` @@ { "," @@ }[","] `
}

-------------------------------------------------------------------------------------------------

type Template

type Template struct {
	// contains filtered or unexported fields
}

func (*Template) Parse

func (self *Template) Parse(data string) error

func (*Template) Render

func (self *Template) Render(c *Context) (string, error)

type Term

type Term struct {
	Factor *Factor `@@`
}

-------------------------------------------------------------------------------------------------

func (*Term) Eval

func (self *Term) Eval(c *Context) (VariableType, error)

type Test

type Test struct {
	Or *OrTest ` @@ `
}

-------------------------------------------------------------------------------------------------

func (*Test) Eval

func (self *Test) Eval(c *Context) (VariableType, error)

type TestList

type TestList struct {
	Tests []*Test `@@ { "," @@ }[","]`
}

-------------------------------------------------------------------------------------------------

type TextChunk

type TextChunk struct {
	Text string
}

func (*TextChunk) Render

func (self *TextChunk) Render(c *Context) (string, error)

type TextToken

type TextToken struct {
	TokenBase
	Text string
}

type Token

type Token interface {
}

func Tokenize

func Tokenize(input string) []Token

type TokenBase

type TokenBase struct {
	Pos, Line               int
	StripBefore, StripAfter bool
}

type TokenBoundary

type TokenBoundary struct {
	Val        string
	Pos        int
	Line       int
	NewLinePos int
}

func FindTokenBoundaries

func FindTokenBoundaries(input string) []TokenBoundary

type Trailer

type Trailer struct {
	ArgList *ArgList `  @@`
	Name    *string  `| "." @Ident`
}

-------------------------------------------------------------------------------------------------

type TupleDisplay

type TupleDisplay struct {
	Items []*Expr `"(" @@ {"," @@ }[","] ")"`
}

-------------------------------------------------------------------------------------------------

type VariableChunk

type VariableChunk struct {
	VarAst *VariableStatement
}

func (*VariableChunk) Render

func (self *VariableChunk) Render(c *Context) (string, error)

type VariableStatement

type VariableStatement struct {
	Test *Test `@@`
}

-------------------------------------------------------------------------------------------------

func ParseVariableStatement

func ParseVariableStatement(statement string) (*VariableStatement, error)

func (*VariableStatement) Eval

func (self *VariableStatement) Eval(c *Context) (VariableType, error)

type VariableToken

type VariableToken struct {
	TokenBase
	Content string
}

type VariableType

type VariableType struct {
	Type PyType
	Data interface{}
}

func GoVarToPyVar

func GoVarToPyVar(v interface{}) (VariableType, error)

func MakeCall

func MakeCall(call PyCallable, incoming_args []CallableArg, c *Context) (VariableType, error)

func ProcessJ2Filters

func ProcessJ2Filters(val VariableType, filters []*J2Filter, c *Context) (VariableType, error)

func ProcessJ2Test

func ProcessJ2Test(val VariableType, test *J2Test, c *Context) (VariableType, error)

-------------------------------------------------------------------------------------------------

func (*VariableType) AsBool

func (self *VariableType) AsBool() (bool, error)

func (*VariableType) AsFloat

func (self *VariableType) AsFloat() (float64, error)

func (*VariableType) AsInt

func (self *VariableType) AsInt() (int64, error)

func (*VariableType) AsString

func (self *VariableType) AsString() (string, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址