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"
"log"
2017-06-05 11:57:27 +00:00
"strconv"
2017-09-03 04:50:31 +00:00
"strings"
2017-06-05 11:57:27 +00:00
//"regexp"
"io/ioutil"
2017-09-03 04:50:31 +00:00
"path/filepath"
"reflect"
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
// nolint
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
}
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
}
2017-09-03 04:50:31 +00:00
type CTemplateSet struct {
2017-11-23 05:37:08 +00:00
tlist map [ string ] * parse . Tree
dir string
funcMap map [ string ] interface { }
importMap map [ string ] string
Fragments map [ string ] int
FragmentCursor map [ string ] int
FragOut string
varList map [ string ] VarItem
localVars map [ string ] map [ string ] VarItemReflect
hasDispInt bool
localDispStructIndex int
stats map [ string ] int
pVarList string
pVarPosition int
previousNode parse . NodeType
currentNode parse . NodeType
nextNode parse . NodeType
2016-12-16 10:37:42 +00:00
//tempVars map[string]string
2017-11-11 06:33:08 +00:00
doImports bool
minify bool
debug bool
superDebug bool
skipHandles bool
expectsInt interface { }
2016-12-16 10:37:42 +00:00
}
2017-11-11 04:06:16 +00:00
func ( c * CTemplateSet ) Minify ( on bool ) {
c . minify = on
}
func ( c * CTemplateSet ) Debug ( on bool ) {
c . debug = on
}
func ( c * CTemplateSet ) SuperDebug ( on bool ) {
c . superDebug = on
}
2017-11-11 06:33:08 +00:00
func ( c * CTemplateSet ) SkipHandles ( on bool ) {
c . skipHandles = on
}
func ( c * CTemplateSet ) Compile ( name string , dir string , expects string , expectsInt interface { } , varList map [ string ] VarItem , imports ... string ) ( out string , err error ) {
2017-11-11 04:06:16 +00:00
if c . debug {
2017-08-06 15:22:18 +00:00
fmt . Println ( "Compiling template '" + name + "'" )
}
2016-12-16 10:37:42 +00:00
c . dir = dir
c . doImports = true
2017-04-12 10:10:36 +00:00
c . funcMap = map [ string ] interface { } {
2017-09-03 04:50:31 +00:00
"and" : "&&" ,
"not" : "!" ,
"or" : "||" ,
"eq" : true ,
"ge" : true ,
"gt" : true ,
"le" : true ,
"lt" : true ,
"ne" : true ,
"add" : true ,
2017-04-12 10:10:36 +00:00
"subtract" : true ,
"multiply" : true ,
2017-09-03 04:50:31 +00:00
"divide" : true ,
2017-11-29 02:34:02 +00:00
"dock" : true ,
2017-04-12 10:10:36 +00:00
}
2017-06-16 10:41:30 +00:00
2017-04-12 10:10:36 +00:00
c . importMap = map [ string ] string {
2017-09-03 04:50:31 +00:00
"net/http" : "net/http" ,
2017-11-11 04:06:16 +00:00
"./common" : "./common" ,
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
}
}
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.pVarList = ""
//c.pVarPosition = 0
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
2016-12-16 10:37:42 +00:00
res , err := ioutil . ReadFile ( dir + name )
if err != nil {
2017-08-13 11:22:34 +00:00
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 )
2017-11-11 04:06:16 +00:00
if c . 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
}
2017-11-08 00:12:50 +00:00
c . log ( 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 ) )
c . tlist = make ( map [ string ] * parse . Tree )
c . tlist [ fname ] = tree
varholder := "tmpl_" + fname + "_vars"
2017-06-16 10:41:30 +00:00
2017-11-08 00:12:50 +00:00
c . log ( c . tlist )
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 )
}
c . FragmentCursor = make ( map [ string ] int )
c . FragmentCursor [ fname ] = 0
2017-06-16 10:41:30 +00:00
2017-11-06 16:24:45 +00:00
out += c . rootIterate ( c . tlist [ fname ] , varholder , holdreflect , fname )
2017-06-16 10:41:30 +00:00
2016-12-16 10:37:42 +00:00
var importList string
if c . doImports {
for _ , item := range c . importMap {
importList += "import \"" + item + "\"\n"
}
}
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"
2017-11-11 06:33:08 +00:00
2017-08-13 11:22:34 +00:00
fout += "package main\n" + importList + c . pVarList + "\n"
2017-11-11 06:33:08 +00:00
fout += "// nolint\nfunc init() {\n"
if ! c . 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"
}
fout += "\tcommon.TmplPtrMap[\"o_" + fname + "\"] = Template_" + fname + "\n}\n\n"
fout += "// nolint\nfunc Template_" + fname + "(tmpl_" + fname + "_vars " + expects + ", w http.ResponseWriter) error {\n" + 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
2017-11-11 04:06:16 +00:00
if c . 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
}
2017-06-16 10:41:30 +00:00
2017-11-08 00:12:50 +00:00
c . log ( "Output!" )
c . log ( 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 ) {
2017-11-08 00:12:50 +00:00
c . log ( tree . Root )
2017-11-06 16:24:45 +00:00
treeLength := len ( tree . Root . Nodes )
for index , node := range tree . Root . Nodes {
2017-11-11 04:06:16 +00:00
c . log ( "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 ) {
2017-11-08 00:12:50 +00:00
c . log ( "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 :
2017-11-08 00:12:50 +00:00
c . log ( "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 :
2017-11-08 00:12:50 +00:00
c . log ( "If Node:" )
c . log ( "node.Pipe" , node . Pipe )
2017-09-03 04:50:31 +00:00
var expr string
for _ , cmd := range node . Pipe . Cmds {
2017-11-08 00:12:50 +00:00
c . log ( "If Node Bit:" , cmd )
c . log ( "If Node Bit Type:" , reflect . ValueOf ( cmd ) . Type ( ) . Name ( ) )
2017-09-03 04:50:31 +00:00
expr += c . compileVarswitch ( varholder , holdreflect , templateName , cmd )
2017-11-08 00:12:50 +00:00
c . log ( "If Node Expression Step:" , c . compileVarswitch ( varholder , holdreflect , templateName , cmd ) )
2017-09-03 04:50:31 +00:00
}
2017-06-16 10:41:30 +00:00
2017-11-08 00:12:50 +00:00
c . log ( "If Node Expression:" , expr )
2017-09-03 04:50:31 +00:00
c . previousNode = c . currentNode
c . currentNode = parse . NodeList
c . nextNode = - 1
if node . ElseList == nil {
2017-11-08 00:12:50 +00:00
c . log ( "Selected Branch 1" )
2017-09-03 04:50:31 +00:00
return "if " + expr + " {\n" + c . compileSwitch ( varholder , holdreflect , templateName , node . List ) + "}\n"
}
2017-06-16 10:41:30 +00:00
2017-11-08 00:12:50 +00:00
c . log ( "Selected Branch 2" )
2017-09-03 04:50:31 +00:00
return "if " + expr + " {\n" + c . compileSwitch ( varholder , holdreflect , templateName , node . List ) + "} else {\n" + c . compileSwitch ( varholder , holdreflect , templateName , node . ElseList ) + "}\n"
case * parse . ListNode :
2017-11-08 00:12:50 +00:00
c . log ( "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 ""
}
fragmentName := templateName + "_" + strconv . Itoa ( c . FragmentCursor [ templateName ] )
_ , ok := c . Fragments [ fragmentName ]
if ! ok {
c . Fragments [ fragmentName ] = len ( node . Text )
c . FragOut += "var " + fragmentName + " = []byte(`" + string ( node . Text ) + "`)\n"
}
c . FragmentCursor [ templateName ] = c . FragmentCursor [ templateName ] + 1
return "w.Write(" + fragmentName + ")\n"
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 ) {
2017-11-08 00:12:50 +00:00
c . log ( "Range Node!" )
c . log ( node . Pipe )
2017-11-07 22:38:15 +00:00
var outVal reflect . Value
for _ , cmd := range node . Pipe . Cmds {
2017-11-11 04:06:16 +00:00
c . log ( "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
}
2017-11-08 00:12:50 +00:00
c . log ( "Returned:" , out )
c . log ( "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 )
}
2017-11-11 04:06:16 +00:00
if c . debug {
2017-11-07 22:38:15 +00:00
fmt . Println ( "Range item:" , item )
}
if ! item . IsValid ( ) {
panic ( "item" + "^\n" + "Invalid map. Maybe, it doesn't have any entries for the template engine to analyse?" )
}
if node . ElseList != nil {
out = "if len(" + out + ") != 0 {\nfor _, item := range " + out + " {\n" + c . compileSwitch ( "item" , item , templateName , node . List ) + "}\n} else {\n" + c . compileSwitch ( "item" , item , templateName , node . ElseList ) + "}\n"
} else {
out = "if len(" + out + ") != 0 {\nfor _, item := range " + out + " {\n" + c . compileSwitch ( "item" , item , templateName , node . List ) + "}\n}"
}
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}"
case reflect . Invalid :
return ""
}
if node . ElseList != nil {
out += " else {\n" + c . compileSwitch ( varholder , holdreflect , templateName , node . ElseList ) + "}"
}
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 ) {
2017-11-08 00:12:50 +00:00
c . log ( "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 :
2017-11-08 00:12:50 +00:00
c . log ( "Field Node:" , n . Ident )
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. 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 {
2017-11-08 00:12:50 +00:00
c . log ( "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 ( )
}
2017-11-08 00:12:50 +00:00
c . log ( "Data Kind:" , cur . Kind ( ) . String ( ) )
c . log ( "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 {
c . log ( "Data Kind:" , cur . Kind ( ) . String ( ) )
c . log ( "Field Bit:" , id )
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 ( ) )
if ! c . superDebug {
c . error ( "cur.Kind():" , cur . Kind ( ) . String ( ) )
}
c . error ( "" )
if ! multiline {
panic ( varholder + varbit + "^\n" + "Invalid value. Maybe, it doesn't exist?" )
} else {
panic ( varbit + "^\n" + "Invalid value. Maybe, it doesn't exist?" )
2017-09-10 16:57:22 +00:00
}
2017-11-23 05:37:08 +00:00
}
2017-09-10 16:57:22 +00:00
2017-11-23 05:37:08 +00:00
c . log ( "in-loop varbit: " + varbit )
if cur . Kind ( ) == reflect . Map {
cur = cur . MapIndex ( reflect . ValueOf ( id ) )
varbit += "[\"" + id + "\"]"
cur = skipPointers ( cur , id )
if cur . Kind ( ) == r eflect . 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 ++
}
varbit = "disp" + dispStr + " " + newVarByte + "= " + varholder + varbit + "\n"
varholder = "disp" + dispStr
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
}
2017-11-23 05:37:08 +00:00
c . log ( "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 :
2017-11-08 00:12:50 +00:00
c . log ( "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 :
2017-11-08 00:12:50 +00:00
c . log ( "Variable Node:" , n . String ( ) )
c . log ( 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 :
2017-11-08 00:12:50 +00:00
c . log ( "Identifier Node:" , node )
c . log ( "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 ) {
2017-11-08 00:12:50 +00:00
c . log ( "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 :
2017-11-11 04:06:16 +00:00
if c . 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 :
2017-11-08 00:12:50 +00:00
c . log ( "Chain Node:" , n . Node )
c . log ( "Chain Node Args:" , node . Args )
2017-09-03 04:50:31 +00:00
case * parse . IdentifierNode :
2017-11-08 00:12:50 +00:00
c . log ( "Identifier Node:" , node )
c . log ( "Identifier Node Args:" , node . Args )
return c . compileIdentSwitchN ( varholder , holdreflect , templateName , node )
2017-09-03 04:50:31 +00:00
case * parse . DotNode :
return varholder
case * parse . VariableNode :
2017-11-08 00:12:50 +00:00
c . log ( "Variable Node:" , n . String ( ) )
c . log ( "Variable 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 :
2017-11-08 00:12:50 +00:00
c . log ( "Pipe Node!" )
c . log ( n )
c . log ( "Args:" , node . Args )
out += c . compileIdentSwitchN ( varholder , holdreflect , templateName , node )
2017-06-16 10:41:30 +00:00
2017-11-08 00:12:50 +00:00
c . log ( "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 ) {
c . log ( "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 ) {
c . log ( "symbol: " , symbol )
c . log ( "node.Args[pos + 1]" , node . Args [ pos + 1 ] )
c . log ( "node.Args[pos + 2]" , node . Args [ pos + 2 ] )
}
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 ) {
2017-11-29 02:34:02 +00:00
c . logf ( "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
2017-11-08 00:12:50 +00:00
c . log ( "Left operand:" , node . Args [ pos - 1 ] )
c . log ( "Right operand:" , node . Args [ pos + 1 ] )
2017-11-07 22:38:15 +00:00
if ! funcExists {
pos ++
}
2017-11-08 00:12:50 +00:00
c . log ( "pos:" , pos )
c . log ( "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 ) {
2017-11-08 00:12:50 +00:00
c . log ( "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 ]
2017-11-08 00:12:50 +00:00
c . log ( "pos:" , pos )
c . log ( "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 )
if val3 . IsValid ( ) {
val = val3
} else {
panic ( "val3 is invalid" )
}
// TODO: Refactor this
out = "w.Write([]byte(common.BuildWidget(" + leftParam + "," + rightParam + ")))\n"
literal = true
break ArgLoop
2017-09-03 04:50:31 +00:00
default :
2017-11-08 00:12:50 +00:00
c . log ( "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 ) {
c . log ( "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 :
2017-11-11 04:06:16 +00:00
if c . 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 :
2017-11-08 00:12:50 +00:00
c . log ( "Chain Node:" , n . Node )
c . log ( "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 ) {
2017-11-08 00:12:50 +00:00
c . log ( "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 ) {
2017-11-08 00:12:50 +00:00
c . log ( "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
2017-11-08 00:12:50 +00:00
c . log ( "Cur Kind:" , cur . Kind ( ) )
c . log ( "Cur Type:" , cur . Type ( ) . Name ( ) )
2016-12-16 10:37:42 +00:00
for _ , bit := range bits {
2017-11-08 00:12:50 +00:00
c . log ( "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 {
2017-11-08 00:12:50 +00:00
c . log ( "Looping over pointer" )
2017-08-06 15:22:18 +00:00
for cur . Kind ( ) == reflect . Ptr {
cur = cur . Elem ( )
}
2017-11-08 00:12:50 +00:00
c . log ( "Data Kind:" , cur . Kind ( ) . String ( ) )
c . log ( "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?" )
}
2017-11-08 00:12:50 +00:00
c . log ( "Data Kind:" , cur . Kind ( ) )
c . log ( "Data Type:" , cur . Type ( ) . Name ( ) )
2016-12-16 10:37:42 +00:00
}
2017-06-16 10:41:30 +00:00
2017-11-08 00:12:50 +00:00
c . log ( "Out Value:" , out )
c . log ( "Out Kind:" , cur . Kind ( ) )
c . log ( "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
2017-11-08 00:12:50 +00:00
c . log ( "Out Value:" , out )
c . log ( "Out Kind:" , cur . Kind ( ) )
c . log ( "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 {
2017-11-08 00:12:50 +00:00
c . log ( "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 ) {
2017-11-08 00:12:50 +00:00
c . log ( "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
2017-11-23 05:37:08 +00:00
c . log ( "varname: " , varname )
c . log ( "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
}
2017-11-23 05:37:08 +00:00
c . log ( "out: " , out )
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 ) {
2017-11-08 00:12:50 +00:00
c . log ( "in compileSubtemplate" )
2017-11-29 02:34:02 +00:00
c . log ( "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 :
2017-11-29 02:34:02 +00:00
c . log ( "Unknown Node: " , firstWord )
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?
2016-12-16 10:37:42 +00:00
res , err := ioutil . ReadFile ( c . dir + node . Name )
if err != nil {
log . Fatal ( err )
}
2017-06-16 10:41:30 +00:00
2016-12-16 10:37:42 +00:00
content := string ( res )
2017-11-11 04:06:16 +00:00
if c . 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
2016-12-16 10:37:42 +00:00
c . tlist [ fname ] = tree
subtree := c . tlist [ fname ]
2017-11-08 00:12:50 +00:00
c . log ( "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 }
2017-01-17 07:55:46 +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 )
2017-06-16 10:41:30 +00:00
return out
2016-12-16 10:37:42 +00:00
}
2017-11-08 00:12:50 +00:00
func ( c * CTemplateSet ) log ( args ... interface { } ) {
2017-11-11 04:06:16 +00:00
if c . superDebug {
2017-11-08 00:12:50 +00:00
fmt . Println ( args ... )
}
}
2017-11-29 02:34:02 +00:00
func ( c * CTemplateSet ) logf ( left string , args ... interface { } ) {
if c . superDebug {
fmt . Printf ( left , args ... )
}
}
2017-11-23 05:37:08 +00:00
func ( c * CTemplateSet ) error ( args ... interface { } ) {
if c . debug {
fmt . Println ( args ... )
}
}
2017-09-10 16:57:22 +00:00
// TODO: Write unit tests for this
2017-02-11 14:51:16 +00:00
func minify ( data string ) string {
2017-09-03 04:50:31 +00:00
data = strings . Replace ( data , "\t" , "" , - 1 )
data = strings . Replace ( data , "\v" , "" , - 1 )
data = strings . Replace ( data , "\n" , "" , - 1 )
data = strings . Replace ( data , "\r" , "" , - 1 )
data = strings . Replace ( data , " " , " " , - 1 )
2017-02-11 14:51:16 +00:00
return data
}
2017-07-29 10:36:39 +00:00
2017-09-10 16:57:22 +00:00
// TODO: Strip comments
// TODO: Handle CSS nested in <style> tags?
// TODO: Write unit tests for this
2017-09-03 04:50:31 +00:00
func minifyHTML ( data string ) string {
2017-07-29 10:36:39 +00:00
return minify ( data )
}
2017-09-10 16:57:22 +00:00
// TODO: Have static files use this
// TODO: Strip comments
// TODO: Convert the rgb()s to hex codes?
// TODO: Write unit tests for this
2017-09-03 04:50:31 +00:00
func minifyCSS ( data string ) string {
2017-07-29 10:36:39 +00:00
return minify ( data )
}
2017-09-10 16:57:22 +00:00
// TODO: Convert this to three character hex strings whenever possible?
// TODO: Write unit tests for this
2017-09-03 04:50:31 +00:00
// nolint
func rgbToHexstr ( red int , green int , blue int ) string {
2017-07-29 10:36:39 +00:00
return strconv . FormatInt ( int64 ( red ) , 16 ) + strconv . FormatInt ( int64 ( green ) , 16 ) + strconv . FormatInt ( int64 ( blue ) , 16 )
}
/ *
2017-09-10 16:57:22 +00:00
// TODO: Write unit tests for this
2017-09-03 04:50:31 +00:00
func hexstrToRgb ( hexstr string ) ( red int , blue int , green int , err error ) {
2017-07-29 10:36:39 +00:00
// Strip the # at the start
if hexstr [ 0 ] == '#' {
hexstr = strings . TrimPrefix ( hexstr , "#" )
}
if len ( hexstr ) != 3 && len ( hexstr ) != 6 {
return 0 , 0 , 0 , errors . New ( "Hex colour codes may only be three or six characters long" )
}
if len ( hexstr ) == 3 {
hexstr = hexstr [ 0 ] + hexstr [ 0 ] + hexstr [ 1 ] + hexstr [ 1 ] + hexstr [ 2 ] + hexstr [ 2 ]
}
} * /