experiment with bce in plist

This commit is contained in:
Azareal 2020-03-05 14:43:40 +10:00
parent f5118d1b11
commit 8f21d34964
4 changed files with 45 additions and 23 deletions

View File

@ -208,14 +208,14 @@ func (list SFileList) JSTmplInit() error {
}); });
}`, "\n\t];") }`, "\n\t];")
data = replace(data, `= data = replace(data, `=
}`, "= []") }`, "=[]")
fragset := tmpl.GetFrag(shortName) fragset := tmpl.GetFrag(shortName)
if fragset != nil { if fragset != nil {
sfrags := []byte("let " + shortName + "_frags = [\n") sfrags := []byte("let " + shortName + "_frags = [\n")
for _, frags := range fragset { for _, frags := range fragset {
//sfrags = append(sfrags, []byte(shortName+"_frags.push(`"+string(frags)+"`);\n")...) //sfrags = append(sfrags, []byte(shortName+"_frags.push(`"+string(frags)+"`);\n")...)
sfrags = append(sfrags, []byte("\t`"+string(frags)+"`,\n")...) sfrags = append(sfrags, []byte("`"+string(frags)+"`,\n")...)
} }
sfrags = append(sfrags, []byte("];\n")...) sfrags = append(sfrags, []byte("];\n")...)
data = append(sfrags, data...) data = append(sfrags, data...)

View File

@ -136,6 +136,7 @@ func InitPhrases(lang string) error {
phraseSet[index] = []byte(phrase) phraseSet[index] = []byte(phrase)
} }
langPack.TmplIndicesToPhrases[tmplID] = phraseSet langPack.TmplIndicesToPhrases[tmplID] = phraseSet
TmplIndexCallback(tmplID, phraseSet)
} }
log.Print("Adding the '" + langPack.Name + "' language pack") log.Print("Adding the '" + langPack.Name + "' language pack")
@ -311,7 +312,7 @@ func getPlaceholderBytes(prefix, suffix string) []byte {
return []byte("{lang." + prefix + "[" + suffix + "]}") return []byte("{lang." + prefix + "[" + suffix + "]}")
} }
// Please don't mutate *LanguagePack // ! Please don't mutate *LanguagePack
func GetCurrentLangPack() *LanguagePack { func GetCurrentLangPack() *LanguagePack {
return currentLangPack.Load().(*LanguagePack) return currentLangPack.Load().(*LanguagePack)
} }
@ -361,3 +362,15 @@ func RegisterTmplPhraseNames(phraseNames []string) (tmplID int) {
func GetTmplPhrasesBytes(tmplID int) [][]byte { func GetTmplPhrasesBytes(tmplID int) [][]byte {
return currentLangPack.Load().(*LanguagePack).TmplIndicesToPhrases[tmplID] return currentLangPack.Load().(*LanguagePack).TmplIndicesToPhrases[tmplID]
} }
// New
var indexCallbacks []func([][]byte)
func TmplIndexCallback(tmplID int, phraseSet [][]byte) {
indexCallbacks[tmplID](phraseSet)
}
func AddTmplIndexCallback(h func([][]byte)) {
indexCallbacks = append(indexCallbacks, h)
}

View File

@ -627,7 +627,7 @@ func getTemplateList(c *tmpl.CTemplateSet, wg *sync.WaitGroup, prefix string) st
if !ok { if !ok {
bodyMap[frag.Body] = front bodyMap[frag.Body] = front
var bits string var bits string
DebugLog("encoding frag.Body") DebugLog("encoding f.Body")
for _, char := range []byte(frag.Body) { for _, char := range []byte(frag.Body) {
if char == '\'' { if char == '\'' {
bits += "'\\" + string(char) + "'," bits += "'\\" + string(char) + "',"

View File

@ -465,6 +465,7 @@ func (c *CTemplateSet) compile(name, content, expects string, expectsInt interfa
} else if !c.config.SkipInitBlock { } else if !c.config.SkipInitBlock {
if len(c.langIndexToName) > 0 { if len(c.langIndexToName) > 0 {
fout += "var " + fname + "_tmpl_phrase_id int\n\n" fout += "var " + fname + "_tmpl_phrase_id int\n\n"
fout += "var " + fname + "_phrase_arr [" + strconv.Itoa(len(c.langIndexToName)) + "][]byte\n\n"
} }
fout += "// nolint\nfunc init() {\n" fout += "// nolint\nfunc init() {\n"
@ -484,6 +485,11 @@ func (c *CTemplateSet) compile(name, content, expects string, expectsInt interfa
fout += "\t\t" + `"` + name + `"` + ",\n" fout += "\t\t" + `"` + name + `"` + ",\n"
} }
fout += "\t})\n" fout += "\t})\n"
fout += ` phrases.AddTmplIndexCallback(func(phraseSet [][]byte) {
copy(` + fname + `_phrase_arr[:], phraseSet)
})
`
} }
fout += "}\n\n" fout += "}\n\n"
} }
@ -513,7 +519,9 @@ if !ok {
} }
if len(c.langIndexToName) > 0 { if len(c.langIndexToName) > 0 {
fout += "var plist = phrases.GetTmplPhrasesBytes(" + fname + "_tmpl_phrase_id)\n" //fout += "var plist = phrases.GetTmplPhrasesBytes(" + fname + "_tmpl_phrase_id)\n"
//fout += "if len(plist) > 0 {\n_ = plist[len(plist)-1]\n}\n"
//fout += "var plist = " + fname + "_phrase_arr\n"
} }
fout += varString fout += varString
@ -526,43 +534,44 @@ if !ok {
} }
for fid := 0; len(outBuf) > fid; fid++ { for fid := 0; len(outBuf) > fid; fid++ {
frame := outBuf[fid] fr := outBuf[fid]
c.detail(frame.Type + " frame") c.detail(fr.Type + " frame")
if frame.Type == "text" { if fr.Type == "text" {
c.detail(frame) c.detail(fr)
oid := fid oid := fid
c.detail("oid:", oid) c.detail("oid:", oid)
skipBlock, ok := skipped[frame.TemplateName] skipBlock, ok := skipped[fr.TemplateName]
if !ok { if !ok {
skipBlock = &SkipBlock{make(map[int]int), 0, 0} skipBlock = &SkipBlock{make(map[int]int), 0, 0}
skipped[frame.TemplateName] = skipBlock skipped[fr.TemplateName] = skipBlock
} }
skip := skipBlock.LastCount skip := skipBlock.LastCount
c.detailf("skipblock %+v\n", skipBlock) c.detailf("skipblock %+v\n", skipBlock)
//var count int //var count int
for len(outBuf) > fid+1 && outBuf[fid+1].Type == "text" && outBuf[fid+1].TemplateName == frame.TemplateName { for len(outBuf) > fid+1 && outBuf[fid+1].Type == "text" && outBuf[fid+1].TemplateName == fr.TemplateName {
c.detail("pre fid:", fid) c.detail("pre fid:", fid)
//count++ //count++
next := outBuf[fid+1] next := outBuf[fid+1]
c.detail("next frame:", next) c.detail("next frame:", next)
c.detail("frame frag:", c.fragBuf[frame.Extra2.(int)]) c.detail("frame frag:", c.fragBuf[fr.Extra2.(int)])
c.detail("next frag:", c.fragBuf[next.Extra2.(int)]) c.detail("next frag:", c.fragBuf[next.Extra2.(int)])
c.fragBuf[frame.Extra2.(int)].Body += c.fragBuf[next.Extra2.(int)].Body c.fragBuf[fr.Extra2.(int)].Body += c.fragBuf[next.Extra2.(int)].Body
c.fragBuf[next.Extra2.(int)].Seen = true c.fragBuf[next.Extra2.(int)].Seen = true
fid++ fid++
skipBlock.LastCount++ skipBlock.LastCount++
skipBlock.Frags[frame.Extra.(int)] = skipBlock.LastCount skipBlock.Frags[fr.Extra.(int)] = skipBlock.LastCount
c.detail("post fid:", fid) c.detail("post fid:", fid)
} }
writeTextFrame(frame.TemplateName, frame.Extra.(int)-skip) writeTextFrame(fr.TemplateName, fr.Extra.(int)-skip)
} else if frame.Type == "varsub" || frame.Type == "cvarsub" { } else if fr.Type == "varsub" || fr.Type == "cvarsub" {
fout += "w.Write(" + frame.Body + ")\n" fout += "w.Write(" + fr.Body + ")\n"
} else if frame.Type == "identifier" { } else if fr.Type == "identifier" {
fout += frame.Body fout += fr.Body
} else if frame.Type == "lang" { } else if fr.Type == "lang" {
fout += "w.Write(plist[" + strconv.Itoa(frame.Extra.(int)) + "])\n" //fout += "w.Write(plist[" + strconv.Itoa(fr.Extra.(int)) + "])\n"
fout += "w.Write(" + fname + "_phrase_arr[" + strconv.Itoa(fr.Extra.(int)) + "])\n"
} else { } else {
fout += frame.Body fout += fr.Body
} }
} }
fout += "return nil\n}\n" fout += "return nil\n}\n"