119 lines
3.1 KiB
Go
119 lines
3.1 KiB
Go
package tmpl
|
|
|
|
import (
|
|
"reflect"
|
|
)
|
|
|
|
// For use in generated code
|
|
type FragLite struct {
|
|
Body string
|
|
}
|
|
|
|
type Fragment struct {
|
|
Body string
|
|
TemplateName string
|
|
Index int
|
|
Seen bool
|
|
}
|
|
|
|
type OutBufferFrame struct {
|
|
Body string
|
|
Type string
|
|
TemplateName string
|
|
Extra interface{}
|
|
Extra2 interface{}
|
|
}
|
|
|
|
type CContext struct {
|
|
RootHolder string
|
|
VarHolder string
|
|
HoldReflect reflect.Value
|
|
RootTemplateName string
|
|
TemplateName string
|
|
LoopDepth int
|
|
OutBuf *[]OutBufferFrame
|
|
}
|
|
|
|
func (con *CContext) Push(nType string, body string) (index int) {
|
|
*con.OutBuf = append(*con.OutBuf, OutBufferFrame{body, nType, con.TemplateName, nil, nil})
|
|
return con.LastBufIndex()
|
|
}
|
|
|
|
func (con *CContext) PushText(body string, fragIndex int, fragOutIndex int) (index int) {
|
|
*con.OutBuf = append(*con.OutBuf, OutBufferFrame{body, "text", con.TemplateName, fragIndex, fragOutIndex})
|
|
return con.LastBufIndex()
|
|
}
|
|
|
|
func (con *CContext) PushPhrase(langIndex int) (index int) {
|
|
*con.OutBuf = append(*con.OutBuf, OutBufferFrame{"", "lang", con.TemplateName, langIndex, nil})
|
|
return con.LastBufIndex()
|
|
}
|
|
|
|
func (con *CContext) PushPhrasef(langIndex int, args string) (index int) {
|
|
*con.OutBuf = append(*con.OutBuf, OutBufferFrame{args, "langf", con.TemplateName, langIndex, nil})
|
|
return con.LastBufIndex()
|
|
}
|
|
|
|
func (con *CContext) StartIf(body string) (index int) {
|
|
*con.OutBuf = append(*con.OutBuf, OutBufferFrame{body, "startif", con.TemplateName, false, nil})
|
|
return con.LastBufIndex()
|
|
}
|
|
func (con *CContext) StartIfPtr(body string) (index int) {
|
|
*con.OutBuf = append(*con.OutBuf, OutBufferFrame{body, "startif", con.TemplateName, true, nil})
|
|
return con.LastBufIndex()
|
|
}
|
|
|
|
func (con *CContext) EndIf(startIndex int, body string) (index int) {
|
|
*con.OutBuf = append(*con.OutBuf, OutBufferFrame{body, "endif", con.TemplateName, startIndex, nil})
|
|
return con.LastBufIndex()
|
|
}
|
|
|
|
func (con *CContext) StartLoop(body string) (index int) {
|
|
con.LoopDepth++
|
|
return con.Push("startloop", body)
|
|
}
|
|
|
|
func (con *CContext) EndLoop(body string) (index int) {
|
|
return con.Push("endloop", body)
|
|
}
|
|
|
|
func (con *CContext) StartTemplate(body string) (index int) {
|
|
return con.addFrame(body, "starttemplate", nil, nil)
|
|
}
|
|
|
|
func (con *CContext) EndTemplate(body string) (index int) {
|
|
return con.Push("endtemplate", body)
|
|
}
|
|
|
|
func (con *CContext) AttachVars(vars string, index int) {
|
|
outBuf := *con.OutBuf
|
|
n := outBuf[index]
|
|
if n.Type != "starttemplate" && n.Type != "startloop" && n.Type != "startif" {
|
|
panic("not a starttemplate, startloop or startif node")
|
|
}
|
|
n.Body += vars
|
|
outBuf[index] = n
|
|
}
|
|
|
|
func (con *CContext) addFrame(body, ftype string, extra1 interface{}, extra2 interface{}) (index int) {
|
|
*con.OutBuf = append(*con.OutBuf, OutBufferFrame{body, ftype, con.TemplateName, extra1, extra2})
|
|
return con.LastBufIndex()
|
|
}
|
|
|
|
func (con *CContext) LastBufIndex() int {
|
|
return len(*con.OutBuf) - 1
|
|
}
|
|
|
|
func (con *CContext) DiscardAndAfter(index int) {
|
|
outBuf := *con.OutBuf
|
|
if len(outBuf) <= index {
|
|
return
|
|
}
|
|
if index == 0 {
|
|
outBuf = nil
|
|
} else {
|
|
outBuf = outBuf[:index]
|
|
}
|
|
*con.OutBuf = outBuf
|
|
}
|