2018-11-18 09:28:26 +00:00
|
|
|
package tmpl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2018-11-22 07:21:43 +00:00
|
|
|
// For use in generated code
|
|
|
|
type FragLite struct {
|
|
|
|
Body string
|
|
|
|
}
|
|
|
|
|
2018-11-19 23:06:15 +00:00
|
|
|
type Fragment struct {
|
|
|
|
Body string
|
|
|
|
TemplateName string
|
|
|
|
Index int
|
|
|
|
Seen bool
|
|
|
|
}
|
|
|
|
|
2018-11-18 09:28:26 +00:00
|
|
|
type OutBufferFrame struct {
|
|
|
|
Body string
|
|
|
|
Type string
|
|
|
|
TemplateName string
|
2018-11-19 23:06:15 +00:00
|
|
|
Extra interface{}
|
|
|
|
Extra2 interface{}
|
2018-11-18 09:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type CContext struct {
|
2018-11-26 05:08:10 +00:00
|
|
|
RootHolder string
|
2018-11-18 09:28:26 +00:00
|
|
|
VarHolder string
|
|
|
|
HoldReflect reflect.Value
|
|
|
|
TemplateName string
|
2018-11-22 07:21:43 +00:00
|
|
|
LoopDepth int
|
2018-11-18 09:28:26 +00:00
|
|
|
OutBuf *[]OutBufferFrame
|
|
|
|
}
|
|
|
|
|
2018-11-19 23:06:15 +00:00
|
|
|
func (con *CContext) Push(nType string, body string) (index int) {
|
|
|
|
*con.OutBuf = append(*con.OutBuf, OutBufferFrame{body, nType, con.TemplateName, nil, nil})
|
2018-11-26 05:08:10 +00:00
|
|
|
return con.LastBufIndex()
|
2018-11-18 09:28:26 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 23:06:15 +00:00
|
|
|
func (con *CContext) PushText(body string, fragIndex int, fragOutIndex int) (index int) {
|
|
|
|
*con.OutBuf = append(*con.OutBuf, OutBufferFrame{body, "text", con.TemplateName, fragIndex, fragOutIndex})
|
2018-11-26 05:08:10 +00:00
|
|
|
return con.LastBufIndex()
|
2018-11-18 09:28:26 +00:00
|
|
|
}
|
2018-11-22 07:21:43 +00:00
|
|
|
|
|
|
|
func (con *CContext) PushPhrase(body string, langIndex int) (index int) {
|
|
|
|
*con.OutBuf = append(*con.OutBuf, OutBufferFrame{body, "lang", con.TemplateName, langIndex, nil})
|
2018-11-26 05:08:10 +00:00
|
|
|
return con.LastBufIndex()
|
2018-11-22 07:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2018-11-26 05:08:10 +00:00
|
|
|
return con.addFrame(body, "starttemplate", nil, nil)
|
2018-11-22 07:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (con *CContext) EndTemplate(body string) (index int) {
|
|
|
|
return con.Push("endtemplate", body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (con *CContext) AttachVars(vars string, index int) {
|
|
|
|
outBuf := *con.OutBuf
|
|
|
|
node := outBuf[index]
|
|
|
|
if node.Type != "starttemplate" && node.Type != "startloop" {
|
|
|
|
panic("not a starttemplate node")
|
|
|
|
}
|
|
|
|
node.Body += vars
|
|
|
|
outBuf[index] = node
|
|
|
|
}
|
2018-11-26 05:08:10 +00:00
|
|
|
|
|
|
|
func (con *CContext) addFrame(body string, 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
|
|
|
|
}
|