2017-11-10 03:33:11 +00:00
|
|
|
package common
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2017-10-14 07:39:22 +00:00
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2018-03-12 04:52:47 +00:00
|
|
|
"strconv"
|
2017-10-14 07:39:22 +00:00
|
|
|
"time"
|
2017-11-11 04:06:16 +00:00
|
|
|
|
|
|
|
"./templates"
|
2017-10-14 07:39:22 +00:00
|
|
|
)
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2017-11-11 06:33:08 +00:00
|
|
|
var Ctemplates []string
|
2017-11-11 04:06:16 +00:00
|
|
|
var Templates = template.New("")
|
|
|
|
var PrebuildTmplList []func(User, *HeaderVars) CTmpl
|
2017-11-10 03:33:11 +00:00
|
|
|
|
|
|
|
type CTmpl struct {
|
|
|
|
Name string
|
|
|
|
Filename string
|
|
|
|
Path string
|
|
|
|
StructName string
|
|
|
|
Data interface{}
|
2017-11-11 06:33:08 +00:00
|
|
|
Imports []string
|
2017-11-10 03:33:11 +00:00
|
|
|
}
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2018-03-08 03:59:47 +00:00
|
|
|
// TODO: Stop duplicating these bits of code
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2017-10-30 09:57:08 +00:00
|
|
|
func interpreted_topic_template(pi TopicPage, w http.ResponseWriter) error {
|
2017-11-11 04:06:16 +00:00
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["topic"]
|
2017-08-27 09:33:45 +00:00
|
|
|
if !ok {
|
|
|
|
mapping = "topic"
|
|
|
|
}
|
2017-11-11 04:06:16 +00:00
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2018-03-17 08:16:43 +00:00
|
|
|
var Template_topic_handle = interpreted_topic_template
|
|
|
|
var Template_topic_alt_handle = interpreted_topic_template
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2018-03-17 08:16:43 +00:00
|
|
|
var Template_topics_handle = func(pi TopicsPage, w http.ResponseWriter) error {
|
2017-11-11 04:06:16 +00:00
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["topics"]
|
2017-08-27 09:33:45 +00:00
|
|
|
if !ok {
|
|
|
|
mapping = "topics"
|
|
|
|
}
|
2017-11-11 04:06:16 +00:00
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2018-03-17 08:16:43 +00:00
|
|
|
var Template_forum_handle = func(pi ForumPage, w http.ResponseWriter) error {
|
2017-11-11 04:06:16 +00:00
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["forum"]
|
2017-08-27 09:33:45 +00:00
|
|
|
if !ok {
|
|
|
|
mapping = "forum"
|
|
|
|
}
|
2017-11-11 04:06:16 +00:00
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2018-03-17 08:16:43 +00:00
|
|
|
var Template_forums_handle = func(pi ForumsPage, w http.ResponseWriter) error {
|
2017-11-11 04:06:16 +00:00
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["forums"]
|
2017-08-27 09:33:45 +00:00
|
|
|
if !ok {
|
|
|
|
mapping = "forums"
|
|
|
|
}
|
2017-11-11 04:06:16 +00:00
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2018-03-17 08:16:43 +00:00
|
|
|
var Template_profile_handle = func(pi ProfilePage, w http.ResponseWriter) error {
|
2017-11-11 04:06:16 +00:00
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["profile"]
|
2017-08-27 09:33:45 +00:00
|
|
|
if !ok {
|
|
|
|
mapping = "profile"
|
|
|
|
}
|
2017-11-11 04:06:16 +00:00
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2018-03-17 08:16:43 +00:00
|
|
|
var Template_create_topic_handle = func(pi CreateTopicPage, w http.ResponseWriter) error {
|
2018-01-10 03:32:48 +00:00
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["create_topic"]
|
2017-08-27 09:33:45 +00:00
|
|
|
if !ok {
|
2018-01-10 03:32:48 +00:00
|
|
|
mapping = "create_topic"
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
2017-11-11 04:06:16 +00:00
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2018-03-08 03:59:47 +00:00
|
|
|
// nolint
|
2018-03-17 08:16:43 +00:00
|
|
|
var Template_login_handle = func(pi Page, w http.ResponseWriter) error {
|
2018-03-08 03:59:47 +00:00
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["login"]
|
|
|
|
if !ok {
|
|
|
|
mapping = "login"
|
|
|
|
}
|
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
|
|
|
}
|
|
|
|
|
|
|
|
// nolint
|
2018-03-17 08:16:43 +00:00
|
|
|
var Template_register_handle = func(pi Page, w http.ResponseWriter) error {
|
2018-03-08 03:59:47 +00:00
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["register"]
|
|
|
|
if !ok {
|
|
|
|
mapping = "register"
|
|
|
|
}
|
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
|
|
|
}
|
|
|
|
|
|
|
|
// nolint
|
2018-03-17 08:16:43 +00:00
|
|
|
var Template_error_handle = func(pi Page, w http.ResponseWriter) error {
|
2018-03-08 03:59:47 +00:00
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["error"]
|
|
|
|
if !ok {
|
|
|
|
mapping = "error"
|
|
|
|
}
|
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
|
|
|
}
|
|
|
|
|
|
|
|
// nolint
|
2018-03-17 08:16:43 +00:00
|
|
|
var Template_ip_search_handle = func(pi IPSearchPage, w http.ResponseWriter) error {
|
2018-03-08 03:59:47 +00:00
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["ip_search"]
|
|
|
|
if !ok {
|
|
|
|
mapping = "ip_search"
|
|
|
|
}
|
|
|
|
return Templates.ExecuteTemplate(w, mapping+".html", pi)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// ? - Add template hooks?
|
2017-09-03 04:50:31 +00:00
|
|
|
func compileTemplates() error {
|
2018-03-08 03:59:47 +00:00
|
|
|
var config tmpl.CTemplateConfig
|
|
|
|
config.Minify = Config.MinifyTemplates
|
|
|
|
config.SuperDebug = Dev.TemplateDebug
|
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
var c tmpl.CTemplateSet
|
2018-03-08 03:59:47 +00:00
|
|
|
c.SetConfig(config)
|
2017-09-10 16:57:22 +00:00
|
|
|
|
|
|
|
// Schemas to train the template compiler on what to expect
|
|
|
|
// TODO: Add support for interface{}s
|
2018-03-08 03:59:47 +00:00
|
|
|
user := User{62, BuildProfileURL("fake-user", 62), "Fake User", "compiler@localhost", 0, false, false, false, false, false, false, GuestPerms, make(map[string]bool), "", false, BuildAvatar(62, ""), "", "", "", "", 0, 0, "0.0.0.0.0", 0}
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Do a more accurate level calculation for this?
|
2018-03-08 03:59:47 +00:00
|
|
|
user2 := User{1, BuildProfileURL("admin-alice", 1), "Admin Alice", "alice@localhost", 1, true, true, true, true, false, false, AllPerms, make(map[string]bool), "", true, BuildAvatar(1, ""), "", "", "", "", 58, 1000, "127.0.0.1", 0}
|
|
|
|
user3 := User{2, BuildProfileURL("admin-fred", 62), "Admin Fred", "fred@localhost", 1, true, true, true, true, false, false, AllPerms, make(map[string]bool), "", true, BuildAvatar(2, ""), "", "", "", "", 42, 900, "::1", 0}
|
2017-09-03 04:50:31 +00:00
|
|
|
headerVars := &HeaderVars{
|
2017-11-11 04:06:16 +00:00
|
|
|
Site: Site,
|
|
|
|
Settings: SettingBox.Load().(SettingMap),
|
|
|
|
Themes: Themes,
|
2017-11-23 05:37:08 +00:00
|
|
|
Theme: Themes[DefaultThemeBox.Load().(string)],
|
2017-09-03 04:50:31 +00:00
|
|
|
NoticeList: []string{"test"},
|
2017-08-27 09:33:45 +00:00
|
|
|
Stylesheets: []string{"panel"},
|
2017-09-03 04:50:31 +00:00
|
|
|
Scripts: []string{"whatever"},
|
2017-08-27 09:33:45 +00:00
|
|
|
Widgets: PageWidgets{
|
|
|
|
LeftSidebar: template.HTML("lalala"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Print("Compiling the templates")
|
|
|
|
|
2017-11-02 02:52:21 +00:00
|
|
|
var now = time.Now()
|
2018-01-26 05:53:34 +00:00
|
|
|
poll := Poll{ID: 1, Type: 0, Options: map[int]string{0: "Nothing", 1: "Something"}, Results: map[int]int{0: 5, 1: 2}, QuickOptions: []PollOption{
|
|
|
|
PollOption{0, "Nothing"},
|
|
|
|
PollOption{1, "Something"},
|
|
|
|
}, VoteCount: 7}
|
|
|
|
topic := TopicUser{1, "blah", "Blah", "Hey there!", 0, false, false, now, RelativeTime(now), now, RelativeTime(now), 0, "", "127.0.0.1", 0, 1, "classname", poll.ID, "weird-data", BuildProfileURL("fake-user", 62), "Fake User", Config.DefaultGroup, "", 0, "", "", "", "", "", 58, false}
|
2017-09-28 22:16:34 +00:00
|
|
|
var replyList []ReplyUser
|
2017-11-11 04:06:16 +00:00
|
|
|
replyList = append(replyList, ReplyUser{0, 0, "Yo!", "Yo!", 0, "alice", "Alice", Config.DefaultGroup, now, RelativeTime(now), 0, 0, "", "", 0, "", "", "", "", 0, "127.0.0.1", false, 1, "", ""})
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
var varList = make(map[string]tmpl.VarItem)
|
2018-01-26 05:53:34 +00:00
|
|
|
tpage := TopicPage{"Title", user, headerVars, replyList, topic, poll, 1, 1}
|
2017-11-11 06:33:08 +00:00
|
|
|
topicIDTmpl, err := c.Compile("topic.html", "templates/", "common.TopicPage", tpage, varList)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-11 06:33:08 +00:00
|
|
|
topicIDAltTmpl, err := c.Compile("topic_alt.html", "templates/", "common.TopicPage", tpage, varList)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
varList = make(map[string]tmpl.VarItem)
|
2017-09-03 04:50:31 +00:00
|
|
|
ppage := ProfilePage{"User 526", user, headerVars, replyList, user}
|
2017-11-11 06:33:08 +00:00
|
|
|
profileTmpl, err := c.Compile("profile.html", "templates/", "common.ProfilePage", ppage, varList)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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: Use a dummy forum list to avoid o(n) problems
|
2017-08-27 09:33:45 +00:00
|
|
|
var forumList []Forum
|
2017-11-23 05:37:08 +00:00
|
|
|
forums, err := Forums.GetAll()
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, forum := range forums {
|
2017-09-10 16:57:22 +00:00
|
|
|
forumList = append(forumList, *forum)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
2017-11-11 04:06:16 +00:00
|
|
|
varList = make(map[string]tmpl.VarItem)
|
2017-09-10 16:57:22 +00:00
|
|
|
forumsPage := ForumsPage{"Forum List", user, headerVars, forumList}
|
2017-11-11 06:33:08 +00:00
|
|
|
forumsTmpl, err := c.Compile("forums.html", "templates/", "common.ForumsPage", forumsPage, varList)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var topicsList []*TopicsRow
|
2017-10-14 07:39:22 +00:00
|
|
|
topicsList = append(topicsList, &TopicsRow{1, "topic-title", "Topic Title", "The topic content.", 1, false, false, "Date", time.Now(), "Date", user3.ID, 1, "", "127.0.0.1", 0, 1, "classname", "", &user2, "", 0, &user3, "General", "/forum/general.2"})
|
2018-01-20 06:50:29 +00:00
|
|
|
topicsPage := TopicsPage{"Topic List", user, headerVars, topicsList, forumList, Config.DefaultForum, []int{1}, 1, 1}
|
2017-11-11 06:33:08 +00:00
|
|
|
topicsTmpl, err := c.Compile("topics.html", "templates/", "common.TopicsPage", topicsPage, varList)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
//var topicList []TopicUser
|
|
|
|
//topicList = append(topicList,TopicUser{1,"topic-title","Topic Title","The topic content.",1,false,false,"Date","Date",1,"","127.0.0.1",0,1,"classname","","admin-fred","Admin Fred",config.DefaultGroup,"",0,"","","","",58,false})
|
2017-11-11 05:22:33 +00:00
|
|
|
forumItem := BlankForum(1, "general-forum.1", "General Forum", "Where the general stuff happens", true, "all", 0, "", 0)
|
2018-02-15 13:15:27 +00:00
|
|
|
forumPage := ForumPage{"General Forum", user, headerVars, topicsList, forumItem, []int{1}, 1, 1}
|
2017-11-11 06:33:08 +00:00
|
|
|
forumTmpl, err := c.Compile("forum.html", "templates/", "common.ForumPage", forumPage, varList)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-08 03:59:47 +00:00
|
|
|
loginPage := Page{"Login Page", user, headerVars, tList, nil}
|
|
|
|
loginTmpl, err := c.Compile("login.html", "templates/", "common.Page", loginPage, varList)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
registerPage := Page{"Registration Page", user, headerVars, tList, nil}
|
|
|
|
registerTmpl, err := c.Compile("register.html", "templates/", "common.Page", registerPage, varList)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-11-11 05:22:33 +00:00
|
|
|
}
|
2017-11-11 06:33:08 +00:00
|
|
|
|
2018-03-08 03:59:47 +00:00
|
|
|
errorPage := Page{"Error", user, headerVars, tList, "A problem has occurred in the system."}
|
|
|
|
errorTmpl, err := c.Compile("error.html", "templates/", "common.Page", errorPage, varList)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ipUserList = make(map[int]*User)
|
|
|
|
ipUserList[1] = &user2
|
|
|
|
ipSearchPage := IPSearchPage{"IP Search", user2, headerVars, ipUserList, "::1"}
|
|
|
|
ipSearchTmpl, err := c.Compile("ip_search.html", "templates/", "common.IPSearchPage", ipSearchPage, varList)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let plugins register their own templates
|
|
|
|
DebugLog("Registering the templates for the plugins")
|
|
|
|
config = c.GetConfig()
|
|
|
|
config.SkipHandles = true
|
|
|
|
c.SetConfig(config)
|
2017-11-11 04:06:16 +00:00
|
|
|
for _, tmplfunc := range PrebuildTmplList {
|
|
|
|
tmplItem := tmplfunc(user, headerVars)
|
|
|
|
varList = make(map[string]tmpl.VarItem)
|
2017-11-11 06:33:08 +00:00
|
|
|
compiledTmpl, err := c.Compile(tmplItem.Filename, tmplItem.Path, tmplItem.StructName, tmplItem.Data, varList, tmplItem.Imports...)
|
2017-11-10 03:33:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-11 04:06:16 +00:00
|
|
|
go writeTemplate(tmplItem.Name, compiledTmpl)
|
2017-11-10 03:33:11 +00:00
|
|
|
}
|
|
|
|
|
2017-08-27 09:33:45 +00:00
|
|
|
log.Print("Writing the templates")
|
2017-09-10 16:57:22 +00:00
|
|
|
go writeTemplate("topic", topicIDTmpl)
|
|
|
|
go writeTemplate("topic_alt", topicIDAltTmpl)
|
|
|
|
go writeTemplate("profile", profileTmpl)
|
|
|
|
go writeTemplate("forums", forumsTmpl)
|
|
|
|
go writeTemplate("topics", topicsTmpl)
|
|
|
|
go writeTemplate("forum", forumTmpl)
|
2018-03-08 03:59:47 +00:00
|
|
|
go writeTemplate("login", loginTmpl)
|
|
|
|
go writeTemplate("register", registerTmpl)
|
|
|
|
go writeTemplate("ip_search", ipSearchTmpl)
|
|
|
|
go writeTemplate("error", errorTmpl)
|
2017-08-27 09:33:45 +00:00
|
|
|
go func() {
|
2018-03-12 04:52:47 +00:00
|
|
|
out := "package main\n\n"
|
|
|
|
for templateName, count := range c.TemplateFragmentCount {
|
|
|
|
out += "var " + templateName + "_frags = make([][]byte," + strconv.Itoa(count) + ")\n"
|
|
|
|
}
|
|
|
|
out += "\n// nolint\nfunc init() {\n" + c.FragOut + "}\n"
|
|
|
|
err := writeFile("./template_list.go", out)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func writeTemplate(name string, content string) {
|
|
|
|
err := writeFile("./template_"+name+".go", content)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-11 05:22:33 +00:00
|
|
|
func InitTemplates() error {
|
2017-11-11 04:06:16 +00:00
|
|
|
if Dev.DebugMode {
|
2017-08-27 09:33:45 +00:00
|
|
|
log.Print("Initialising the template system")
|
|
|
|
}
|
2017-11-11 05:22:33 +00:00
|
|
|
err := compileTemplates()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Add support for 64-bit integers
|
|
|
|
// TODO: Add support for floats
|
2017-08-27 09:33:45 +00:00
|
|
|
fmap := make(map[string]interface{})
|
2017-09-03 04:50:31 +00:00
|
|
|
fmap["add"] = func(left interface{}, right interface{}) interface{} {
|
2017-09-10 16:57:22 +00:00
|
|
|
var leftInt, rightInt int
|
2017-08-27 09:33:45 +00:00
|
|
|
switch left := left.(type) {
|
2017-09-03 04:50:31 +00:00
|
|
|
case uint, uint8, uint16, int, int32:
|
2017-09-10 16:57:22 +00:00
|
|
|
leftInt = left.(int)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
switch right := right.(type) {
|
2017-09-03 04:50:31 +00:00
|
|
|
case uint, uint8, uint16, int, int32:
|
2017-09-10 16:57:22 +00:00
|
|
|
rightInt = right.(int)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
2017-09-10 16:57:22 +00:00
|
|
|
return leftInt + rightInt
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
fmap["subtract"] = func(left interface{}, right interface{}) interface{} {
|
2017-09-10 16:57:22 +00:00
|
|
|
var leftInt, rightInt int
|
2017-08-27 09:33:45 +00:00
|
|
|
switch left := left.(type) {
|
2017-09-03 04:50:31 +00:00
|
|
|
case uint, uint8, uint16, int, int32:
|
2017-09-10 16:57:22 +00:00
|
|
|
leftInt = left.(int)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
switch right := right.(type) {
|
2017-09-03 04:50:31 +00:00
|
|
|
case uint, uint8, uint16, int, int32:
|
2017-09-10 16:57:22 +00:00
|
|
|
rightInt = right.(int)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
2017-09-10 16:57:22 +00:00
|
|
|
return leftInt - rightInt
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
fmap["multiply"] = func(left interface{}, right interface{}) interface{} {
|
2017-09-10 16:57:22 +00:00
|
|
|
var leftInt, rightInt int
|
2017-08-27 09:33:45 +00:00
|
|
|
switch left := left.(type) {
|
2017-09-03 04:50:31 +00:00
|
|
|
case uint, uint8, uint16, int, int32:
|
2017-09-10 16:57:22 +00:00
|
|
|
leftInt = left.(int)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
switch right := right.(type) {
|
2017-09-03 04:50:31 +00:00
|
|
|
case uint, uint8, uint16, int, int32:
|
2017-09-10 16:57:22 +00:00
|
|
|
rightInt = right.(int)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
2017-09-10 16:57:22 +00:00
|
|
|
return leftInt * rightInt
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
fmap["divide"] = func(left interface{}, right interface{}) interface{} {
|
2017-09-10 16:57:22 +00:00
|
|
|
var leftInt, rightInt int
|
2017-08-27 09:33:45 +00:00
|
|
|
switch left := left.(type) {
|
2017-09-03 04:50:31 +00:00
|
|
|
case uint, uint8, uint16, int, int32:
|
2017-09-10 16:57:22 +00:00
|
|
|
leftInt = left.(int)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
switch right := right.(type) {
|
2017-09-03 04:50:31 +00:00
|
|
|
case uint, uint8, uint16, int, int32:
|
2017-09-10 16:57:22 +00:00
|
|
|
rightInt = right.(int)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
2017-09-10 16:57:22 +00:00
|
|
|
if leftInt == 0 || rightInt == 0 {
|
2017-08-27 09:33:45 +00:00
|
|
|
return 0
|
|
|
|
}
|
2017-09-10 16:57:22 +00:00
|
|
|
return leftInt / rightInt
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 02:34:02 +00:00
|
|
|
fmap["dock"] = func(dock interface{}, headerVarInt interface{}) interface{} {
|
|
|
|
return template.HTML(BuildWidget(dock.(string), headerVarInt.(*HeaderVars)))
|
|
|
|
}
|
|
|
|
|
2018-03-08 03:59:47 +00:00
|
|
|
fmap["lang"] = func(phraseNameInt interface{}) interface{} {
|
|
|
|
phraseName, ok := phraseNameInt.(string)
|
|
|
|
if !ok {
|
|
|
|
panic("phraseNameInt is not a string")
|
|
|
|
}
|
2018-03-11 09:33:49 +00:00
|
|
|
return GetTmplPhrase(phraseName) // TODO: Log non-existent phrases?
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
2018-03-08 03:59:47 +00:00
|
|
|
|
|
|
|
// The interpreted templates...
|
|
|
|
DebugLog("Loading the template files...")
|
2017-11-11 04:06:16 +00:00
|
|
|
Templates.Funcs(fmap)
|
|
|
|
template.Must(Templates.ParseGlob("templates/*"))
|
|
|
|
template.Must(Templates.ParseGlob("pages/*"))
|
2017-11-11 05:22:33 +00:00
|
|
|
|
|
|
|
return nil
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|