diff --git a/common/files.go b/common/files.go index 4e1e87e1..9c2d110c 100644 --- a/common/files.go +++ b/common/files.go @@ -63,13 +63,13 @@ func (list SFileList) JSTmplInit() error { 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 { - 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, "function init() {", "if(tmplInits===undefined) var tmplInits = {};\ntmplInits[\""+tmplName+"\"] = ") data = replace(data, " error {\n", " {\nlet out = \"\"\n") funcIndex, hasFunc := skipAllUntilCharsExist(data, 0, []byte("function Template_")) if !hasFunc { @@ -180,11 +180,10 @@ func (list SFileList) JSTmplInit() error { data = replace(data, "w.Write(", "out += ") data = replace(data, "strconv.Itoa(", "") data = replace(data, "strconv.FormatInt(", "") - data = replace(data, "c.", "") + data = replace(data, " c.", "") data = replace(data, "phrases.", "") data = replace(data, ", 10;", "") - data = replace(data, shortName+"_tmpl_phrase_id = RegisterTmplPhraseNames([]string{", "[") - data = replace(data, "var plist = GetTmplPhrasesBytes("+shortName+"_tmpl_phrase_id)", "let plist = tmplPhrases[\""+tmplName+"\"];") + data = replace(data, "var plist = GetTmplPhrasesBytes("+shortName+"_tmpl_phrase_id)", "const plist = tmplPhrases[\""+tmplName+"\"];") data = replace(data, "var cached_var_", "let cached_var_") data = replace(data, `tmpl_`+shortName+`_vars, ok := tmpl_`+shortName+`_i.`, `/*`) data = replace(data, "[]byte(", "") diff --git a/common/templates/templates.go b/common/templates/templates.go index 47ed277d..9a58ca01 100644 --- a/common/templates/templates.go +++ b/common/templates/templates.go @@ -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 += "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 { 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) { - c.logger.Println("Unknown Kind:", reflect.ValueOf(node).Elem().Kind()) - c.logger.Println("Unknown Type:", reflect.ValueOf(node).Elem().Type().Name()) + elem := reflect.ValueOf(node).Elem() + 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...") } @@ -982,8 +995,8 @@ func (c *CTemplateSet) compileIdentSwitchN(con CContext, node *parse.CommandNode func (c *CTemplateSet) dumpSymbol(pos int, node *parse.CommandNode, symbol string) { c.detail("symbol: ", symbol) - c.detail("node.Args[pos + 1]", node.Args[pos+1]) - c.detail("node.Args[pos + 2]", node.Args[pos+2]) + c.detail("node.Args[pos+1]", node.Args[pos+1]) + c.detail("node.Args[pos+2]", node.Args[pos+2]) } func (c *CTemplateSet) compareFunc(con CContext, pos int, node *parse.CommandNode, compare string) (out string) { @@ -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) { leftParam, val2 := c.compileIfVarSub(con, node.Args[pos+1].String()) rightParam, val3 := c.compileIfVarSub(con, node.Args[pos+2].String()) - if val2.IsValid() { val = val2 } else if val3.IsValid() { @@ -1070,8 +1082,8 @@ ArgLoop: val = rval break ArgLoop case "elapsed": - leftOperand := node.Args[pos+1].String() - leftParam, _ := c.compileIfVarSub(con, leftOperand) + leftOp := node.Args[pos+1].String() + leftParam, _ := c.compileIfVarSub(con, leftOp) // TODO: Refactor this // TODO: Validate that this is actually a time.Time litString("time.Since("+leftParam+").String()", false) @@ -1079,19 +1091,19 @@ ArgLoop: break ArgLoop case "dock": // TODO: Implement string literals properly - leftOperand := node.Args[pos+1].String() - rightOperand := node.Args[pos+2].String() - if len(leftOperand) == 0 || len(rightOperand) == 0 { + leftOp := node.Args[pos+1].String() + rightOp := node.Args[pos+2].String() + if len(leftOp) == 0 || len(rightOp) == 0 { panic("The left or right operand for function dock cannot be left blank") } - leftParam := leftOperand - if leftOperand[0] != '"' { + leftParam := leftOp + if leftOp[0] != '"' { leftParam, _ = c.compileIfVarSub(con, leftParam) } - if rightOperand[0] == '"' { + if rightOp[0] == '"' { 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() { panic("val3 is invalid") } @@ -1102,19 +1114,19 @@ ArgLoop: break ArgLoop case "hasWidgets": // TODO: Implement string literals properly - leftOperand := node.Args[pos+1].String() - rightOperand := node.Args[pos+2].String() - if len(leftOperand) == 0 || len(rightOperand) == 0 { + leftOp := node.Args[pos+1].String() + rightOp := node.Args[pos+2].String() + if len(leftOp) == 0 || len(rightOp) == 0 { panic("The left or right operand for function dock cannot be left blank") } - leftParam := leftOperand - if leftOperand[0] != '"' { + leftParam := leftOp + if leftOp[0] != '"' { leftParam, _ = c.compileIfVarSub(con, leftParam) } - if rightOperand[0] == '"' { + if rightOp[0] == '"' { 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() { panic("val3 is invalid") } @@ -1126,19 +1138,19 @@ ArgLoop: break ArgLoop case "lang": // TODO: Implement string literals properly - leftOperand := node.Args[pos+1].String() - if len(leftOperand) == 0 { + leftOp := node.Args[pos+1].String() + if len(leftOp) == 0 { 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 - leftParam := strings.Replace(leftOperand, "\"", "", -1) + leftParam := strings.Replace(leftOp, "\"", "", -1) c.langIndexToName = append(c.langIndexToName, leftParam) notident = true con.PushPhrase(len(c.langIndexToName) - 1) } else { - leftParam := leftOperand - if leftOperand[0] != '"' { + leftParam := leftOp + if leftOp[0] != '"' { 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 @@ -1148,11 +1160,11 @@ ArgLoop: break ArgLoop case "langf": // TODO: Implement string literals properly - leftOperand := node.Args[pos+1].String() - if len(leftOperand) == 0 { + leftOp := node.Args[pos+1].String() + if len(leftOp) == 0 { panic("The left operand for the language string cannot be left blank") } - if leftOperand[0] != '"' { + if leftOp[0] != '"' { panic("Phrase names cannot be dynamic") } @@ -1193,27 +1205,27 @@ ArgLoop: // TODO: Implement string literals properly // ! Slightly crude but it does the job - litString("phrases.GetTmplPhrasef("+leftOperand+ob+")", false) + litString("phrases.GetTmplPhrasef("+leftOp+ob+")", false) c.importMap[langPkg] = langPkg break ArgLoop case "level": // TODO: Implement level literals - leftOperand := node.Args[pos+1].String() - if len(leftOperand) == 0 { + leftOp := node.Args[pos+1].String() + if len(leftOp) == 0 { panic("The leftoperand for function level cannot be left blank") } - leftParam, _ := c.compileIfVarSub(con, leftOperand) + leftParam, _ := c.compileIfVarSub(con, leftOp) // TODO: Refactor this litString("phrases.GetLevelPhrase("+leftParam+")", false) c.importMap[langPkg] = langPkg break ArgLoop case "bunit": // TODO: Implement bunit literals - leftOperand := node.Args[pos+1].String() - if len(leftOperand) == 0 { + leftOp := node.Args[pos+1].String() + if len(leftOp) == 0 { 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 += "w.Write(StringToBytes(fmt.Sprintf(\"%.1f\", byteFloat) + unit))\n}\n" literal = true @@ -1221,21 +1233,21 @@ ArgLoop: break ArgLoop case "abstime": // TODO: Implement level literals - leftOperand := node.Args[pos+1].String() - if len(leftOperand) == 0 { + leftOp := node.Args[pos+1].String() + if len(leftOp) == 0 { panic("The leftoperand for function abstime cannot be left blank") } - leftParam, _ := c.compileIfVarSub(con, leftOperand) + leftParam, _ := c.compileIfVarSub(con, leftOp) // TODO: Refactor this litString(leftParam+".Format(\"2006-01-02 15:04:05\")", false) break ArgLoop case "reltime": // TODO: Implement level literals - leftOperand := node.Args[pos+1].String() - if len(leftOperand) == 0 { + leftOp := node.Args[pos+1].String() + if len(leftOp) == 0 { panic("The leftoperand for function reltime cannot be left blank") } - leftParam, _ := c.compileIfVarSub(con, leftOperand) + leftParam, _ := c.compileIfVarSub(con, leftOp) // TODO: Refactor this litString("c.RelativeTime("+leftParam+")", false) break ArgLoop @@ -1246,28 +1258,28 @@ ArgLoop: var pageParam, headParam string // TODO: Implement string literals properly // TODO: Should we check to see if pos+3 is within the bounds of the slice? - nameOperand := node.Args[pos+1].String() - pageOperand := node.Args[pos+2].String() - headOperand := node.Args[pos+3].String() - if len(nameOperand) == 0 || len(pageOperand) == 0 || len(headOperand) == 0 { + nameOp := node.Args[pos+1].String() + pageOp := node.Args[pos+2].String() + headOp := node.Args[pos+3].String() + if len(nameOp) == 0 || len(pageOp) == 0 || len(headOp) == 0 { panic("None of the three operands for function dyntmpl can be left blank") } - nameParam := nameOperand - if nameOperand[0] != '"' { + nameParam := nameOp + if nameOp[0] != '"' { nameParam, _ = c.compileIfVarSub(con, nameParam) } - if pageOperand[0] == '"' { + if pageOp[0] == '"' { 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") } - pageParam, val3 := c.compileIfVarSub(con, pageOperand) + pageParam, val3 := c.compileIfVarSub(con, pageOp) if !val3.IsValid() { panic("val3 is invalid") } - headParam, val4 := c.compileIfVarSub(con, headOperand) + headParam, val4 := c.compileIfVarSub(con, headOp) if !val4.IsValid() { panic("val4 is invalid") } @@ -1283,9 +1295,7 @@ ArgLoop: if c.lang == "js" { continue } - out = "if fl, ok := iw.(http.Flusher); ok {\n" - out += "fl.Flush()\n" - out += "}\n" + out = "if fl, ok := iw.(http.Flusher); ok {\nfl.Flush()\n}\n" literal = true c.importMap["net/http"] = "net/http" break ArgLoop