2017-11-11 04:06:16 +00:00
package tmpl
2017-05-07 08:31:41 +00:00
2017-06-05 11:57:27 +00:00
import (
"bytes"
2017-09-03 04:50:31 +00:00
"fmt"
2017-06-05 11:57:27 +00:00
"io/ioutil"
2018-03-08 03:59:47 +00:00
"log"
2017-09-03 04:50:31 +00:00
"path/filepath"
"reflect"
2018-03-08 03:59:47 +00:00
"strconv"
"strings"
2017-06-05 11:57:27 +00:00
"text/template/parse"
)
2016-12-16 10:37:42 +00:00
2017-09-10 16:57:22 +00:00
// TODO: Turn this file into a library
2017-09-03 04:50:31 +00:00
var textOverlapList = make ( map [ string ] int )
2018-11-17 02:36:02 +00:00
2018-11-01 06:43:56 +00:00
// TODO: Stop hard-coding this here
var langPkg = "github.com/Azareal/Gosora/common/phrases"
2017-01-01 15:45:43 +00:00
2017-09-03 04:50:31 +00:00
type VarItem struct {
Name string
2016-12-16 10:37:42 +00:00
Destination string
2017-09-03 04:50:31 +00:00
Type string
2016-12-16 10:37:42 +00:00
}
2018-03-08 03:59:47 +00:00
2017-09-03 04:50:31 +00:00
type VarItemReflect struct {
Name string
2016-12-16 10:37:42 +00:00
Destination string
2017-09-03 04:50:31 +00:00
Value reflect . Value
2016-12-16 10:37:42 +00:00
}
2018-03-08 03:59:47 +00:00
type CTemplateConfig struct {
2018-06-24 13:49:29 +00:00
Minify bool
Debug bool
SuperDebug bool
SkipHandles bool
SkipTmplPtrMap bool
SkipInitBlock bool
PackageName string
2018-03-08 03:59:47 +00:00
}
2018-11-18 05:28:27 +00:00
type OutBufferFrame struct {
Body string
Type string
}
2018-03-08 03:59:47 +00:00
// nolint
2017-09-03 04:50:31 +00:00
type CTemplateSet struct {
2018-03-12 04:52:47 +00:00
templateList map [ string ] * parse . Tree
fileDir string
funcMap map [ string ] interface { }
importMap map [ string ] string
TemplateFragmentCount map [ string ] int
Fragments map [ string ] int
fragmentCursor map [ string ] int
FragOut string
varList map [ string ] VarItem
localVars map [ string ] map [ string ] VarItemReflect
hasDispInt bool
localDispStructIndex int
langIndexToName [ ] string
stats map [ string ] int
previousNode parse . NodeType
currentNode parse . NodeType
nextNode parse . NodeType
2016-12-16 10:37:42 +00:00
//tempVars map[string]string
2018-04-22 12:33:56 +00:00
config CTemplateConfig
baseImportMap map [ string ] string
2018-06-24 13:49:29 +00:00
buildTags string
2018-04-22 12:33:56 +00:00
expectsInt interface { }
}
func NewCTemplateSet ( ) * CTemplateSet {
return & CTemplateSet {
config : CTemplateConfig {
PackageName : "main" ,
} ,
baseImportMap : map [ string ] string { } ,
funcMap : map [ string ] interface { } {
"and" : "&&" ,
"not" : "!" ,
"or" : "||" ,
2018-11-18 05:28:27 +00:00
"eq" : "==" ,
"ge" : ">=" ,
"gt" : ">" ,
"le" : "<=" ,
"lt" : "<" ,
"ne" : "!=" ,
"add" : "+" ,
"subtract" : "-" ,
"multiply" : "*" ,
"divide" : "/" ,
2018-04-22 12:33:56 +00:00
"dock" : true ,
2018-11-17 02:36:02 +00:00
"elapsed" : true ,
2018-04-22 12:33:56 +00:00
"lang" : true ,
2018-10-10 07:33:51 +00:00
"level" : true ,
2018-04-22 12:33:56 +00:00
"scope" : true ,
2018-10-27 03:21:02 +00:00
"dyntmpl" : true ,
2018-04-22 12:33:56 +00:00
} ,
}
2017-11-11 04:06:16 +00:00
}
2018-03-08 03:59:47 +00:00
func ( c * CTemplateSet ) SetConfig ( config CTemplateConfig ) {
2018-04-22 12:33:56 +00:00
if config . PackageName == "" {
config . PackageName = "main"
}
2018-03-08 03:59:47 +00:00
c . config = config
2017-11-11 04:06:16 +00:00
}
2018-03-08 03:59:47 +00:00
func ( c * CTemplateSet ) GetConfig ( ) CTemplateConfig {
return c . config
2017-11-11 06:33:08 +00:00
}
2018-04-22 12:33:56 +00:00
func ( c * CTemplateSet ) SetBaseImportMap ( importMap map [ string ] string ) {
c . baseImportMap = importMap
}
2018-06-24 13:49:29 +00:00
func ( c * CTemplateSet ) SetBuildTags ( tags string ) {
c . buildTags = tags
}
2018-03-08 03:59:47 +00:00
func ( c * CTemplateSet ) Compile ( name string , fileDir string , expects string , expectsInt interface { } , varList map [ string ] VarItem , imports ... string ) ( out string , err error ) {
if c . config . Debug {
2017-08-06 15:22:18 +00:00
fmt . Println ( "Compiling template '" + name + "'" )
}
2018-04-22 12:33:56 +00:00
c . importMap = map [ string ] string { }
for index , item := range c . baseImportMap {
c . importMap [ index ] = item
2017-04-12 10:10:36 +00:00
}
2017-11-11 06:33:08 +00:00
if len ( imports ) > 0 {
for _ , importItem := range imports {
c . importMap [ importItem ] = importItem
}
}
2018-04-22 12:33:56 +00:00
c . fileDir = fileDir
2016-12-16 10:37:42 +00:00
c . varList = varList
2017-11-23 05:37:08 +00:00
c . hasDispInt = false
c . localDispStructIndex = 0
2016-12-17 03:39:53 +00:00
c . stats = make ( map [ string ] int )
2016-12-16 10:37:42 +00:00
c . expectsInt = expectsInt
2017-06-16 10:41:30 +00:00
2018-03-21 05:56:33 +00:00
res , err := ioutil . ReadFile ( fileDir + "overrides/" + name )
2016-12-16 10:37:42 +00:00
if err != nil {
2018-03-21 05:56:33 +00:00
c . detail ( "override path: " , fileDir + "overrides/" + name )
c . detail ( "override err: " , err )
res , err = ioutil . ReadFile ( fileDir + name )
if err != nil {
return "" , err
}
2016-12-16 10:37:42 +00:00
}
content := string ( res )
2018-03-08 03:59:47 +00:00
if c . config . Minify {
2017-02-11 14:51:16 +00:00
content = minify ( content )
}
2017-06-16 10:41:30 +00:00
2016-12-16 10:37:42 +00:00
tree := parse . New ( name , c . funcMap )
2017-09-03 04:50:31 +00:00
var treeSet = make ( map [ string ] * parse . Tree )
tree , err = tree . Parse ( content , "{{" , "}}" , treeSet , c . funcMap )
2016-12-16 10:37:42 +00:00
if err != nil {
2017-08-13 11:22:34 +00:00
return "" , err
2016-12-16 10:37:42 +00:00
}
2018-03-21 05:56:33 +00:00
c . detail ( name )
2017-06-16 10:41:30 +00:00
2016-12-16 10:37:42 +00:00
fname := strings . TrimSuffix ( name , filepath . Ext ( name ) )
2018-11-18 05:28:27 +00:00
var outBuf [ ] OutBufferFrame
con := CContext { VarHolder : "tmpl_" + fname + "_vars" , HoldReflect : reflect . ValueOf ( expectsInt ) , TemplateName : fname , OutBuf : & outBuf }
2018-03-08 03:59:47 +00:00
c . templateList = map [ string ] * parse . Tree { fname : tree }
2018-03-21 05:56:33 +00:00
c . detail ( c . templateList )
2016-12-16 10:37:42 +00:00
c . localVars = make ( map [ string ] map [ string ] VarItemReflect )
c . localVars [ fname ] = make ( map [ string ] VarItemReflect )
2018-11-18 05:28:27 +00:00
c . localVars [ fname ] [ "." ] = VarItemReflect { "." , con . VarHolder , con . HoldReflect }
2017-01-17 07:55:46 +00:00
if c . Fragments == nil {
c . Fragments = make ( map [ string ] int )
}
2018-03-12 04:52:47 +00:00
c . fragmentCursor = map [ string ] int { fname : 0 }
2018-03-08 03:59:47 +00:00
c . langIndexToName = nil
2017-06-16 10:41:30 +00:00
2018-03-12 04:52:47 +00:00
// TODO: Is this the first template loaded in? We really should have some sort of constructor for CTemplateSet
if c . TemplateFragmentCount == nil {
c . TemplateFragmentCount = make ( map [ string ] int )
}
2018-11-18 05:28:27 +00:00
c . rootIterate ( c . templateList [ fname ] , con )
2018-03-12 04:52:47 +00:00
c . TemplateFragmentCount [ fname ] = c . fragmentCursor [ fname ] + 1
2017-06-16 10:41:30 +00:00
2018-11-01 06:43:56 +00:00
if len ( c . langIndexToName ) > 0 {
c . importMap [ langPkg ] = langPkg
}
2016-12-16 10:37:42 +00:00
var importList string
2018-04-22 12:33:56 +00:00
for _ , item := range c . importMap {
importList += "import \"" + item + "\"\n"
2016-12-16 10:37:42 +00:00
}
2017-06-16 10:41:30 +00:00
2016-12-16 10:37:42 +00:00
var varString string
for _ , varItem := range c . varList {
varString += "var " + varItem . Name + " " + varItem . Type + " = " + varItem . Destination + "\n"
}
2017-06-16 10:41:30 +00:00
2018-06-24 13:49:29 +00:00
var fout string
if c . buildTags != "" {
fout += "// +build " + c . buildTags + "\n\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"
2018-04-22 12:33:56 +00:00
fout += "package " + c . config . PackageName + "\n" + importList + "\n"
2017-11-11 06:33:08 +00:00
2018-04-22 12:33:56 +00:00
if ! c . config . SkipInitBlock {
if len ( c . langIndexToName ) > 0 {
fout += "var " + fname + "_tmpl_phrase_id int\n\n"
}
fout += "// nolint\nfunc init() {\n"
2017-11-11 06:33:08 +00:00
2018-04-22 12:33:56 +00:00
if ! c . config . SkipHandles {
fout += "\tcommon.Template_" + fname + "_handle = Template_" + fname + "\n"
fout += "\tcommon.Ctemplates = append(common.Ctemplates,\"" + fname + "\")\n\tcommon.TmplPtrMap[\"" + fname + "\"] = &common.Template_" + fname + "_handle\n"
}
2017-11-11 06:33:08 +00:00
2018-06-24 13:49:29 +00:00
if ! c . config . SkipTmplPtrMap {
fout += "\tcommon.TmplPtrMap[\"o_" + fname + "\"] = Template_" + fname + "\n"
}
2018-04-22 12:33:56 +00:00
if len ( c . langIndexToName ) > 0 {
2018-11-01 06:43:56 +00:00
fout += "\t" + fname + "_tmpl_phrase_id = phrases.RegisterTmplPhraseNames([]string{\n"
2018-04-22 12:33:56 +00:00
for _ , name := range c . langIndexToName {
fout += "\t\t" + ` " ` + name + ` " ` + ",\n"
}
fout += "\t})\n"
2018-03-08 03:59:47 +00:00
}
2018-04-22 12:33:56 +00:00
fout += "}\n\n"
2018-03-08 03:59:47 +00:00
}
2017-11-11 06:33:08 +00:00
2018-04-22 12:33:56 +00:00
fout += "// nolint\nfunc Template_" + fname + "(tmpl_" + fname + "_vars " + expects + ", w io.Writer) error {\n"
2018-03-08 03:59:47 +00:00
if len ( c . langIndexToName ) > 0 {
2018-11-01 06:43:56 +00:00
fout += "var plist = phrases.GetTmplPhrasesBytes(" + fname + "_tmpl_phrase_id)\n"
2018-03-08 03:59:47 +00:00
}
2018-11-18 05:28:27 +00:00
fout += varString
for _ , frame := range outBuf {
fout += frame . Body
}
fout += "return nil\n}\n"
2017-06-16 10:41:30 +00:00
2017-09-03 04:50:31 +00:00
fout = strings . Replace ( fout , ` ) )
w . Write ( [ ] byte ( ` , " + " , - 1 )
fout = strings . Replace ( fout , "` + `" , "" , - 1 )
2017-01-17 07:55:46 +00:00
//spstr := "`([:space:]*)`"
2017-11-07 22:38:15 +00:00
//whitespaceWrites := regexp.MustCompile(`(?s)w.Write\(\[\]byte\(`+spstr+`\)\)`)
//fout = whitespaceWrites.ReplaceAllString(fout,"")
2017-06-16 10:41:30 +00:00
2018-03-08 03:59:47 +00:00
if c . config . Debug {
2017-05-07 08:31:41 +00:00
for index , count := range c . stats {
2017-11-07 22:38:15 +00:00
fmt . Println ( index + ": " , strconv . Itoa ( count ) )
2017-05-07 08:31:41 +00:00
}
fmt . Println ( " " )
2016-12-17 03:39:53 +00:00
}
2018-03-21 05:56:33 +00:00
c . detail ( "Output!" )
c . detail ( fout )
2017-08-13 11:22:34 +00:00
return fout , nil
2016-12-16 10:37:42 +00:00
}
2018-11-18 05:28:27 +00:00
type CContext struct {
VarHolder string
HoldReflect reflect . Value
TemplateName string
OutBuf * [ ] OutBufferFrame
}
func ( con * CContext ) Push ( nType string , body string ) {
* con . OutBuf = append ( * con . OutBuf , OutBufferFrame { body , nType } )
}
func ( c * CTemplateSet ) rootIterate ( tree * parse . Tree , con CContext ) {
2018-03-21 05:56:33 +00:00
c . detail ( tree . Root )
2017-11-06 16:24:45 +00:00
treeLength := len ( tree . Root . Nodes )
for index , node := range tree . Root . Nodes {
2018-03-21 05:56:33 +00:00
c . detail ( "Node:" , node . String ( ) )
2017-11-06 16:24:45 +00:00
c . previousNode = c . currentNode
c . currentNode = node . Type ( )
if treeLength != ( index + 1 ) {
c . nextNode = tree . Root . Nodes [ index + 1 ] . Type ( )
}
2018-11-18 05:28:27 +00:00
c . compileSwitch ( con , node )
2017-11-06 16:24:45 +00:00
}
}
2018-11-18 05:28:27 +00:00
func ( c * CTemplateSet ) compileSwitch ( con CContext , node parse . Node ) {
c . dumpCall ( "compileSwitch" , con , node )
2016-12-16 10:37:42 +00:00
switch node := node . ( type ) {
2017-09-03 04:50:31 +00:00
case * parse . ActionNode :
2018-03-21 05:56:33 +00:00
c . detail ( "Action Node" )
2017-09-03 04:50:31 +00:00
if node . Pipe == nil {
break
}
for _ , cmd := range node . Pipe . Cmds {
2018-11-18 05:28:27 +00:00
c . compileSubSwitch ( con , cmd )
2017-09-03 04:50:31 +00:00
}
case * parse . IfNode :
2018-03-21 05:56:33 +00:00
c . detail ( "If Node:" )
c . detail ( "node.Pipe" , node . Pipe )
2017-09-03 04:50:31 +00:00
var expr string
for _ , cmd := range node . Pipe . Cmds {
2018-03-21 05:56:33 +00:00
c . detail ( "If Node Bit:" , cmd )
c . detail ( "Bit Type:" , reflect . ValueOf ( cmd ) . Type ( ) . Name ( ) )
2018-11-18 05:28:27 +00:00
expr += c . compileExprSwitch ( con , cmd )
c . detail ( "Expression Step:" , c . compileExprSwitch ( con , cmd ) )
2017-09-03 04:50:31 +00:00
}
2017-06-16 10:41:30 +00:00
2018-03-21 05:56:33 +00:00
c . detail ( "Expression:" , expr )
2017-09-03 04:50:31 +00:00
c . previousNode = c . currentNode
c . currentNode = parse . NodeList
c . nextNode = - 1
2018-11-18 05:28:27 +00:00
con . Push ( "startif" , "if " + expr + " {\n" )
c . compileSwitch ( con , node . List )
2017-09-03 04:50:31 +00:00
if node . ElseList == nil {
2018-03-21 05:56:33 +00:00
c . detail ( "Selected Branch 1" )
2018-11-18 05:28:27 +00:00
con . Push ( "endif" , "}\n" )
} else {
c . detail ( "Selected Branch 2" )
con . Push ( "endif" , "}" )
con . Push ( "startelse" , " else {\n" )
c . compileSwitch ( con , node . ElseList )
con . Push ( "endelse" , "}\n" )
2017-09-03 04:50:31 +00:00
}
case * parse . ListNode :
2018-03-21 05:56:33 +00:00
c . detail ( "List Node" )
2017-09-03 04:50:31 +00:00
for _ , subnode := range node . Nodes {
2018-11-18 05:28:27 +00:00
c . compileSwitch ( con , subnode )
2017-09-03 04:50:31 +00:00
}
case * parse . RangeNode :
2018-11-18 05:28:27 +00:00
c . compileRangeNode ( con , node )
2017-09-03 04:50:31 +00:00
case * parse . TemplateNode :
2018-11-18 05:28:27 +00:00
c . compileSubTemplate ( con , node )
2017-09-03 04:50:31 +00:00
case * parse . TextNode :
c . previousNode = c . currentNode
c . currentNode = node . Type ( )
c . nextNode = 0
tmpText := bytes . TrimSpace ( node . Text )
if len ( tmpText ) == 0 {
2018-11-18 05:28:27 +00:00
return
2017-09-03 04:50:31 +00:00
}
2018-11-18 05:28:27 +00:00
fragmentName := con . TemplateName + "_" + strconv . Itoa ( c . fragmentCursor [ con . TemplateName ] )
fragmentPrefix := con . TemplateName + "_frags[" + strconv . Itoa ( c . fragmentCursor [ con . TemplateName ] ) + "]"
2017-09-03 04:50:31 +00:00
_ , ok := c . Fragments [ fragmentName ]
if ! ok {
c . Fragments [ fragmentName ] = len ( node . Text )
2018-03-12 04:52:47 +00:00
c . FragOut += fragmentPrefix + " = []byte(`" + string ( node . Text ) + "`)\n"
2017-09-03 04:50:31 +00:00
}
2018-11-18 05:28:27 +00:00
c . fragmentCursor [ con . TemplateName ] = c . fragmentCursor [ con . TemplateName ] + 1
con . Push ( "text" , "w.Write(" + fragmentPrefix + ")\n" )
2017-09-03 04:50:31 +00:00
default :
2018-11-18 05:28:27 +00:00
c . unknownNode ( node )
2016-12-16 10:37:42 +00:00
}
2017-11-07 22:38:15 +00:00
}
2018-11-18 05:28:27 +00:00
func ( c * CTemplateSet ) compileRangeNode ( con CContext , node * parse . RangeNode ) {
c . dumpCall ( "compileRangeNode" , con , node )
c . detail ( "node.Pipe: " , node . Pipe )
var expr string
2017-11-07 22:38:15 +00:00
var outVal reflect . Value
for _ , cmd := range node . Pipe . Cmds {
2018-03-21 05:56:33 +00:00
c . detail ( "Range Bit:" , cmd )
2018-11-18 05:28:27 +00:00
// ! This bit is slightly suspect, hm.
expr , outVal = c . compileReflectSwitch ( con , cmd )
2017-11-07 22:38:15 +00:00
}
2018-11-18 05:28:27 +00:00
c . detail ( "Expr:" , expr )
2018-03-21 05:56:33 +00:00
c . detail ( "Range Kind Switch!" )
2017-11-07 22:38:15 +00:00
2018-11-18 05:28:27 +00:00
var startIf = func ( item reflect . Value , useCopy bool ) {
con . Push ( "startif" , "if len(" + expr + ") != 0 {\n" )
con . Push ( "startloop" , "for _, item := range " + expr + " {\n" )
ccon := con
ccon . VarHolder = "item"
ccon . HoldReflect = item
c . compileSwitch ( ccon , node . List )
con . Push ( "endloop" , "}\n" )
if node . ElseList != nil {
con . Push ( "endif" , "}" )
con . Push ( "startelse" , " else {\n" )
if ! useCopy {
ccon = con
}
c . compileSwitch ( ccon , node . ElseList )
con . Push ( "endelse" , "}\n" )
} else {
con . Push ( "endloop" , "}\n" )
}
}
2017-11-07 22:38:15 +00:00
switch outVal . Kind ( ) {
case reflect . Map :
var item reflect . Value
for _ , key := range outVal . MapKeys ( ) {
item = outVal . MapIndex ( key )
}
2018-03-21 05:56:33 +00:00
c . detail ( "Range item:" , item )
2017-11-07 22:38:15 +00:00
if ! item . IsValid ( ) {
panic ( "item" + "^\n" + "Invalid map. Maybe, it doesn't have any entries for the template engine to analyse?" )
}
2018-11-18 05:28:27 +00:00
startIf ( item , true )
2017-11-07 22:38:15 +00:00
case reflect . Slice :
if outVal . Len ( ) == 0 {
panic ( "The sample data needs at-least one or more elements for the slices. We're looking into removing this requirement at some point!" )
}
2018-11-18 05:28:27 +00:00
startIf ( outVal . Index ( 0 ) , false )
2017-11-07 22:38:15 +00:00
case reflect . Invalid :
2018-11-18 05:28:27 +00:00
return
2017-11-07 22:38:15 +00:00
}
2016-12-16 10:37:42 +00:00
}
2018-11-18 05:28:27 +00:00
func ( c * CTemplateSet ) compileSubSwitch ( con CContext , node * parse . CommandNode ) {
c . dumpCall ( "compileSubSwitch" , con , node )
2016-12-16 10:37:42 +00:00
firstWord := node . Args [ 0 ]
switch n := firstWord . ( type ) {
2017-09-03 04:50:31 +00:00
case * parse . FieldNode :
2018-03-21 05:56:33 +00:00
c . detail ( "Field Node:" , n . Ident )
2017-09-03 04:50:31 +00:00
/* Use reflect to determine if the field is for a method, otherwise assume it's a variable. Variable declarations are coming soon! */
2018-11-18 05:28:27 +00:00
cur := con . HoldReflect
2017-06-16 10:41:30 +00:00
2018-11-18 05:28:27 +00:00
var varBit string
2017-09-03 04:50:31 +00:00
if cur . Kind ( ) == reflect . Interface {
cur = cur . Elem ( )
2018-11-18 05:28:27 +00:00
varBit += ".(" + cur . Type ( ) . Name ( ) + ")"
2017-09-03 04:50:31 +00:00
}
2017-11-23 05:37:08 +00:00
// ! Might not work so well for non-struct pointers
skipPointers := func ( cur reflect . Value , id string ) reflect . Value {
2017-09-03 04:50:31 +00:00
if cur . Kind ( ) == reflect . Ptr {
2018-03-21 05:56:33 +00:00
c . detail ( "Looping over pointer" )
2017-09-03 04:50:31 +00:00
for cur . Kind ( ) == reflect . Ptr {
2016-12-16 10:37:42 +00:00
cur = cur . Elem ( )
}
2018-03-21 05:56:33 +00:00
c . detail ( "Data Kind:" , cur . Kind ( ) . String ( ) )
c . detail ( "Field Bit:" , id )
2016-12-16 10:37:42 +00:00
}
2017-11-23 05:37:08 +00:00
return cur
}
var assLines string
var multiline = false
for _ , id := range n . Ident {
2018-03-21 05:56:33 +00:00
c . detail ( "Data Kind:" , cur . Kind ( ) . String ( ) )
c . detail ( "Field Bit:" , id )
2017-11-23 05:37:08 +00:00
cur = skipPointers ( cur , id )
2017-06-16 10:41:30 +00:00
2017-09-03 04:50:31 +00:00
if ! cur . IsValid ( ) {
2017-11-23 05:37:08 +00:00
c . error ( "Debug Data:" )
2018-11-18 05:28:27 +00:00
c . error ( "Holdreflect:" , con . HoldReflect )
c . error ( "Holdreflect.Kind():" , con . HoldReflect . Kind ( ) )
2018-03-08 03:59:47 +00:00
if ! c . config . SuperDebug {
2017-11-23 05:37:08 +00:00
c . error ( "cur.Kind():" , cur . Kind ( ) . String ( ) )
}
c . error ( "" )
if ! multiline {
2018-11-18 05:28:27 +00:00
panic ( con . VarHolder + varBit + "^\n" + "Invalid value. Maybe, it doesn't exist?" )
2017-09-10 16:57:22 +00:00
}
2018-11-18 05:28:27 +00:00
panic ( varBit + "^\n" + "Invalid value. Maybe, it doesn't exist?" )
2017-11-23 05:37:08 +00:00
}
2017-09-10 16:57:22 +00:00
2018-11-18 05:28:27 +00:00
c . detail ( "in-loop varBit: " + varBit )
2017-11-23 05:37:08 +00:00
if cur . Kind ( ) == reflect . Map {
cur = cur . MapIndex ( reflect . ValueOf ( id ) )
2018-11-18 05:28:27 +00:00
varBit += "[\"" + id + "\"]"
2017-11-23 05:37:08 +00:00
cur = skipPointers ( cur , id )
if cur . Kind ( ) == reflect . Struct || cur . Kind ( ) == reflect . Interface {
// TODO: Move the newVarByte declaration to the top level or to the if level, if a dispInt is only used in a particular if statement
var dispStr , newVarByte string
if cur . Kind ( ) == reflect . Interface {
dispStr = "Int"
if ! c . hasDispInt {
newVarByte = ":"
c . hasDispInt = true
}
}
// TODO: De-dupe identical struct types rather than allocating a variable for each one
if cur . Kind ( ) == reflect . Struct {
dispStr = "Struct" + strconv . Itoa ( c . localDispStructIndex )
newVarByte = ":"
c . localDispStructIndex ++
}
2018-11-18 05:28:27 +00:00
con . VarHolder = "disp" + dispStr
varBit = con . VarHolder + " " + newVarByte + "= " + con . VarHolder + varBit + "\n"
2017-11-23 05:37:08 +00:00
multiline = true
} else {
continue
}
}
if cur . Kind ( ) != reflect . Interface {
cur = cur . FieldByName ( id )
2018-11-18 05:28:27 +00:00
varBit += "." + id
2016-12-16 10:37:42 +00:00
}
2017-09-03 04:50:31 +00:00
2017-11-23 05:37:08 +00:00
// TODO: Handle deeply nested pointers mixed with interfaces mixed with pointers better
2017-09-03 04:50:31 +00:00
if cur . Kind ( ) == reflect . Interface {
cur = cur . Elem ( )
2018-11-18 05:28:27 +00:00
varBit += ".("
2017-11-23 05:37:08 +00:00
// TODO: Surely, there's a better way of doing this?
2017-09-03 04:50:31 +00:00
if cur . Type ( ) . PkgPath ( ) != "main" && cur . Type ( ) . PkgPath ( ) != "" {
c . importMap [ "html/template" ] = "html/template"
2018-11-18 05:28:27 +00:00
varBit += strings . TrimPrefix ( cur . Type ( ) . PkgPath ( ) , "html/" ) + "."
2017-09-03 04:50:31 +00:00
}
2018-11-18 05:28:27 +00:00
varBit += cur . Type ( ) . Name ( ) + ")"
2016-12-16 10:37:42 +00:00
}
2018-11-18 05:28:27 +00:00
c . detail ( "End Cycle: " , varBit )
2017-09-03 04:50:31 +00:00
}
2017-11-23 05:37:08 +00:00
if multiline {
2018-11-18 05:28:27 +00:00
assSplit := strings . Split ( varBit , "\n" )
varBit = assSplit [ len ( assSplit ) - 1 ]
2017-11-23 05:37:08 +00:00
assSplit = assSplit [ : len ( assSplit ) - 1 ]
assLines = strings . Join ( assSplit , "\n" ) + "\n"
}
2018-11-18 05:28:27 +00:00
c . compileVarSub ( con , con . VarHolder + varBit , cur , assLines , func ( in string ) string {
for _ , varItem := range c . varList {
if strings . HasPrefix ( in , varItem . Destination ) {
in = strings . Replace ( in , varItem . Destination , varItem . Name , 1 )
}
2017-01-21 18:16:27 +00:00
}
2018-11-18 05:28:27 +00:00
return in
} )
2017-09-03 04:50:31 +00:00
case * parse . DotNode :
2018-03-21 05:56:33 +00:00
c . detail ( "Dot Node:" , node . String ( ) )
2018-11-18 05:28:27 +00:00
c . compileVarSub ( con , con . VarHolder , con . HoldReflect , "" , nil )
2017-09-03 04:50:31 +00:00
case * parse . NilNode :
panic ( "Nil is not a command x.x" )
case * parse . VariableNode :
2018-03-21 05:56:33 +00:00
c . detail ( "Variable Node:" , n . String ( ) )
c . detail ( n . Ident )
2018-11-18 05:28:27 +00:00
varname , reflectVal := c . compileIfVarSub ( con , n . String ( ) )
c . compileVarSub ( con , varname , reflectVal , "" , nil )
2017-09-03 04:50:31 +00:00
case * parse . StringNode :
2018-11-18 05:28:27 +00:00
con . Push ( "stringnode" , n . Quoted )
2017-09-03 04:50:31 +00:00
case * parse . IdentifierNode :
2018-03-21 05:56:33 +00:00
c . detail ( "Identifier Node:" , node )
c . detail ( "Identifier Node Args:" , node . Args )
2018-11-18 05:28:27 +00:00
out , outval , lit := c . compileIdentSwitch ( con , node )
2017-11-29 02:34:02 +00:00
if lit {
2018-11-18 05:28:27 +00:00
con . Push ( "identifier" , out )
return
2017-11-29 02:34:02 +00:00
}
2018-11-18 05:28:27 +00:00
c . compileVarSub ( con , out , outval , "" , nil )
2017-09-03 04:50:31 +00:00
default :
2018-11-18 05:28:27 +00:00
c . unknownNode ( node )
2016-12-16 10:37:42 +00:00
}
}
2018-11-18 05:28:27 +00:00
func ( c * CTemplateSet ) compileExprSwitch ( con CContext , node * parse . CommandNode ) ( out string ) {
2018-11-18 05:43:10 +00:00
c . dumpCall ( "compileExprSwitch" , con , node )
2016-12-16 10:37:42 +00:00
firstWord := node . Args [ 0 ]
switch n := firstWord . ( type ) {
2017-09-03 04:50:31 +00:00
case * parse . FieldNode :
2018-03-08 03:59:47 +00:00
if c . config . SuperDebug {
2017-09-03 04:50:31 +00:00
fmt . Println ( "Field Node:" , n . Ident )
for _ , id := range n . Ident {
fmt . Println ( "Field Bit:" , id )
2016-12-16 10:37:42 +00:00
}
2017-09-03 04:50:31 +00:00
}
/* Use reflect to determine if the field is for a method, otherwise assume it's a variable. Coming Soon. */
2018-11-18 05:43:10 +00:00
out = c . compileBoolSub ( con , n . String ( ) )
2017-09-03 04:50:31 +00:00
case * parse . ChainNode :
2018-03-21 05:56:33 +00:00
c . detail ( "Chain Node:" , n . Node )
c . detail ( "Node Args:" , node . Args )
2017-09-03 04:50:31 +00:00
case * parse . IdentifierNode :
2018-03-21 05:56:33 +00:00
c . detail ( "Identifier Node:" , node )
c . detail ( "Node Args:" , node . Args )
2018-11-18 05:43:10 +00:00
out = c . compileIdentSwitchN ( con , node )
2017-09-03 04:50:31 +00:00
case * parse . DotNode :
2018-11-18 05:43:10 +00:00
out = con . VarHolder
2017-09-03 04:50:31 +00:00
case * parse . VariableNode :
2018-03-21 05:56:33 +00:00
c . detail ( "Variable Node:" , n . String ( ) )
c . detail ( "Node Identifier:" , n . Ident )
2018-11-18 05:28:27 +00:00
out , _ = c . compileIfVarSub ( con , n . String ( ) )
2017-09-03 04:50:31 +00:00
case * parse . NilNode :
panic ( "Nil is not a command x.x" )
case * parse . PipeNode :
2018-03-21 05:56:33 +00:00
c . detail ( "Pipe Node!" )
c . detail ( n )
c . detail ( "Node Args:" , node . Args )
2018-11-18 05:28:27 +00:00
out += c . compileIdentSwitchN ( con , node )
2017-09-03 04:50:31 +00:00
default :
2018-11-18 05:28:27 +00:00
c . unknownNode ( firstWord )
2016-12-16 10:37:42 +00:00
}
2018-11-18 05:43:10 +00:00
c . retCall ( "compileExprSwitch" , out )
2017-11-07 22:38:15 +00:00
return out
}
2018-11-18 05:28:27 +00:00
func ( c * CTemplateSet ) unknownNode ( node parse . Node ) {
2017-11-07 22:38:15 +00:00
fmt . Println ( "Unknown Kind:" , reflect . ValueOf ( node ) . Elem ( ) . Kind ( ) )
fmt . Println ( "Unknown Type:" , reflect . ValueOf ( node ) . Elem ( ) . Type ( ) . Name ( ) )
panic ( "I don't know what node this is! Grr..." )
2016-12-16 10:37:42 +00:00
}
2018-11-18 05:28:27 +00:00
func ( c * CTemplateSet ) compileIdentSwitchN ( con CContext , node * parse . CommandNode ) ( out string ) {
2018-03-21 05:56:33 +00:00
c . detail ( "in compileIdentSwitchN" )
2018-11-18 05:28:27 +00:00
out , _ , _ = c . compileIdentSwitch ( con , node )
2017-01-21 18:16:27 +00:00
return out
}
2017-11-08 00:12:50 +00:00
func ( c * CTemplateSet ) dumpSymbol ( pos int , node * parse . CommandNode , symbol string ) {
2018-03-21 05:56:33 +00:00
c . detail ( "symbol: " , symbol )
c . detail ( "node.Args[pos + 1]" , node . Args [ pos + 1 ] )
c . detail ( "node.Args[pos + 2]" , node . Args [ pos + 2 ] )
2017-11-08 00:12:50 +00:00
}
2018-11-18 05:28:27 +00:00
func ( c * CTemplateSet ) compareFunc ( con CContext , pos int , node * parse . CommandNode , compare string ) ( out string ) {
2017-11-08 00:12:50 +00:00
c . dumpSymbol ( pos , node , compare )
2018-11-18 05:28:27 +00:00
return c . compileIfVarSubN ( con , node . Args [ pos + 1 ] . String ( ) ) + " " + compare + " " + c . compileIfVarSubN ( con , node . Args [ pos + 2 ] . String ( ) )
2017-11-07 22:38:15 +00:00
}
2018-11-18 05:28:27 +00:00
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 ( ) )
2017-11-07 22:38:15 +00:00
if val2 . IsValid ( ) {
val = val2
} else if val3 . IsValid ( ) {
val = val3
} else {
// TODO: What does this do?
numSample := 1
val = reflect . ValueOf ( numSample )
}
2017-11-08 00:12:50 +00:00
c . dumpSymbol ( pos , node , symbol )
2017-11-07 22:38:15 +00:00
return leftParam + " " + symbol + " " + rightParam , val
}
2018-11-18 05:28:27 +00:00
func ( c * CTemplateSet ) compareJoin ( con CContext , pos int , node * parse . CommandNode , symbol string ) ( pos2 int , out string ) {
2018-03-21 05:56:33 +00:00
c . detailf ( "Building %s function" , symbol )
2017-11-07 22:38:15 +00:00
if pos == 0 {
fmt . Println ( "pos:" , pos )
panic ( symbol + " is missing a left operand" )
}
if len ( node . Args ) <= pos {
fmt . Println ( "post pos:" , pos )
fmt . Println ( "len(node.Args):" , len ( node . Args ) )
panic ( symbol + " is missing a right operand" )
}
2018-11-18 05:43:10 +00:00
left := c . compileBoolSub ( con , node . Args [ pos - 1 ] . String ( ) )
2017-11-07 22:38:15 +00:00
_ , funcExists := c . funcMap [ node . Args [ pos + 1 ] . String ( ) ]
var right string
if ! funcExists {
2018-11-18 05:43:10 +00:00
right = c . compileBoolSub ( con , node . Args [ pos + 1 ] . String ( ) )
2017-11-07 22:38:15 +00:00
}
out = left + " " + symbol + " " + right
2018-03-21 05:56:33 +00:00
c . detail ( "Left operand:" , node . Args [ pos - 1 ] )
c . detail ( "Right operand:" , node . Args [ pos + 1 ] )
2017-11-07 22:38:15 +00:00
if ! funcExists {
pos ++
}
2018-03-21 05:56:33 +00:00
c . detail ( "pos:" , pos )
c . detail ( "len(node.Args):" , len ( node . Args ) )
2017-11-07 22:38:15 +00:00
return pos , out
}
2018-11-18 05:28:27 +00:00
func ( c * CTemplateSet ) compileIdentSwitch ( con CContext , node * parse . CommandNode ) ( out string , val reflect . Value , literal bool ) {
c . dumpCall ( "compileIdentSwitch" , con , node )
2018-11-17 02:36:02 +00:00
var litString = func ( inner string ) {
out = "w.Write([]byte(" + inner + "))\n"
literal = true
}
2017-09-03 04:50:31 +00:00
ArgLoop :
2017-06-05 11:57:27 +00:00
for pos := 0 ; pos < len ( node . Args ) ; pos ++ {
id := node . Args [ pos ]
2018-03-21 05:56:33 +00:00
c . detail ( "pos:" , pos )
c . detail ( "ID:" , id )
2016-12-16 10:37:42 +00:00
switch id . String ( ) {
2017-09-03 04:50:31 +00:00
case "not" :
out += "!"
2017-11-08 00:12:50 +00:00
case "or" , "and" :
2017-11-07 22:38:15 +00:00
var rout string
2018-11-18 05:28:27 +00:00
pos , rout = c . compareJoin ( con , pos , node , c . funcMap [ id . String ( ) ] . ( string ) ) // TODO: Test this
2017-11-07 22:38:15 +00:00
out += rout
2018-11-17 02:36:02 +00:00
case "le" , "lt" , "gt" , "ge" , "eq" , "ne" :
2018-11-18 05:28:27 +00:00
out += c . compareFunc ( con , pos , node , c . funcMap [ id . String ( ) ] . ( string ) )
2017-09-03 04:50:31 +00:00
break ArgLoop
2018-11-17 02:36:02 +00:00
case "add" , "subtract" , "divide" , "multiply" :
2018-11-18 05:28:27 +00:00
rout , rval := c . simpleMath ( con , pos , node , c . funcMap [ id . String ( ) ] . ( string ) )
2017-11-07 22:38:15 +00:00
out += rout
val = rval
2017-09-03 04:50:31 +00:00
break ArgLoop
2018-11-17 02:36:02 +00:00
case "elapsed" :
leftOperand := node . Args [ pos + 1 ] . String ( )
2018-11-18 05:28:27 +00:00
leftParam , _ := c . compileIfVarSub ( con , leftOperand )
2018-11-17 02:36:02 +00:00
// TODO: Refactor this
// TODO: Validate that this is actually a time.Time
litString ( "time.Since(" + leftParam + ").String()" )
c . importMap [ "time" ] = "time"
2017-09-03 04:50:31 +00:00
break ArgLoop
2017-11-29 02:34:02 +00:00
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 {
panic ( "The left or right operand for function dock cannot be left blank" )
}
2018-11-18 05:28:27 +00:00
leftParam := leftOperand
if leftOperand [ 0 ] != '"' {
leftParam , _ = c . compileIfVarSub ( con , leftParam )
2017-11-29 02:34:02 +00:00
}
if rightOperand [ 0 ] == '"' {
panic ( "The right operand for function dock cannot be a string" )
}
2018-11-18 05:28:27 +00:00
rightParam , val3 := c . compileIfVarSub ( con , rightOperand )
2018-03-08 03:59:47 +00:00
if ! val3 . IsValid ( ) {
2017-11-29 02:34:02 +00:00
panic ( "val3 is invalid" )
}
2018-03-08 03:59:47 +00:00
val = val3
2017-11-29 02:34:02 +00:00
// TODO: Refactor this
2018-11-17 02:36:02 +00:00
litString ( "common.BuildWidget(" + leftParam + "," + rightParam + ")" )
2017-11-29 02:34:02 +00:00
break ArgLoop
2018-03-08 03:59:47 +00:00
case "lang" :
// TODO: Implement string literals properly
leftOperand := node . Args [ pos + 1 ] . String ( )
if len ( leftOperand ) == 0 {
panic ( "The left operand for the language string cannot be left blank" )
}
2018-11-17 02:36:02 +00:00
if leftOperand [ 0 ] != '"' {
2018-03-08 03:59:47 +00:00
panic ( "Phrase names cannot be dynamic" )
}
2018-11-17 02:36:02 +00:00
// ! Slightly crude but it does the job
leftParam := strings . Replace ( leftOperand , "\"" , "" , - 1 )
2018-03-08 03:59:47 +00:00
c . langIndexToName = append ( c . langIndexToName , leftParam )
2018-11-17 02:36:02 +00:00
litString ( "plist[" + strconv . Itoa ( len ( c . langIndexToName ) - 1 ) + "]" )
2018-03-08 03:59:47 +00:00
break ArgLoop
2018-10-10 07:33:51 +00:00
case "level" :
// TODO: Implement level literals
leftOperand := node . Args [ pos + 1 ] . String ( )
if len ( leftOperand ) == 0 {
panic ( "The leftoperand for function level cannot be left blank" )
}
2018-11-18 05:28:27 +00:00
leftParam , _ := c . compileIfVarSub ( con , leftOperand )
2018-10-10 07:33:51 +00:00
// TODO: Refactor this
2018-11-17 02:36:02 +00:00
litString ( "phrases.GetLevelPhrase(" + leftParam + ")" )
2018-11-01 06:43:56 +00:00
c . importMap [ langPkg ] = langPkg
2018-10-10 07:33:51 +00:00
break ArgLoop
2018-03-31 05:25:27 +00:00
case "scope" :
literal = true
break ArgLoop
2018-07-13 11:27:58 +00:00
case "dyntmpl" :
2018-11-18 05:28:27 +00:00
var pageParam , headParam string
2018-10-27 03:21:02 +00:00
// 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 {
panic ( "None of the three operands for function dyntmpl can be left blank" )
}
2018-11-18 05:28:27 +00:00
nameParam := nameOperand
if nameOperand [ 0 ] != '"' {
nameParam , _ = c . compileIfVarSub ( con , nameParam )
2018-10-27 03:21:02 +00:00
}
if pageOperand [ 0 ] == '"' {
panic ( "The page operand for function dyntmpl cannot be a string" )
}
if headOperand [ 0 ] == '"' {
panic ( "The head operand for function dyntmpl cannot be a string" )
}
2018-11-18 05:28:27 +00:00
pageParam , val3 := c . compileIfVarSub ( con , pageOperand )
2018-10-27 03:21:02 +00:00
if ! val3 . IsValid ( ) {
panic ( "val3 is invalid" )
}
2018-11-18 05:28:27 +00:00
headParam , val4 := c . compileIfVarSub ( con , headOperand )
2018-10-27 03:21:02 +00:00
if ! val4 . IsValid ( ) {
panic ( "val4 is invalid" )
}
val = val4
// TODO: Refactor this
// TODO: Call the template function directly rather than going through RunThemeTemplate to eliminate a round of indirection?
out = "{\nerr := common.RunThemeTemplate(" + headParam + ".Theme.Name," + nameParam + "," + pageParam + ",w)\n"
out += "if err != nil {\nreturn err\n}\n}\n"
2018-07-13 11:27:58 +00:00
literal = true
break ArgLoop
2017-09-03 04:50:31 +00:00
default :
2018-03-21 05:56:33 +00:00
c . detail ( "Variable!" )
2017-09-03 04:50:31 +00:00
if len ( node . Args ) > ( pos + 1 ) {
nextNode := node . Args [ pos + 1 ] . String ( )
if nextNode == "or" || nextNode == "and" {
continue
2017-06-05 11:57:27 +00:00
}
2017-09-03 04:50:31 +00:00
}
2018-11-18 05:28:27 +00:00
out += c . compileIfVarSubN ( con , id . String ( ) )
2016-12-16 10:37:42 +00:00
}
}
2018-11-18 05:28:27 +00:00
c . retCall ( "compileIdentSwitch" , out , val , literal )
2017-11-29 02:34:02 +00:00
return out , val , literal
2016-12-16 10:37:42 +00:00
}
2018-11-18 05:28:27 +00:00
func ( c * CTemplateSet ) compileReflectSwitch ( con CContext , node * parse . CommandNode ) ( out string , outVal reflect . Value ) {
2018-11-18 05:43:10 +00:00
c . dumpCall ( "compileReflectSwitch" , con , node )
2016-12-16 10:37:42 +00:00
firstWord := node . Args [ 0 ]
switch n := firstWord . ( type ) {
2017-09-03 04:50:31 +00:00
case * parse . FieldNode :
2018-03-08 03:59:47 +00:00
if c . config . SuperDebug {
2017-09-03 04:50:31 +00:00
fmt . Println ( "Field Node:" , n . Ident )
for _ , id := range n . Ident {
fmt . Println ( "Field Bit:" , id )
2016-12-16 10:37:42 +00:00
}
2017-09-03 04:50:31 +00:00
}
/* Use reflect to determine if the field is for a method, otherwise assume it's a variable. Coming Soon. */
2018-11-18 05:28:27 +00:00
return c . compileIfVarSub ( con , n . String ( ) )
2017-09-03 04:50:31 +00:00
case * parse . ChainNode :
2018-03-21 05:56:33 +00:00
c . detail ( "Chain Node:" , n . Node )
c . detail ( "node.Args:" , node . Args )
2017-09-03 04:50:31 +00:00
case * parse . DotNode :
2018-11-18 05:28:27 +00:00
return con . VarHolder , con . HoldReflect
2017-09-03 04:50:31 +00:00
case * parse . NilNode :
panic ( "Nil is not a command x.x" )
default :
//panic("I don't know what node this is")
2016-12-16 10:37:42 +00:00
}
2018-11-18 05:43:10 +00:00
return out , outVal
2016-12-16 10:37:42 +00:00
}
2018-11-18 05:28:27 +00:00
func ( c * CTemplateSet ) compileIfVarSubN ( con CContext , varname string ) ( out string ) {
c . dumpCall ( "compileIfVarSubN" , con , varname )
out , _ = c . compileIfVarSub ( con , varname )
2016-12-16 10:37:42 +00:00
return out
}
2018-11-18 05:28:27 +00:00
func ( c * CTemplateSet ) compileIfVarSub ( con CContext , varname string ) ( out string , val reflect . Value ) {
c . dumpCall ( "compileIfVarSub" , con , varname )
cur := con . HoldReflect
2016-12-16 10:37:42 +00:00
if varname [ 0 ] != '.' && varname [ 0 ] != '$' {
return varname , cur
}
2017-06-16 10:41:30 +00:00
2018-11-18 05:28:27 +00:00
var stepInterface = func ( ) {
if cur . Kind ( ) == reflect . Interface {
cur = cur . Elem ( )
out += ".(" + cur . Type ( ) . Name ( ) + ")"
}
}
2017-09-03 04:50:31 +00:00
bits := strings . Split ( varname , "." )
2016-12-16 10:37:42 +00:00
if varname [ 0 ] == '$' {
var res VarItemReflect
if varname [ 1 ] == '.' {
2018-11-18 05:28:27 +00:00
res = c . localVars [ con . TemplateName ] [ "." ]
2016-12-16 10:37:42 +00:00
} else {
2018-11-18 05:28:27 +00:00
res = c . localVars [ con . TemplateName ] [ strings . TrimPrefix ( bits [ 0 ] , "$" ) ]
2016-12-16 10:37:42 +00:00
}
out += res . Destination
cur = res . Value
2017-06-16 10:41:30 +00:00
2016-12-16 10:37:42 +00:00
if cur . Kind ( ) == reflect . Interface {
cur = cur . Elem ( )
}
} else {
2018-11-18 05:28:27 +00:00
out += con . VarHolder
stepInterface ( )
2016-12-16 10:37:42 +00:00
}
2017-09-03 04:50:31 +00:00
bits [ 0 ] = strings . TrimPrefix ( bits [ 0 ] , "$" )
2017-06-16 10:41:30 +00:00
2018-11-18 05:28:27 +00:00
var dumpKind = func ( pre string ) {
c . detail ( pre + " Kind:" , cur . Kind ( ) )
c . detail ( pre + " Type:" , cur . Type ( ) . Name ( ) )
}
dumpKind ( "Cur" )
2016-12-16 10:37:42 +00:00
for _ , bit := range bits {
2018-03-21 05:56:33 +00:00
c . detail ( "Variable Field:" , bit )
2016-12-16 10:37:42 +00:00
if bit == "" {
continue
}
2017-06-16 10:41:30 +00:00
2017-09-10 16:57:22 +00:00
// TODO: Fix this up so that it works for regular pointers and not just struct pointers. Ditto for the other cur.Kind() == reflect.Ptr we have in this file
2017-08-06 15:22:18 +00:00
if cur . Kind ( ) == reflect . Ptr {
2018-03-21 05:56:33 +00:00
c . detail ( "Looping over pointer" )
2017-08-06 15:22:18 +00:00
for cur . Kind ( ) == reflect . Ptr {
cur = cur . Elem ( )
}
2018-03-21 05:56:33 +00:00
c . detail ( "Data Kind:" , cur . Kind ( ) . String ( ) )
c . detail ( "Field Bit:" , bit )
2017-08-06 15:22:18 +00:00
}
2016-12-16 10:37:42 +00:00
cur = cur . FieldByName ( bit )
2017-11-08 00:12:50 +00:00
out += "." + bit
2018-11-18 05:28:27 +00:00
stepInterface ( )
2017-06-19 08:06:54 +00:00
if ! cur . IsValid ( ) {
2018-06-24 13:49:29 +00:00
fmt . Println ( "cur: " , cur )
2017-06-19 08:06:54 +00:00
panic ( out + "^\n" + "Invalid value. Maybe, it doesn't exist?" )
}
2018-11-18 05:28:27 +00:00
dumpKind ( "Data" )
2016-12-16 10:37:42 +00:00
}
2017-06-16 10:41:30 +00:00
2018-03-21 05:56:33 +00:00
c . detail ( "Out Value:" , out )
2018-11-18 05:28:27 +00:00
dumpKind ( "Out" )
2016-12-16 10:37:42 +00:00
for _ , varItem := range c . varList {
if strings . HasPrefix ( out , varItem . Destination ) {
out = strings . Replace ( out , varItem . Destination , varItem . Name , 1 )
}
}
2017-06-16 10:41:30 +00:00
2016-12-17 03:39:53 +00:00
_ , ok := c . stats [ out ]
if ok {
c . stats [ out ] ++
} else {
c . stats [ out ] = 1
}
2017-06-16 10:41:30 +00:00
2018-11-18 05:43:10 +00:00
c . retCall ( "compileIfVarSub" , out , cur )
2016-12-16 10:37:42 +00:00
return out , cur
}
2018-11-18 05:43:10 +00:00
func ( c * CTemplateSet ) compileBoolSub ( con CContext , varname string ) string {
c . dumpCall ( "compileBoolSub" , con , varname )
2018-11-18 05:28:27 +00:00
out , val := c . compileIfVarSub ( con , varname )
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
// TODO: What if it's a pointer or an interface? I *think* we've got pointers handled somewhere, but not interfaces which we don't know the types of at compile time
2016-12-16 10:37:42 +00:00
switch val . Kind ( ) {
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
case reflect . Int , reflect . Int8 , reflect . Int16 , reflect . Int32 , reflect . Int64 , reflect . Uint , reflect . Uint8 , reflect . Uint16 , reflect . Uint32 , reflect . Uint64 , reflect . Float32 , reflect . Float64 :
2017-09-03 04:50:31 +00:00
out += " > 0"
case reflect . Bool : // Do nothing
case reflect . String :
out += " != \"\""
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
case reflect . Slice , reflect . Map :
out = "len(" + out + ") != 0"
2017-09-03 04:50:31 +00:00
default :
fmt . Println ( "Variable Name:" , varname )
2018-11-18 05:28:27 +00:00
fmt . Println ( "Variable Holder:" , con . VarHolder )
fmt . Println ( "Variable Kind:" , con . HoldReflect . Kind ( ) )
2017-09-03 04:50:31 +00:00
panic ( "I don't know what this variable's type is o.o\n" )
2016-12-16 10:37:42 +00:00
}
2018-11-18 05:43:10 +00:00
c . retCall ( "compileBoolSub" , out )
2016-12-16 10:37:42 +00:00
return out
}
2018-11-18 05:28:27 +00:00
// For debugging the template generator
func ( c * CTemplateSet ) debugParam ( param interface { } , depth int ) ( pstr string ) {
switch p := param . ( type ) {
case CContext :
return "con,"
case reflect . Value :
if p . Kind ( ) == reflect . Ptr || p . Kind ( ) == reflect . Interface {
for p . Kind ( ) == reflect . Ptr || p . Kind ( ) == reflect . Interface {
if p . Kind ( ) == reflect . Ptr {
pstr += "*"
} else {
pstr += "£"
}
p = p . Elem ( )
}
}
kind := p . Kind ( ) . String ( )
if kind != "struct" {
pstr += kind
} else {
pstr += p . Type ( ) . Name ( )
}
return pstr + ","
case string :
return "\"" + p + "\","
case int :
return strconv . Itoa ( p ) + ","
case bool :
if p {
return "true,"
}
return "false,"
case func ( string ) string :
if p == nil {
return "nil,"
}
return "func(string) string),"
default :
return "?,"
}
}
func ( c * CTemplateSet ) dumpCall ( name string , params ... interface { } ) {
var pstr string
for _ , param := range params {
pstr += c . debugParam ( param , 0 )
}
if len ( pstr ) > 0 {
pstr = pstr [ : len ( pstr ) - 1 ]
}
c . detail ( "called " + name + "(" + pstr + ")" )
}
func ( c * CTemplateSet ) retCall ( name string , params ... interface { } ) {
var pstr string
for _ , param := range params {
pstr += c . debugParam ( param , 0 )
}
if len ( pstr ) > 0 {
pstr = pstr [ : len ( pstr ) - 1 ]
}
c . detail ( "returned from " + name + " => (" + pstr + ")" )
}
func ( c * CTemplateSet ) compileVarSub ( con CContext , varname string , val reflect . Value , assLines string , onEnd func ( string ) string ) {
c . dumpCall ( "compileVarSub" , con , varname , val , assLines , onEnd )
if onEnd == nil {
onEnd = func ( in string ) string {
return in
}
}
2017-11-29 02:34:02 +00:00
// Is this a literal string?
if len ( varname ) != 0 && varname [ 0 ] == '"' {
2018-11-18 05:28:27 +00:00
con . Push ( "varsub" , onEnd ( assLines + "w.Write([]byte(" + varname + "))\n" ) )
return
2017-11-29 02:34:02 +00:00
}
2016-12-16 10:37:42 +00:00
for _ , varItem := range c . varList {
if strings . HasPrefix ( varname , varItem . Destination ) {
varname = strings . Replace ( varname , varItem . Destination , varItem . Name , 1 )
}
}
2017-06-16 10:41:30 +00:00
2016-12-17 03:39:53 +00:00
_ , ok := c . stats [ varname ]
if ok {
c . stats [ varname ] ++
} else {
c . stats [ varname ] = 1
}
2016-12-16 10:37:42 +00:00
if val . Kind ( ) == reflect . Interface {
val = val . Elem ( )
}
2018-11-18 05:28:27 +00:00
if val . Kind ( ) == reflect . Ptr {
for val . Kind ( ) == reflect . Ptr {
val = val . Elem ( )
varname = "*" + varname
}
}
2017-06-16 10:41:30 +00:00
2018-03-21 05:56:33 +00:00
c . detail ( "varname: " , varname )
c . detail ( "assLines: " , assLines )
2018-11-18 05:28:27 +00:00
var base string
2016-12-16 10:37:42 +00:00
switch val . Kind ( ) {
2017-09-03 04:50:31 +00:00
case reflect . Int :
c . importMap [ "strconv" ] = "strconv"
2018-11-18 05:28:27 +00:00
base = "w.Write([]byte(strconv.Itoa(" + varname + ")))\n"
2017-09-03 04:50:31 +00:00
case reflect . Bool :
2018-11-18 05:28:27 +00:00
base = "if " + varname + " {\nw.Write([]byte(\"true\"))} else {\nw.Write([]byte(\"false\"))\n}\n"
2017-09-03 04:50:31 +00:00
case reflect . String :
if val . Type ( ) . Name ( ) != "string" && ! strings . HasPrefix ( varname , "string(" ) {
2017-11-23 05:37:08 +00:00
varname = "string(" + varname + ")"
2017-09-03 04:50:31 +00:00
}
2018-11-18 05:28:27 +00:00
base = "w.Write([]byte(" + varname + "))\n"
2017-09-03 04:50:31 +00:00
case reflect . Int64 :
c . importMap [ "strconv" ] = "strconv"
2018-11-18 05:28:27 +00:00
base = "w.Write([]byte(strconv.FormatInt(" + varname + ", 10)))\n"
2017-09-03 04:50:31 +00:00
default :
if ! val . IsValid ( ) {
2017-11-23 05:37:08 +00:00
panic ( assLines + varname + "^\n" + "Invalid value. Maybe, it doesn't exist?" )
2017-09-03 04:50:31 +00:00
}
fmt . Println ( "Unknown Variable Name:" , varname )
fmt . Println ( "Unknown Kind:" , val . Kind ( ) )
fmt . Println ( "Unknown Type:" , val . Type ( ) . Name ( ) )
2017-11-29 02:34:02 +00:00
panic ( "-- I don't know what this variable's type is o.o\n" )
2016-12-16 10:37:42 +00:00
}
2018-11-18 05:28:27 +00:00
c . detail ( "base: " , base )
con . Push ( "varsub" , onEnd ( assLines + base ) )
2016-12-16 10:37:42 +00:00
}
2018-11-18 05:28:27 +00:00
func ( c * CTemplateSet ) compileSubTemplate ( pcon CContext , node * parse . TemplateNode ) {
c . detail ( "in compileSubTemplate" )
2018-03-21 05:56:33 +00:00
c . detail ( "Template Node: " , node . Name )
2017-06-16 10:41:30 +00:00
2016-12-16 10:37:42 +00:00
fname := strings . TrimSuffix ( node . Name , filepath . Ext ( node . Name ) )
2018-11-18 05:28:27 +00:00
con := pcon
con . VarHolder = "tmpl_" + fname + "_vars"
con . TemplateName = fname
2016-12-16 10:37:42 +00:00
if node . Pipe != nil {
for _ , cmd := range node . Pipe . Cmds {
firstWord := cmd . Args [ 0 ]
switch firstWord . ( type ) {
2017-09-03 04:50:31 +00:00
case * parse . DotNode :
2018-11-18 05:28:27 +00:00
con . VarHolder = pcon . VarHolder
con . HoldReflect = pcon . HoldReflect
2017-09-03 04:50:31 +00:00
case * parse . NilNode :
panic ( "Nil is not a command x.x" )
default :
2018-03-21 05:56:33 +00:00
c . detail ( "Unknown Node: " , firstWord )
2017-11-29 02:34:02 +00:00
panic ( "" )
2016-12-16 10:37:42 +00:00
}
}
}
2017-06-16 10:41:30 +00:00
2017-09-10 16:57:22 +00:00
// TODO: Cascade errors back up the tree to the caller?
2018-03-21 05:56:33 +00:00
res , err := ioutil . ReadFile ( c . fileDir + "overrides/" + node . Name )
2016-12-16 10:37:42 +00:00
if err != nil {
2018-03-21 05:56:33 +00:00
c . detail ( "override path: " , c . fileDir + "overrides/" + node . Name )
c . detail ( "override err: " , err )
res , err = ioutil . ReadFile ( c . fileDir + node . Name )
if err != nil {
log . Fatal ( err )
}
2016-12-16 10:37:42 +00:00
}
content := string ( res )
2018-03-08 03:59:47 +00:00
if c . config . Minify {
2017-02-11 14:51:16 +00:00
content = minify ( content )
}
2017-06-16 10:41:30 +00:00
2016-12-16 10:37:42 +00:00
tree := parse . New ( node . Name , c . funcMap )
2017-09-03 04:50:31 +00:00
var treeSet = make ( map [ string ] * parse . Tree )
tree , err = tree . Parse ( content , "{{" , "}}" , treeSet , c . funcMap )
2016-12-16 10:37:42 +00:00
if err != nil {
log . Fatal ( err )
}
2017-06-16 10:41:30 +00:00
2018-03-08 03:59:47 +00:00
c . templateList [ fname ] = tree
subtree := c . templateList [ fname ]
2018-03-21 05:56:33 +00:00
c . detail ( "subtree.Root" , subtree . Root )
2017-06-16 10:41:30 +00:00
2016-12-16 10:37:42 +00:00
c . localVars [ fname ] = make ( map [ string ] VarItemReflect )
2018-11-18 05:28:27 +00:00
c . localVars [ fname ] [ "." ] = VarItemReflect { "." , con . VarHolder , con . HoldReflect }
2018-03-12 04:52:47 +00:00
c . fragmentCursor [ fname ] = 0
2018-11-18 05:28:27 +00:00
c . rootIterate ( subtree , con )
2018-03-12 04:52:47 +00:00
c . TemplateFragmentCount [ fname ] = c . fragmentCursor [ fname ] + 1
2016-12-16 10:37:42 +00:00
}
2018-03-08 03:59:47 +00:00
// TODO: Should we rethink the way the log methods work or their names?
2018-03-21 05:56:33 +00:00
func ( c * CTemplateSet ) detail ( args ... interface { } ) {
2018-03-08 03:59:47 +00:00
if c . config . SuperDebug {
2017-11-08 00:12:50 +00:00
fmt . Println ( args ... )
}
}
2018-03-21 05:56:33 +00:00
func ( c * CTemplateSet ) detailf ( left string , args ... interface { } ) {
2018-03-08 03:59:47 +00:00
if c . config . SuperDebug {
2017-11-29 02:34:02 +00:00
fmt . Printf ( left , args ... )
}
}
2017-11-23 05:37:08 +00:00
func ( c * CTemplateSet ) error ( args ... interface { } ) {
2018-03-08 03:59:47 +00:00
if c . config . Debug {
2017-11-23 05:37:08 +00:00
fmt . Println ( args ... )
}
}