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 )
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-04-22 12:33:56 +00:00
Minify bool
Debug bool
SuperDebug bool
SkipHandles bool
SkipInitBlock bool
PackageName 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
expectsInt interface { }
}
func NewCTemplateSet ( ) * CTemplateSet {
return & CTemplateSet {
config : CTemplateConfig {
PackageName : "main" ,
} ,
baseImportMap : map [ string ] string { } ,
funcMap : map [ string ] interface { } {
"and" : "&&" ,
"not" : "!" ,
"or" : "||" ,
"eq" : true ,
"ge" : true ,
"gt" : true ,
"le" : true ,
"lt" : true ,
"ne" : true ,
"add" : true ,
"subtract" : true ,
"multiply" : true ,
"divide" : true ,
"dock" : true ,
"lang" : true ,
"scope" : true ,
} ,
}
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-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
holdreflect := reflect . ValueOf ( 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
}
2017-06-16 10:41:30 +00:00
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
out = ""
fname := strings . TrimSuffix ( name , filepath . Ext ( name ) )
2018-03-08 03:59:47 +00:00
c . templateList = map [ string ] * parse . Tree { fname : tree }
2016-12-16 10:37:42 +00:00
varholder := "tmpl_" + fname + "_vars"
2017-06-16 10:41:30 +00:00
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 )
2017-09-03 04:50:31 +00:00
c . localVars [ fname ] [ "." ] = VarItemReflect { "." , varholder , 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-03-08 03:59:47 +00:00
out += c . rootIterate ( c . templateList [ fname ] , varholder , holdreflect , fname )
2018-03-12 04:52:47 +00:00
c . TemplateFragmentCount [ fname ] = c . fragmentCursor [ fname ] + 1
2017-06-16 10:41:30 +00:00
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
2017-08-13 11:22:34 +00:00
fout := "// +build !no_templategen\n\n// 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"
2017-11-11 06:33:08 +00:00
2018-04-22 12:33:56 +00:00
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-04-22 12:33:56 +00:00
fout += "\tcommon.TmplPtrMap[\"o_" + fname + "\"] = Template_" + fname + "\n"
if len ( c . langIndexToName ) > 0 {
fout += "\t" + fname + "_tmpl_phrase_id = common.RegisterTmplPhraseNames([]string{\n"
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-03-12 04:52:47 +00:00
fout += "var phrases = common.GetTmplPhrasesBytes(" + fname + "_tmpl_phrase_id)\n"
2018-03-08 03:59:47 +00:00
}
fout += varString + out + "\treturn 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
}
2017-11-06 16:24:45 +00:00
func ( c * CTemplateSet ) rootIterate ( tree * parse . Tree , varholder string , holdreflect reflect . Value , fname string ) ( out string ) {
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 ( )
}
out += c . compileSwitch ( varholder , holdreflect , fname , node )
}
return out
}
2017-11-07 22:38:15 +00:00
func ( c * CTemplateSet ) compileSwitch ( varholder string , holdreflect reflect . Value , templateName string , node parse . Node ) ( out string ) {
2018-03-21 05:56:33 +00:00
c . detail ( "in compileSwitch" )
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 {
out += c . compileSubswitch ( varholder , holdreflect , templateName , cmd )
}
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 ( ) )
2017-09-03 04:50:31 +00:00
expr += c . compileVarswitch ( varholder , holdreflect , templateName , cmd )
2018-03-21 05:56:33 +00:00
c . detail ( "Expression Step:" , c . compileVarswitch ( varholder , holdreflect , templateName , 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-03-08 03:59:47 +00:00
out = "if " + expr + " {\n" + c . compileSwitch ( varholder , holdreflect , templateName , 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-03-08 03:59:47 +00:00
return out + "\n"
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 ( "Selected Branch 2" )
2018-03-08 03:59:47 +00:00
return out + " else {\n" + c . compileSwitch ( varholder , holdreflect , templateName , node . ElseList ) + "}\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 {
out += c . compileSwitch ( varholder , holdreflect , templateName , subnode )
}
case * parse . RangeNode :
2017-11-07 22:38:15 +00:00
return c . compileRangeNode ( varholder , holdreflect , templateName , node )
2017-09-03 04:50:31 +00:00
case * parse . TemplateNode :
return c . compileSubtemplate ( varholder , holdreflect , node )
case * parse . TextNode :
c . previousNode = c . currentNode
c . currentNode = node . Type ( )
c . nextNode = 0
tmpText := bytes . TrimSpace ( node . Text )
if len ( tmpText ) == 0 {
return ""
}
2018-03-12 04:52:47 +00:00
fragmentName := templateName + "_" + strconv . Itoa ( c . fragmentCursor [ templateName ] )
fragmentPrefix := templateName + "_frags[" + strconv . Itoa ( c . fragmentCursor [ 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-03-12 04:52:47 +00:00
c . fragmentCursor [ templateName ] = c . fragmentCursor [ templateName ] + 1
return "w.Write(" + fragmentPrefix + ")\n"
2017-09-03 04:50:31 +00:00
default :
2017-11-07 22:38:15 +00:00
return c . unknownNode ( node )
2016-12-16 10:37:42 +00:00
}
2017-11-07 22:38:15 +00:00
return out
}
func ( c * CTemplateSet ) compileRangeNode ( varholder string , holdreflect reflect . Value , templateName string , node * parse . RangeNode ) ( out string ) {
2018-03-21 05:56:33 +00:00
c . detail ( "Range Node!" )
c . detail ( node . Pipe )
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 )
2017-11-08 00:12:50 +00:00
out , outVal = c . compileReflectSwitch ( varholder , holdreflect , templateName , cmd )
2017-11-07 22:38:15 +00:00
}
2018-03-21 05:56:33 +00:00
c . detail ( "Returned:" , out )
c . detail ( "Range Kind Switch!" )
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-08 03:59:47 +00:00
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-03-08 03:59:47 +00:00
out = "if len(" + out + ") != 0 {\nfor _, item := range " + out + " {\n" + c . compileSwitch ( "item" , item , templateName , node . List ) + "}\n}"
2017-11-07 22:38:15 +00:00
if node . ElseList != nil {
2018-03-08 03:59:47 +00:00
out += " else {\n" + c . compileSwitch ( "item" , item , templateName , node . ElseList ) + "}\n"
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!" )
}
item := outVal . Index ( 0 )
out = "if len(" + out + ") != 0 {\nfor _, item := range " + out + " {\n" + c . compileSwitch ( "item" , item , templateName , node . List ) + "}\n}"
2018-03-08 03:59:47 +00:00
if node . ElseList != nil {
out += " else {\n" + c . compileSwitch ( varholder , holdreflect , templateName , node . ElseList ) + "}"
}
2017-11-07 22:38:15 +00:00
case reflect . Invalid :
return ""
}
return out + "\n"
2016-12-16 10:37:42 +00:00
}
2017-09-03 04:50:31 +00:00
func ( c * CTemplateSet ) compileSubswitch ( varholder string , holdreflect reflect . Value , templateName string , node * parse . CommandNode ) ( out string ) {
2018-03-21 05:56:33 +00:00
c . detail ( "in compileSubswitch" )
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! */
cur := holdreflect
2017-06-16 10:41:30 +00:00
2017-09-03 04:50:31 +00:00
var varbit string
if cur . Kind ( ) == reflect . Interface {
cur = cur . Elem ( )
varbit += ".(" + cur . Type ( ) . Name ( ) + ")"
}
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:" )
c . error ( "Holdreflect:" , holdreflect )
c . error ( "Holdreflect.Kind():" , 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 {
panic ( varholder + varbit + "^\n" + "Invalid value. Maybe, it doesn't exist?" )
2017-09-10 16:57:22 +00:00
}
2018-03-08 03:59:47 +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-03-21 05:56:33 +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 ) )
varbit += "[\"" + id + "\"]"
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 ++
}
varholder = "disp" + dispStr
2018-03-08 03:59:47 +00:00
varbit = varholder + " " + newVarByte + "= " + varholder + varbit + "\n"
2017-11-23 05:37:08 +00:00
multiline = true
} else {
continue
}
}
if cur . Kind ( ) != reflect . Interface {
cur = cur . FieldByName ( id )
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 ( )
2017-11-23 05:37:08 +00:00
varbit += ".("
// 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"
2017-11-23 05:37:08 +00:00
varbit += strings . TrimPrefix ( cur . Type ( ) . PkgPath ( ) , "html/" ) + "."
2017-09-03 04:50:31 +00:00
}
2017-11-23 05:37:08 +00:00
varbit += cur . Type ( ) . Name ( ) + ")"
2016-12-16 10:37:42 +00:00
}
2018-03-21 05:56:33 +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 {
assSplit := strings . Split ( varbit , "\n" )
varbit = assSplit [ len ( assSplit ) - 1 ]
assSplit = assSplit [ : len ( assSplit ) - 1 ]
assLines = strings . Join ( assSplit , "\n" ) + "\n"
}
varbit = varholder + varbit
out = c . compileVarsub ( varbit , cur , assLines )
2017-09-03 04:50:31 +00:00
for _ , varItem := range c . varList {
if strings . HasPrefix ( out , varItem . Destination ) {
out = strings . Replace ( out , varItem . Destination , varItem . Name , 1 )
2017-01-21 18:16:27 +00:00
}
2017-09-03 04:50:31 +00:00
}
return out
case * parse . DotNode :
2018-03-21 05:56:33 +00:00
c . detail ( "Dot Node:" , node . String ( ) )
2017-11-23 05:37:08 +00:00
return c . compileVarsub ( varholder , holdreflect , "" )
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 )
2017-09-03 04:50:31 +00:00
varname , reflectVal := c . compileIfVarsub ( n . String ( ) , varholder , templateName , holdreflect )
2017-11-23 05:37:08 +00:00
return c . compileVarsub ( varname , reflectVal , "" )
2017-09-03 04:50:31 +00:00
case * parse . StringNode :
return n . Quoted
case * parse . IdentifierNode :
2018-03-21 05:56:33 +00:00
c . detail ( "Identifier Node:" , node )
c . detail ( "Identifier Node Args:" , node . Args )
2017-11-29 02:34:02 +00:00
out , outval , lit := c . compileIdentSwitch ( varholder , holdreflect , templateName , node )
if lit {
return out
}
2017-11-23 05:37:08 +00:00
return c . compileVarsub ( out , outval , "" )
2017-09-03 04:50:31 +00:00
default :
2017-11-07 22:38:15 +00:00
return c . unknownNode ( node )
2016-12-16 10:37:42 +00:00
}
}
2017-09-03 04:50:31 +00:00
func ( c * CTemplateSet ) compileVarswitch ( varholder string , holdreflect reflect . Value , templateName string , node * parse . CommandNode ) ( out string ) {
2018-03-21 05:56:33 +00:00
c . detail ( "in compileVarswitch" )
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
}
2017-06-16 10:41:30 +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. */
return c . compileBoolsub ( n . String ( ) , varholder , templateName , holdreflect )
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 )
2017-11-08 00:12:50 +00:00
return c . compileIdentSwitchN ( varholder , holdreflect , templateName , node )
2017-09-03 04:50:31 +00:00
case * parse . DotNode :
return varholder
case * parse . VariableNode :
2018-03-21 05:56:33 +00:00
c . detail ( "Variable Node:" , n . String ( ) )
c . detail ( "Node Identifier:" , n . Ident )
2017-09-03 04:50:31 +00:00
out , _ = c . compileIfVarsub ( n . String ( ) , varholder , templateName , holdreflect )
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 )
2017-11-08 00:12:50 +00:00
out += c . compileIdentSwitchN ( varholder , holdreflect , templateName , node )
2018-03-21 05:56:33 +00:00
c . detail ( "Out:" , out )
2017-09-03 04:50:31 +00:00
default :
2017-11-07 22:38:15 +00:00
return c . unknownNode ( firstWord )
2016-12-16 10:37:42 +00:00
}
2017-11-07 22:38:15 +00:00
return out
}
func ( c * CTemplateSet ) unknownNode ( node parse . Node ) ( out string ) {
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
}
2017-11-08 00:12:50 +00:00
func ( c * CTemplateSet ) compileIdentSwitchN ( varholder string , holdreflect reflect . Value , templateName string , node * parse . CommandNode ) ( out string ) {
2018-03-21 05:56:33 +00:00
c . detail ( "in compileIdentSwitchN" )
2017-11-29 02:34:02 +00:00
out , _ , _ = c . compileIdentSwitch ( varholder , holdreflect , templateName , 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
}
2017-11-07 22:38:15 +00:00
func ( c * CTemplateSet ) compareFunc ( varholder string , holdreflect reflect . Value , templateName string , pos int , node * parse . CommandNode , compare string ) ( out string ) {
2017-11-08 00:12:50 +00:00
c . dumpSymbol ( pos , node , compare )
2017-11-07 22:38:15 +00:00
return c . compileIfVarsubN ( node . Args [ pos + 1 ] . String ( ) , varholder , templateName , holdreflect ) + " " + compare + " " + c . compileIfVarsubN ( node . Args [ pos + 2 ] . String ( ) , varholder , templateName , holdreflect )
}
func ( c * CTemplateSet ) simpleMath ( varholder string , holdreflect reflect . Value , templateName string , pos int , node * parse . CommandNode , symbol string ) ( out string , val reflect . Value ) {
leftParam , val2 := c . compileIfVarsub ( node . Args [ pos + 1 ] . String ( ) , varholder , templateName , holdreflect )
rightParam , val3 := c . compileIfVarsub ( node . Args [ pos + 2 ] . String ( ) , varholder , templateName , holdreflect )
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
}
func ( c * CTemplateSet ) compareJoin ( varholder string , holdreflect reflect . Value , templateName string , 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" )
}
left := c . compileBoolsub ( node . Args [ pos - 1 ] . String ( ) , varholder , templateName , holdreflect )
_ , funcExists := c . funcMap [ node . Args [ pos + 1 ] . String ( ) ]
var right string
if ! funcExists {
right = c . compileBoolsub ( node . Args [ pos + 1 ] . String ( ) , varholder , templateName , holdreflect )
}
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
}
2017-11-29 02:34:02 +00:00
func ( c * CTemplateSet ) compileIdentSwitch ( varholder string , holdreflect reflect . Value , templateName string , node * parse . CommandNode ) ( out string , val reflect . Value , literal bool ) {
2018-03-21 05:56:33 +00:00
c . detail ( "in compileIdentSwitch" )
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
2017-11-08 00:12:50 +00:00
pos , rout = c . compareJoin ( varholder , holdreflect , templateName , pos , node , c . funcMap [ id . String ( ) ] . ( string ) ) // TODO: Test this
2017-11-07 22:38:15 +00:00
out += rout
case "le" : // TODO: Can we condense these comparison cases down into one?
out += c . compareFunc ( varholder , holdreflect , templateName , pos , node , "<=" )
2017-09-03 04:50:31 +00:00
break ArgLoop
case "lt" :
2017-11-07 22:38:15 +00:00
out += c . compareFunc ( varholder , holdreflect , templateName , pos , node , "<" )
2017-09-03 04:50:31 +00:00
break ArgLoop
case "gt" :
2017-11-07 22:38:15 +00:00
out += c . compareFunc ( varholder , holdreflect , templateName , pos , node , ">" )
2017-09-03 04:50:31 +00:00
break ArgLoop
case "ge" :
2017-11-07 22:38:15 +00:00
out += c . compareFunc ( varholder , holdreflect , templateName , pos , node , ">=" )
2017-09-03 04:50:31 +00:00
break ArgLoop
case "eq" :
2017-11-07 22:38:15 +00:00
out += c . compareFunc ( varholder , holdreflect , templateName , pos , node , "==" )
2017-09-03 04:50:31 +00:00
break ArgLoop
case "ne" :
2017-11-07 22:38:15 +00:00
out += c . compareFunc ( varholder , holdreflect , templateName , pos , node , "!=" )
2017-09-03 04:50:31 +00:00
break ArgLoop
case "add" :
2017-11-07 22:38:15 +00:00
rout , rval := c . simpleMath ( varholder , holdreflect , templateName , pos , node , "+" )
out += rout
val = rval
2017-09-03 04:50:31 +00:00
break ArgLoop
case "subtract" :
2017-11-07 22:38:15 +00:00
rout , rval := c . simpleMath ( varholder , holdreflect , templateName , pos , node , "-" )
out += rout
val = rval
2017-09-03 04:50:31 +00:00
break ArgLoop
case "divide" :
2017-11-07 22:38:15 +00:00
rout , rval := c . simpleMath ( varholder , holdreflect , templateName , pos , node , "/" )
out += rout
val = rval
2017-09-03 04:50:31 +00:00
break ArgLoop
case "multiply" :
2017-11-07 22:38:15 +00:00
rout , rval := c . simpleMath ( varholder , holdreflect , templateName , pos , node , "*" )
out += rout
val = rval
2017-09-03 04:50:31 +00:00
break ArgLoop
2017-11-29 02:34:02 +00:00
case "dock" :
var leftParam , rightParam string
// 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" )
}
if leftOperand [ 0 ] == '"' {
leftParam = leftOperand
} else {
leftParam , _ = c . compileIfVarsub ( leftOperand , varholder , templateName , holdreflect )
}
if rightOperand [ 0 ] == '"' {
panic ( "The right operand for function dock cannot be a string" )
}
rightParam , val3 := c . compileIfVarsub ( rightOperand , varholder , templateName , holdreflect )
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
out = "w.Write([]byte(common.BuildWidget(" + leftParam + "," + rightParam + ")))\n"
literal = true
break ArgLoop
2018-03-08 03:59:47 +00:00
case "lang" :
var leftParam string
// 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" )
}
if leftOperand [ 0 ] == '"' {
// ! Slightly crude but it does the job
leftParam = strings . Replace ( leftOperand , "\"" , "" , - 1 )
} else {
panic ( "Phrase names cannot be dynamic" )
}
c . langIndexToName = append ( c . langIndexToName , leftParam )
out = "w.Write(phrases[" + strconv . Itoa ( len ( c . langIndexToName ) - 1 ) + "])\n"
literal = true
break ArgLoop
2018-03-31 05:25:27 +00:00
case "scope" :
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
}
out += c . compileIfVarsubN ( id . String ( ) , varholder , templateName , holdreflect )
2016-12-16 10:37:42 +00:00
}
}
2017-11-29 02:34:02 +00:00
return out , val , literal
2016-12-16 10:37:42 +00:00
}
2017-11-08 00:12:50 +00:00
func ( c * CTemplateSet ) compileReflectSwitch ( varholder string , holdreflect reflect . Value , templateName string , node * parse . CommandNode ) ( out string , outVal reflect . Value ) {
2018-03-21 05:56:33 +00:00
c . detail ( "in compileReflectSwitch" )
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. */
return c . compileIfVarsub ( n . String ( ) , varholder , templateName , holdreflect )
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 :
return varholder , holdreflect
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
}
return "" , outVal
}
2017-09-03 04:50:31 +00:00
func ( c * CTemplateSet ) compileIfVarsubN ( varname string , varholder string , templateName string , cur reflect . Value ) ( out string ) {
2018-03-21 05:56:33 +00:00
c . detail ( "in compileIfVarsubN" )
2017-09-03 04:50:31 +00:00
out , _ = c . compileIfVarsub ( varname , varholder , templateName , cur )
2016-12-16 10:37:42 +00:00
return out
}
2017-09-03 04:50:31 +00:00
func ( c * CTemplateSet ) compileIfVarsub ( varname string , varholder string , templateName string , cur reflect . Value ) ( out string , val reflect . Value ) {
2018-03-21 05:56:33 +00:00
c . detail ( "in compileIfVarsub" )
2016-12-16 10:37:42 +00:00
if varname [ 0 ] != '.' && varname [ 0 ] != '$' {
return varname , cur
}
2017-06-16 10:41:30 +00:00
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 ] == '.' {
2017-09-03 04:50:31 +00:00
res = c . localVars [ templateName ] [ "." ]
2016-12-16 10:37:42 +00:00
} else {
2017-09-03 04:50:31 +00:00
res = c . localVars [ 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 {
2017-11-08 00:12:50 +00:00
out += varholder
2016-12-16 10:37:42 +00:00
if cur . Kind ( ) == reflect . Interface {
cur = cur . Elem ( )
2017-11-08 00:12:50 +00:00
out += ".(" + cur . Type ( ) . Name ( ) + ")"
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-03-21 05:56:33 +00:00
c . detail ( "Cur Kind:" , cur . Kind ( ) )
c . detail ( "Cur Type:" , cur . Type ( ) . Name ( ) )
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
2016-12-16 10:37:42 +00:00
if cur . Kind ( ) == reflect . Interface {
cur = cur . Elem ( )
2017-11-08 00:12:50 +00:00
out += ".(" + cur . Type ( ) . Name ( ) + ")"
2016-12-16 10:37:42 +00:00
}
2017-06-19 08:06:54 +00:00
if ! cur . IsValid ( ) {
panic ( out + "^\n" + "Invalid value. Maybe, it doesn't exist?" )
}
2018-03-21 05:56:33 +00:00
c . detail ( "Data Kind:" , cur . Kind ( ) )
c . detail ( "Data Type:" , cur . Type ( ) . Name ( ) )
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 )
c . detail ( "Out Kind:" , cur . Kind ( ) )
c . detail ( "Out Type:" , cur . Type ( ) . Name ( ) )
2017-06-16 10:41:30 +00:00
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
2018-03-21 05:56:33 +00:00
c . detail ( "Out Value:" , out )
c . detail ( "Out Kind:" , cur . Kind ( ) )
c . detail ( "Out Type:" , cur . Type ( ) . Name ( ) )
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
2016-12-16 10:37:42 +00:00
return out , cur
}
2017-09-03 04:50:31 +00:00
func ( c * CTemplateSet ) compileBoolsub ( varname string , varholder string , templateName string , val reflect . Value ) string {
2018-03-21 05:56:33 +00:00
c . detail ( "in compileBoolsub" )
2017-09-03 04:50:31 +00:00
out , val := c . compileIfVarsub ( varname , varholder , templateName , val )
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 )
fmt . Println ( "Variable Holder:" , varholder )
fmt . Println ( "Variable Kind:" , val . Kind ( ) )
panic ( "I don't know what this variable's type is o.o\n" )
2016-12-16 10:37:42 +00:00
}
return out
}
2017-11-23 05:37:08 +00:00
func ( c * CTemplateSet ) compileVarsub ( varname string , val reflect . Value , assLines string ) ( out string ) {
2018-03-21 05:56:33 +00:00
c . detail ( "in compileVarsub" )
2017-11-29 02:34:02 +00:00
// Is this a literal string?
if len ( varname ) != 0 && varname [ 0 ] == '"' {
return assLines + "w.Write([]byte(" + varname + "))\n"
}
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
}
2017-06-16 10:41:30 +00:00
2016-12-16 10:37:42 +00:00
if val . Kind ( ) == reflect . Interface {
val = val . Elem ( )
}
2017-06-16 10:41:30 +00:00
2018-03-21 05:56:33 +00:00
c . detail ( "varname: " , varname )
c . detail ( "assLines: " , assLines )
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"
2017-11-23 05:37:08 +00:00
out = "w.Write([]byte(strconv.Itoa(" + varname + ")))\n"
2017-09-03 04:50:31 +00:00
case reflect . Bool :
2017-11-23 05:37:08 +00:00
out = "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
}
2017-11-23 05:37:08 +00:00
out = "w.Write([]byte(" + varname + "))\n"
2017-09-03 04:50:31 +00:00
case reflect . Int64 :
c . importMap [ "strconv" ] = "strconv"
2017-11-23 05:37:08 +00:00
out = "w.Write([]byte(strconv.FormatInt(" + varname + ", 10)))"
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-03-21 05:56:33 +00:00
c . detail ( "out: " , out )
2017-11-23 05:37:08 +00:00
return assLines + out
2016-12-16 10:37:42 +00:00
}
2017-09-03 04:50:31 +00:00
func ( c * CTemplateSet ) compileSubtemplate ( pvarholder string , pholdreflect reflect . Value , node * parse . TemplateNode ) ( out string ) {
2018-03-21 05:56:33 +00:00
c . detail ( "in compileSubtemplate" )
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 ) )
varholder := "tmpl_" + fname + "_vars"
var holdreflect reflect . Value
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 :
varholder = pvarholder
holdreflect = pholdreflect
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
}
2017-06-16 10:41:30 +00:00
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 )
2017-09-03 04:50:31 +00:00
c . localVars [ fname ] [ "." ] = VarItemReflect { "." , varholder , holdreflect }
2018-03-12 04:52:47 +00:00
c . fragmentCursor [ fname ] = 0
2017-06-16 10:41:30 +00:00
2017-11-06 16:24:45 +00:00
out += c . rootIterate ( subtree , varholder , holdreflect , fname )
2018-03-12 04:52:47 +00:00
c . TemplateFragmentCount [ fname ] = c . fragmentCursor [ fname ] + 1
2017-06-16 10:41:30 +00:00
return out
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 ... )
}
}