2017-08-27 09:33:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "log"
|
|
|
|
import "html/template"
|
|
|
|
import "net/http"
|
|
|
|
|
|
|
|
var templates = template.New("")
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2017-08-27 09:33:45 +00:00
|
|
|
func interpreted_topic_template(pi TopicPage, w http.ResponseWriter) {
|
2017-09-10 16:57:22 +00:00
|
|
|
mapping, ok := themes[defaultThemeBox.Load().(string)].TemplatesMap["topic"]
|
2017-08-27 09:33:45 +00:00
|
|
|
if !ok {
|
|
|
|
mapping = "topic"
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
err := templates.ExecuteTemplate(w, mapping+".html", pi)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
2017-09-03 04:50:31 +00:00
|
|
|
InternalError(err, w)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2017-09-03 04:50:31 +00:00
|
|
|
var template_topic_handle func(TopicPage, http.ResponseWriter) = interpreted_topic_template
|
|
|
|
var template_topic_alt_handle func(TopicPage, http.ResponseWriter) = interpreted_topic_template
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2017-09-03 04:50:31 +00:00
|
|
|
var template_topics_handle func(TopicsPage, http.ResponseWriter) = func(pi TopicsPage, w http.ResponseWriter) {
|
2017-09-10 16:57:22 +00:00
|
|
|
mapping, ok := themes[defaultThemeBox.Load().(string)].TemplatesMap["topics"]
|
2017-08-27 09:33:45 +00:00
|
|
|
if !ok {
|
|
|
|
mapping = "topics"
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
err := templates.ExecuteTemplate(w, mapping+".html", pi)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
2017-09-03 04:50:31 +00:00
|
|
|
InternalError(err, w)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2017-09-03 04:50:31 +00:00
|
|
|
var template_forum_handle func(ForumPage, http.ResponseWriter) = func(pi ForumPage, w http.ResponseWriter) {
|
2017-09-10 16:57:22 +00:00
|
|
|
mapping, ok := themes[defaultThemeBox.Load().(string)].TemplatesMap["forum"]
|
2017-08-27 09:33:45 +00:00
|
|
|
if !ok {
|
|
|
|
mapping = "forum"
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
err := templates.ExecuteTemplate(w, mapping+".html", pi)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
2017-09-03 04:50:31 +00:00
|
|
|
InternalError(err, w)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2017-09-03 04:50:31 +00:00
|
|
|
var template_forums_handle func(ForumsPage, http.ResponseWriter) = func(pi ForumsPage, w http.ResponseWriter) {
|
2017-09-10 16:57:22 +00:00
|
|
|
mapping, ok := themes[defaultThemeBox.Load().(string)].TemplatesMap["forums"]
|
2017-08-27 09:33:45 +00:00
|
|
|
if !ok {
|
|
|
|
mapping = "forums"
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
err := templates.ExecuteTemplate(w, mapping+".html", pi)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
2017-09-03 04:50:31 +00:00
|
|
|
InternalError(err, w)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2017-09-03 04:50:31 +00:00
|
|
|
var template_profile_handle func(ProfilePage, http.ResponseWriter) = func(pi ProfilePage, w http.ResponseWriter) {
|
2017-09-10 16:57:22 +00:00
|
|
|
mapping, ok := themes[defaultThemeBox.Load().(string)].TemplatesMap["profile"]
|
2017-08-27 09:33:45 +00:00
|
|
|
if !ok {
|
|
|
|
mapping = "profile"
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
err := templates.ExecuteTemplate(w, mapping+".html", pi)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
2017-09-03 04:50:31 +00:00
|
|
|
InternalError(err, w)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2017-09-03 04:50:31 +00:00
|
|
|
var template_create_topic_handle func(CreateTopicPage, http.ResponseWriter) = func(pi CreateTopicPage, w http.ResponseWriter) {
|
2017-09-10 16:57:22 +00:00
|
|
|
mapping, ok := themes[defaultThemeBox.Load().(string)].TemplatesMap["create-topic"]
|
2017-08-27 09:33:45 +00:00
|
|
|
if !ok {
|
|
|
|
mapping = "create-topic"
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
err := templates.ExecuteTemplate(w, mapping+".html", pi)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
2017-09-03 04:50:31 +00:00
|
|
|
InternalError(err, w)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
func compileTemplates() error {
|
2017-08-27 09:33:45 +00:00
|
|
|
var c CTemplateSet
|
2017-09-10 16:57:22 +00:00
|
|
|
|
|
|
|
// Schemas to train the template compiler on what to expect
|
|
|
|
// TODO: Add support for interface{}s
|
2017-09-03 04:50:31 +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, "", "", "", "", "", 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?
|
2017-09-03 04:50:31 +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, "", "", "", "", "", 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, "", "", "", "", "", 42, 900, "::1", 0}
|
|
|
|
headerVars := &HeaderVars{
|
|
|
|
Site: site,
|
2017-09-10 16:57:22 +00:00
|
|
|
Settings: settingBox.Load().(SettingBox),
|
|
|
|
Themes: themes,
|
|
|
|
ThemeName: 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-09-28 22:16:34 +00:00
|
|
|
topic := TopicUser{1, "blah", "Blah", "Hey there!", 0, false, false, "Date", "Date", 0, "", "127.0.0.1", 0, 1, "classname", "weird-data", buildProfileURL("fake-user", 62), "Fake User", config.DefaultGroup, "", 0, "", "", "", "", "", 58, false}
|
|
|
|
var replyList []ReplyUser
|
|
|
|
replyList = append(replyList, ReplyUser{0, 0, "Yo!", "Yo!", 0, "alice", "Alice", config.DefaultGroup, "", 0, 0, "", "", 0, "", "", "", "", 0, "127.0.0.1", false, 1, "", ""})
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
var varList = make(map[string]VarItem)
|
2017-09-03 04:50:31 +00:00
|
|
|
tpage := TopicPage{"Title", user, headerVars, replyList, topic, 1, 1}
|
2017-09-10 16:57:22 +00:00
|
|
|
topicIDTmpl, err := c.compileTemplate("topic.html", "templates/", "TopicPage", tpage, varList)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-09-10 16:57:22 +00:00
|
|
|
topicIDAltTmpl, err := c.compileTemplate("topic_alt.html", "templates/", "TopicPage", tpage, varList)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
varList = make(map[string]VarItem)
|
2017-09-03 04:50:31 +00:00
|
|
|
ppage := ProfilePage{"User 526", user, headerVars, replyList, user}
|
2017-09-10 16:57:22 +00:00
|
|
|
profileTmpl, err := c.compileTemplate("profile.html", "templates/", "ProfilePage", ppage, varList)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var forumList []Forum
|
2017-09-24 00:49:41 +00:00
|
|
|
forums, err := fstore.GetAll()
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, forum := range forums {
|
2017-09-28 22:16:34 +00:00
|
|
|
//log.Printf("*forum %+v\n", *forum)
|
2017-09-10 16:57:22 +00:00
|
|
|
forumList = append(forumList, *forum)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
varList = make(map[string]VarItem)
|
2017-09-10 16:57:22 +00:00
|
|
|
forumsPage := ForumsPage{"Forum List", user, headerVars, forumList}
|
|
|
|
forumsTmpl, err := c.compileTemplate("forums.html", "templates/", "ForumsPage", forumsPage, varList)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var topicsList []*TopicsRow
|
2017-09-03 04:50:31 +00:00
|
|
|
topicsList = append(topicsList, &TopicsRow{1, "topic-title", "Topic Title", "The topic content.", 1, false, false, "Date", "Date", user3.ID, 1, "", "127.0.0.1", 0, 1, "classname", "", &user2, "", 0, &user3, "General", "/forum/general.2"})
|
2017-09-10 16:57:22 +00:00
|
|
|
topicsPage := TopicsPage{"Topic List", user, headerVars, topicsList}
|
|
|
|
topicsTmpl, err := c.compileTemplate("topics.html", "templates/", "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-09-28 22:16:34 +00:00
|
|
|
forumItem := makeDummyForum(1, "general-forum.1", "General Forum", "Where the general stuff happens", true, "all", 0, "", 0)
|
2017-09-10 16:57:22 +00:00
|
|
|
forumPage := ForumPage{"General Forum", user, headerVars, topicsList, forumItem, 1, 1}
|
|
|
|
forumTmpl, err := c.compileTemplate("forum.html", "templates/", "ForumPage", forumPage, varList)
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2017-08-27 09:33:45 +00:00
|
|
|
go func() {
|
2017-09-03 04:50:31 +00:00
|
|
|
err := writeFile("./template_list.go", "package main\n\n// nolint\n"+c.FragOut)
|
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-09-03 04:50:31 +00:00
|
|
|
func initTemplates() {
|
2017-08-27 09:33:45 +00:00
|
|
|
if dev.DebugMode {
|
|
|
|
log.Print("Initialising the template system")
|
|
|
|
}
|
2017-09-03 04:50:31 +00:00
|
|
|
compileTemplates()
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// The interpreted templates...
|
|
|
|
if dev.DebugMode {
|
|
|
|
log.Print("Loading the template files...")
|
|
|
|
}
|
|
|
|
templates.Funcs(fmap)
|
|
|
|
template.Must(templates.ParseGlob("templates/*"))
|
|
|
|
template.Must(templates.ParseGlob("pages/*"))
|
|
|
|
}
|