Fix undefined buttons on edit reply.

Move some logic from files.go into templates.go
Shorten some things in templates.go

Fixes #63
This commit is contained in:
Azareal 2019-11-07 13:53:29 +10:00
parent cc76d399e5
commit 6e8c241d8b
2 changed files with 73 additions and 64 deletions

View File

@ -63,13 +63,13 @@ func (list SFileList) JSTmplInit() error {
return bytes.Replace(data, []byte(replaceThis), []byte(withThis), -1) return bytes.Replace(data, []byte(replaceThis), []byte(withThis), -1)
} }
startIndex, hasFunc := skipAllUntilCharsExist(data, 0, []byte("func init() {")) startIndex, hasFunc := skipAllUntilCharsExist(data, 0, []byte("if(tmplInits===undefined)"))
if !hasFunc { if !hasFunc {
return errors.New("no init function found") return errors.New("no init map found")
} }
data = data[startIndex-len([]byte("func init() {")):] data = data[startIndex-len([]byte("if(tmplInits===undefined)")):]
data = replace(data, "// nolint", "")
data = replace(data, "func ", "function ") data = replace(data, "func ", "function ")
data = replace(data, "function init() {", "if(tmplInits===undefined) var tmplInits = {};\ntmplInits[\""+tmplName+"\"] = ")
data = replace(data, " error {\n", " {\nlet out = \"\"\n") data = replace(data, " error {\n", " {\nlet out = \"\"\n")
funcIndex, hasFunc := skipAllUntilCharsExist(data, 0, []byte("function Template_")) funcIndex, hasFunc := skipAllUntilCharsExist(data, 0, []byte("function Template_"))
if !hasFunc { if !hasFunc {
@ -183,8 +183,7 @@ func (list SFileList) JSTmplInit() error {
data = replace(data, " c.", "") data = replace(data, " c.", "")
data = replace(data, "phrases.", "") data = replace(data, "phrases.", "")
data = replace(data, ", 10;", "") data = replace(data, ", 10;", "")
data = replace(data, shortName+"_tmpl_phrase_id = RegisterTmplPhraseNames([]string{", "[") data = replace(data, "var plist = GetTmplPhrasesBytes("+shortName+"_tmpl_phrase_id)", "const plist = tmplPhrases[\""+tmplName+"\"];")
data = replace(data, "var plist = GetTmplPhrasesBytes("+shortName+"_tmpl_phrase_id)", "let plist = tmplPhrases[\""+tmplName+"\"];")
data = replace(data, "var cached_var_", "let cached_var_") data = replace(data, "var cached_var_", "let cached_var_")
data = replace(data, `tmpl_`+shortName+`_vars, ok := tmpl_`+shortName+`_i.`, `/*`) data = replace(data, `tmpl_`+shortName+`_vars, ok := tmpl_`+shortName+`_i.`, `/*`)
data = replace(data, "[]byte(", "") data = replace(data, "[]byte(", "")

View File

@ -447,7 +447,19 @@ func (c *CTemplateSet) compile(name string, content string, expects string, expe
fout += "// Code generated by Gosora. More below:\n/* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */\n" fout += "// Code generated by Gosora. More below:\n/* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */\n"
fout += "package " + c.config.PackageName + "\n" + importList + "\n" fout += "package " + c.config.PackageName + "\n" + importList + "\n"
if !c.config.SkipInitBlock { if c.lang == "js" {
var l string
if len(c.langIndexToName) > 0 {
for _, name := range c.langIndexToName {
l += "\t" + `"` + name + `"` + ",\n"
}
}
if len(l) > 0 {
l = "\n" + l
}
fout += "if(tmplInits===undefined) var tmplInits = {}\n"
fout += "tmplInits[\"template_" + fname + "\"] = [" + l + "]\n"
} 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"
} }
@ -969,8 +981,9 @@ func (c *CTemplateSet) compileExprSwitch(con CContext, node *parse.CommandNode)
} }
func (c *CTemplateSet) unknownNode(node parse.Node) { func (c *CTemplateSet) unknownNode(node parse.Node) {
c.logger.Println("Unknown Kind:", reflect.ValueOf(node).Elem().Kind()) elem := reflect.ValueOf(node).Elem()
c.logger.Println("Unknown Type:", reflect.ValueOf(node).Elem().Type().Name()) c.logger.Println("Unknown Kind:", elem.Kind())
c.logger.Println("Unknown Type:", elem.Type().Name())
panic("I don't know what node this is! Grr...") panic("I don't know what node this is! Grr...")
} }
@ -994,7 +1007,6 @@ func (c *CTemplateSet) compareFunc(con CContext, pos int, node *parse.CommandNod
func (c *CTemplateSet) simpleMath(con CContext, pos int, node *parse.CommandNode, symbol string) (out string, val reflect.Value) { func (c *CTemplateSet) simpleMath(con CContext, pos int, node *parse.CommandNode, symbol string) (out string, val reflect.Value) {
leftParam, val2 := c.compileIfVarSub(con, node.Args[pos+1].String()) leftParam, val2 := c.compileIfVarSub(con, node.Args[pos+1].String())
rightParam, val3 := c.compileIfVarSub(con, node.Args[pos+2].String()) rightParam, val3 := c.compileIfVarSub(con, node.Args[pos+2].String())
if val2.IsValid() { if val2.IsValid() {
val = val2 val = val2
} else if val3.IsValid() { } else if val3.IsValid() {
@ -1070,8 +1082,8 @@ ArgLoop:
val = rval val = rval
break ArgLoop break ArgLoop
case "elapsed": case "elapsed":
leftOperand := node.Args[pos+1].String() leftOp := node.Args[pos+1].String()
leftParam, _ := c.compileIfVarSub(con, leftOperand) leftParam, _ := c.compileIfVarSub(con, leftOp)
// TODO: Refactor this // TODO: Refactor this
// TODO: Validate that this is actually a time.Time // TODO: Validate that this is actually a time.Time
litString("time.Since("+leftParam+").String()", false) litString("time.Since("+leftParam+").String()", false)
@ -1079,19 +1091,19 @@ ArgLoop:
break ArgLoop break ArgLoop
case "dock": case "dock":
// TODO: Implement string literals properly // TODO: Implement string literals properly
leftOperand := node.Args[pos+1].String() leftOp := node.Args[pos+1].String()
rightOperand := node.Args[pos+2].String() rightOp := node.Args[pos+2].String()
if len(leftOperand) == 0 || len(rightOperand) == 0 { if len(leftOp) == 0 || len(rightOp) == 0 {
panic("The left or right operand for function dock cannot be left blank") panic("The left or right operand for function dock cannot be left blank")
} }
leftParam := leftOperand leftParam := leftOp
if leftOperand[0] != '"' { if leftOp[0] != '"' {
leftParam, _ = c.compileIfVarSub(con, leftParam) leftParam, _ = c.compileIfVarSub(con, leftParam)
} }
if rightOperand[0] == '"' { if rightOp[0] == '"' {
panic("The right operand for function dock cannot be a string") panic("The right operand for function dock cannot be a string")
} }
rightParam, val3 := c.compileIfVarSub(con, rightOperand) rightParam, val3 := c.compileIfVarSub(con, rightOp)
if !val3.IsValid() { if !val3.IsValid() {
panic("val3 is invalid") panic("val3 is invalid")
} }
@ -1102,19 +1114,19 @@ ArgLoop:
break ArgLoop break ArgLoop
case "hasWidgets": case "hasWidgets":
// TODO: Implement string literals properly // TODO: Implement string literals properly
leftOperand := node.Args[pos+1].String() leftOp := node.Args[pos+1].String()
rightOperand := node.Args[pos+2].String() rightOp := node.Args[pos+2].String()
if len(leftOperand) == 0 || len(rightOperand) == 0 { if len(leftOp) == 0 || len(rightOp) == 0 {
panic("The left or right operand for function dock cannot be left blank") panic("The left or right operand for function dock cannot be left blank")
} }
leftParam := leftOperand leftParam := leftOp
if leftOperand[0] != '"' { if leftOp[0] != '"' {
leftParam, _ = c.compileIfVarSub(con, leftParam) leftParam, _ = c.compileIfVarSub(con, leftParam)
} }
if rightOperand[0] == '"' { if rightOp[0] == '"' {
panic("The right operand for function dock cannot be a string") panic("The right operand for function dock cannot be a string")
} }
rightParam, val3 := c.compileIfVarSub(con, rightOperand) rightParam, val3 := c.compileIfVarSub(con, rightOp)
if !val3.IsValid() { if !val3.IsValid() {
panic("val3 is invalid") panic("val3 is invalid")
} }
@ -1126,19 +1138,19 @@ ArgLoop:
break ArgLoop break ArgLoop
case "lang": case "lang":
// TODO: Implement string literals properly // TODO: Implement string literals properly
leftOperand := node.Args[pos+1].String() leftOp := node.Args[pos+1].String()
if len(leftOperand) == 0 { if len(leftOp) == 0 {
panic("The left operand for the language string cannot be left blank") panic("The left operand for the language string cannot be left blank")
} }
if leftOperand[0] == '"' { if leftOp[0] == '"' {
// ! Slightly crude but it does the job // ! Slightly crude but it does the job
leftParam := strings.Replace(leftOperand, "\"", "", -1) leftParam := strings.Replace(leftOp, "\"", "", -1)
c.langIndexToName = append(c.langIndexToName, leftParam) c.langIndexToName = append(c.langIndexToName, leftParam)
notident = true notident = true
con.PushPhrase(len(c.langIndexToName) - 1) con.PushPhrase(len(c.langIndexToName) - 1)
} else { } else {
leftParam := leftOperand leftParam := leftOp
if leftOperand[0] != '"' { if leftOp[0] != '"' {
leftParam, _ = c.compileIfVarSub(con, leftParam) leftParam, _ = c.compileIfVarSub(con, leftParam)
} }
// TODO: Add an optimisation if it's a string literal passsed in from a parent template rather than a true dynamic // TODO: Add an optimisation if it's a string literal passsed in from a parent template rather than a true dynamic
@ -1148,11 +1160,11 @@ ArgLoop:
break ArgLoop break ArgLoop
case "langf": case "langf":
// TODO: Implement string literals properly // TODO: Implement string literals properly
leftOperand := node.Args[pos+1].String() leftOp := node.Args[pos+1].String()
if len(leftOperand) == 0 { if len(leftOp) == 0 {
panic("The left operand for the language string cannot be left blank") panic("The left operand for the language string cannot be left blank")
} }
if leftOperand[0] != '"' { if leftOp[0] != '"' {
panic("Phrase names cannot be dynamic") panic("Phrase names cannot be dynamic")
} }
@ -1193,27 +1205,27 @@ ArgLoop:
// TODO: Implement string literals properly // TODO: Implement string literals properly
// ! Slightly crude but it does the job // ! Slightly crude but it does the job
litString("phrases.GetTmplPhrasef("+leftOperand+ob+")", false) litString("phrases.GetTmplPhrasef("+leftOp+ob+")", false)
c.importMap[langPkg] = langPkg c.importMap[langPkg] = langPkg
break ArgLoop break ArgLoop
case "level": case "level":
// TODO: Implement level literals // TODO: Implement level literals
leftOperand := node.Args[pos+1].String() leftOp := node.Args[pos+1].String()
if len(leftOperand) == 0 { if len(leftOp) == 0 {
panic("The leftoperand for function level cannot be left blank") panic("The leftoperand for function level cannot be left blank")
} }
leftParam, _ := c.compileIfVarSub(con, leftOperand) leftParam, _ := c.compileIfVarSub(con, leftOp)
// TODO: Refactor this // TODO: Refactor this
litString("phrases.GetLevelPhrase("+leftParam+")", false) litString("phrases.GetLevelPhrase("+leftParam+")", false)
c.importMap[langPkg] = langPkg c.importMap[langPkg] = langPkg
break ArgLoop break ArgLoop
case "bunit": case "bunit":
// TODO: Implement bunit literals // TODO: Implement bunit literals
leftOperand := node.Args[pos+1].String() leftOp := node.Args[pos+1].String()
if len(leftOperand) == 0 { if len(leftOp) == 0 {
panic("The leftoperand for function buint cannot be left blank") panic("The leftoperand for function buint cannot be left blank")
} }
leftParam, _ := c.compileIfVarSub(con, leftOperand) leftParam, _ := c.compileIfVarSub(con, leftOp)
out = "{\nbyteFloat, unit := c.ConvertByteUnit(float64(" + leftParam + "))\n" out = "{\nbyteFloat, unit := c.ConvertByteUnit(float64(" + leftParam + "))\n"
out += "w.Write(StringToBytes(fmt.Sprintf(\"%.1f\", byteFloat) + unit))\n}\n" out += "w.Write(StringToBytes(fmt.Sprintf(\"%.1f\", byteFloat) + unit))\n}\n"
literal = true literal = true
@ -1221,21 +1233,21 @@ ArgLoop:
break ArgLoop break ArgLoop
case "abstime": case "abstime":
// TODO: Implement level literals // TODO: Implement level literals
leftOperand := node.Args[pos+1].String() leftOp := node.Args[pos+1].String()
if len(leftOperand) == 0 { if len(leftOp) == 0 {
panic("The leftoperand for function abstime cannot be left blank") panic("The leftoperand for function abstime cannot be left blank")
} }
leftParam, _ := c.compileIfVarSub(con, leftOperand) leftParam, _ := c.compileIfVarSub(con, leftOp)
// TODO: Refactor this // TODO: Refactor this
litString(leftParam+".Format(\"2006-01-02 15:04:05\")", false) litString(leftParam+".Format(\"2006-01-02 15:04:05\")", false)
break ArgLoop break ArgLoop
case "reltime": case "reltime":
// TODO: Implement level literals // TODO: Implement level literals
leftOperand := node.Args[pos+1].String() leftOp := node.Args[pos+1].String()
if len(leftOperand) == 0 { if len(leftOp) == 0 {
panic("The leftoperand for function reltime cannot be left blank") panic("The leftoperand for function reltime cannot be left blank")
} }
leftParam, _ := c.compileIfVarSub(con, leftOperand) leftParam, _ := c.compileIfVarSub(con, leftOp)
// TODO: Refactor this // TODO: Refactor this
litString("c.RelativeTime("+leftParam+")", false) litString("c.RelativeTime("+leftParam+")", false)
break ArgLoop break ArgLoop
@ -1246,28 +1258,28 @@ ArgLoop:
var pageParam, headParam string var pageParam, headParam string
// TODO: Implement string literals properly // TODO: Implement string literals properly
// TODO: Should we check to see if pos+3 is within the bounds of the slice? // TODO: Should we check to see if pos+3 is within the bounds of the slice?
nameOperand := node.Args[pos+1].String() nameOp := node.Args[pos+1].String()
pageOperand := node.Args[pos+2].String() pageOp := node.Args[pos+2].String()
headOperand := node.Args[pos+3].String() headOp := node.Args[pos+3].String()
if len(nameOperand) == 0 || len(pageOperand) == 0 || len(headOperand) == 0 { if len(nameOp) == 0 || len(pageOp) == 0 || len(headOp) == 0 {
panic("None of the three operands for function dyntmpl can be left blank") panic("None of the three operands for function dyntmpl can be left blank")
} }
nameParam := nameOperand nameParam := nameOp
if nameOperand[0] != '"' { if nameOp[0] != '"' {
nameParam, _ = c.compileIfVarSub(con, nameParam) nameParam, _ = c.compileIfVarSub(con, nameParam)
} }
if pageOperand[0] == '"' { if pageOp[0] == '"' {
panic("The page operand for function dyntmpl cannot be a string") panic("The page operand for function dyntmpl cannot be a string")
} }
if headOperand[0] == '"' { if headOp[0] == '"' {
panic("The head operand for function dyntmpl cannot be a string") panic("The head operand for function dyntmpl cannot be a string")
} }
pageParam, val3 := c.compileIfVarSub(con, pageOperand) pageParam, val3 := c.compileIfVarSub(con, pageOp)
if !val3.IsValid() { if !val3.IsValid() {
panic("val3 is invalid") panic("val3 is invalid")
} }
headParam, val4 := c.compileIfVarSub(con, headOperand) headParam, val4 := c.compileIfVarSub(con, headOp)
if !val4.IsValid() { if !val4.IsValid() {
panic("val4 is invalid") panic("val4 is invalid")
} }
@ -1283,9 +1295,7 @@ ArgLoop:
if c.lang == "js" { if c.lang == "js" {
continue continue
} }
out = "if fl, ok := iw.(http.Flusher); ok {\n" out = "if fl, ok := iw.(http.Flusher); ok {\nfl.Flush()\n}\n"
out += "fl.Flush()\n"
out += "}\n"
literal = true literal = true
c.importMap["net/http"] = "net/http" c.importMap["net/http"] = "net/http"
break ArgLoop break ArgLoop