Let plugins define imports to custom templates.

Things should work now.
This commit is contained in:
Azareal 2017-11-11 06:33:08 +00:00
parent d2a8fb1267
commit fb8fa10570
10 changed files with 199 additions and 63 deletions

View File

@ -9,6 +9,7 @@ import (
"./templates"
)
var Ctemplates []string
var Templates = template.New("")
var PrebuildTmplList []func(User, *HeaderVars) CTmpl
@ -18,6 +19,7 @@ type CTmpl struct {
Path string
StructName string
Data interface{}
Imports []string
}
// nolint
@ -30,11 +32,11 @@ func interpreted_topic_template(pi TopicPage, w http.ResponseWriter) error {
}
// nolint
var template_topic_handle func(TopicPage, http.ResponseWriter) error = interpreted_topic_template
var template_topic_alt_handle func(TopicPage, http.ResponseWriter) error = interpreted_topic_template
var Template_topic_handle func(TopicPage, http.ResponseWriter) error = interpreted_topic_template
var Template_topic_alt_handle func(TopicPage, http.ResponseWriter) error = interpreted_topic_template
// nolint
var template_topics_handle func(TopicsPage, http.ResponseWriter) error = func(pi TopicsPage, w http.ResponseWriter) error {
var Template_topics_handle func(TopicsPage, http.ResponseWriter) error = func(pi TopicsPage, w http.ResponseWriter) error {
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["topics"]
if !ok {
mapping = "topics"
@ -43,7 +45,7 @@ var template_topics_handle func(TopicsPage, http.ResponseWriter) error = func(pi
}
// nolint
var template_forum_handle func(ForumPage, http.ResponseWriter) error = func(pi ForumPage, w http.ResponseWriter) error {
var Template_forum_handle func(ForumPage, http.ResponseWriter) error = func(pi ForumPage, w http.ResponseWriter) error {
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["forum"]
if !ok {
mapping = "forum"
@ -52,7 +54,7 @@ var template_forum_handle func(ForumPage, http.ResponseWriter) error = func(pi F
}
// nolint
var template_forums_handle func(ForumsPage, http.ResponseWriter) error = func(pi ForumsPage, w http.ResponseWriter) error {
var Template_forums_handle func(ForumsPage, http.ResponseWriter) error = func(pi ForumsPage, w http.ResponseWriter) error {
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["forums"]
if !ok {
mapping = "forums"
@ -61,7 +63,7 @@ var template_forums_handle func(ForumsPage, http.ResponseWriter) error = func(pi
}
// nolint
var template_profile_handle func(ProfilePage, http.ResponseWriter) error = func(pi ProfilePage, w http.ResponseWriter) error {
var Template_profile_handle func(ProfilePage, http.ResponseWriter) error = func(pi ProfilePage, w http.ResponseWriter) error {
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["profile"]
if !ok {
mapping = "profile"
@ -70,7 +72,7 @@ var template_profile_handle func(ProfilePage, http.ResponseWriter) error = func(
}
// nolint
var template_create_topic_handle func(CreateTopicPage, http.ResponseWriter) error = func(pi CreateTopicPage, w http.ResponseWriter) error {
var Template_create_topic_handle func(CreateTopicPage, http.ResponseWriter) error = func(pi CreateTopicPage, w http.ResponseWriter) error {
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap["create-topic"]
if !ok {
mapping = "create-topic"
@ -112,18 +114,18 @@ func compileTemplates() error {
var varList = make(map[string]tmpl.VarItem)
tpage := TopicPage{"Title", user, headerVars, replyList, topic, 1, 1}
topicIDTmpl, err := c.Compile("topic.html", "templates/", "TopicPage", tpage, varList)
topicIDTmpl, err := c.Compile("topic.html", "templates/", "common.TopicPage", tpage, varList)
if err != nil {
return err
}
topicIDAltTmpl, err := c.Compile("topic_alt.html", "templates/", "TopicPage", tpage, varList)
topicIDAltTmpl, err := c.Compile("topic_alt.html", "templates/", "common.TopicPage", tpage, varList)
if err != nil {
return err
}
varList = make(map[string]tmpl.VarItem)
ppage := ProfilePage{"User 526", user, headerVars, replyList, user}
profileTmpl, err := c.Compile("profile.html", "templates/", "ProfilePage", ppage, varList)
profileTmpl, err := c.Compile("profile.html", "templates/", "common.ProfilePage", ppage, varList)
if err != nil {
return err
}
@ -141,7 +143,7 @@ func compileTemplates() error {
}
varList = make(map[string]tmpl.VarItem)
forumsPage := ForumsPage{"Forum List", user, headerVars, forumList}
forumsTmpl, err := c.Compile("forums.html", "templates/", "ForumsPage", forumsPage, varList)
forumsTmpl, err := c.Compile("forums.html", "templates/", "common.ForumsPage", forumsPage, varList)
if err != nil {
return err
}
@ -149,7 +151,7 @@ func compileTemplates() error {
var topicsList []*TopicsRow
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"})
topicsPage := TopicsPage{"Topic List", user, headerVars, topicsList, forumList, Config.DefaultForum}
topicsTmpl, err := c.Compile("topics.html", "templates/", "TopicsPage", topicsPage, varList)
topicsTmpl, err := c.Compile("topics.html", "templates/", "common.TopicsPage", topicsPage, varList)
if err != nil {
return err
}
@ -158,7 +160,7 @@ func compileTemplates() error {
//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})
forumItem := BlankForum(1, "general-forum.1", "General Forum", "Where the general stuff happens", true, "all", 0, "", 0)
forumPage := ForumPage{"General Forum", user, headerVars, topicsList, forumItem, 1, 1}
forumTmpl, err := c.Compile("forum.html", "templates/", "ForumPage", forumPage, varList)
forumTmpl, err := c.Compile("forum.html", "templates/", "common.ForumPage", forumPage, varList)
if err != nil {
return err
}
@ -167,10 +169,12 @@ func compileTemplates() error {
if Dev.DebugMode {
log.Print("Registering the templates for the plugins")
}
c.SkipHandles(true)
for _, tmplfunc := range PrebuildTmplList {
tmplItem := tmplfunc(user, headerVars)
varList = make(map[string]tmpl.VarItem)
compiledTmpl, err := c.Compile(tmplItem.Filename, tmplItem.Path, tmplItem.StructName, tmplItem.Data, varList)
compiledTmpl, err := c.Compile(tmplItem.Filename, tmplItem.Path, tmplItem.StructName, tmplItem.Data, varList, tmplItem.Imports...)
if err != nil {
return err
}

View File

@ -14,7 +14,6 @@ import (
)
// TODO: Turn this file into a library
var cTemplates []string
var textOverlapList = make(map[string]int)
// nolint
@ -49,6 +48,7 @@ type CTemplateSet struct {
minify bool
debug bool
superDebug bool
skipHandles bool
expectsInt interface{}
}
@ -64,7 +64,11 @@ func (c *CTemplateSet) SuperDebug(on bool) {
c.superDebug = on
}
func (c *CTemplateSet) Compile(name string, dir string, expects string, expectsInt interface{}, varList map[string]VarItem) (out string, err error) {
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) {
if c.debug {
fmt.Println("Compiling template '" + name + "'")
}
@ -91,6 +95,12 @@ func (c *CTemplateSet) Compile(name string, dir string, expects string, expectsI
"net/http": "net/http",
"./common": "./common",
}
if len(imports) > 0 {
for _, importItem := range imports {
c.importMap[importItem] = importItem
}
}
c.varList = varList
//c.pVarList = ""
//c.pVarPosition = 0
@ -147,9 +157,19 @@ func (c *CTemplateSet) Compile(name string, dir string, expects string, expectsI
}
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"
fout += "package main\n" + importList + c.pVarList + "\n"
fout += "// nolint\nfunc init() {\n\ttemplate_" + fname + "_handle = template_" + fname + "\n\t//o_template_" + fname + "_handle = template_" + fname + "\n\tctemplates = append(ctemplates,\"" + fname + "\")\n\tTmplPtrMap[\"" + fname + "\"] = &template_" + fname + "_handle\n\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"
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"
fout = strings.Replace(fout, `))
w.Write([]byte(`, " + ", -1)

View File

@ -110,7 +110,7 @@ func PrebuildTmplList(user common.User, headerVars *common.HeaderVars) common.CT
},
}
listPage := ListPage{"Guild List", user, headerVars, guildList}
return common.CTmpl{"guilds_guild_list", "guilds_guild_list.html", "templates/", "guilds.ListPage", listPage}
return common.CTmpl{"guilds_guild_list", "guilds_guild_list.html", "templates/", "guilds.ListPage", listPage, []string{"./extend/guilds/lib"}}
}
// TODO: Do this properly via the widget system

View File

@ -9,15 +9,14 @@ import "strconv"
// nolint
func init() {
template_forum_handle = template_forum
//o_template_forum_handle = template_forum
ctemplates = append(ctemplates,"forum")
TmplPtrMap["forum"] = &template_forum_handle
common.TmplPtrMap["o_forum"] = template_forum
common.Template_forum_handle = Template_forum
common.Ctemplates = append(common.Ctemplates,"forum")
common.TmplPtrMap["forum"] = &common.Template_forum_handle
common.TmplPtrMap["o_forum"] = Template_forum
}
// nolint
func template_forum(tmpl_forum_vars ForumPage, w http.ResponseWriter) error {
func Template_forum(tmpl_forum_vars common.ForumPage, w http.ResponseWriter) error {
w.Write(header_0)
w.Write([]byte(tmpl_forum_vars.Title))
w.Write(header_1)

View File

@ -3,20 +3,19 @@
// Code generated by Gosora. More below:
/* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */
package main
import "./common"
import "net/http"
import "./common"
// nolint
func init() {
template_forums_handle = template_forums
//o_template_forums_handle = template_forums
ctemplates = append(ctemplates,"forums")
TmplPtrMap["forums"] = &template_forums_handle
common.TmplPtrMap["o_forums"] = template_forums
common.Template_forums_handle = Template_forums
common.Ctemplates = append(common.Ctemplates,"forums")
common.TmplPtrMap["forums"] = &common.Template_forums_handle
common.TmplPtrMap["o_forums"] = Template_forums
}
// nolint
func template_forums(tmpl_forums_vars ForumsPage, w http.ResponseWriter) error {
func Template_forums(tmpl_forums_vars common.ForumsPage, w http.ResponseWriter) error {
w.Write(header_0)
w.Write([]byte(tmpl_forums_vars.Title))
w.Write(header_1)

View File

@ -0,0 +1,118 @@
// +build !no_templategen
// Code generated by Gosora. More below:
/* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */
package main
import "net/http"
import "./common"
import "./extend/guilds/lib"
import "strconv"
// nolint
func init() {
common.TmplPtrMap["o_guilds_guild_list"] = Template_guilds_guild_list
}
// nolint
func Template_guilds_guild_list(tmpl_guilds_guild_list_vars guilds.ListPage, w http.ResponseWriter) error {
w.Write(header_0)
w.Write([]byte(tmpl_guilds_guild_list_vars.Title))
w.Write(header_1)
w.Write([]byte(tmpl_guilds_guild_list_vars.Header.Site.Name))
w.Write(header_2)
w.Write([]byte(tmpl_guilds_guild_list_vars.Header.ThemeName))
w.Write(header_3)
if len(tmpl_guilds_guild_list_vars.Header.Stylesheets) != 0 {
for _, item := range tmpl_guilds_guild_list_vars.Header.Stylesheets {
w.Write(header_4)
w.Write([]byte(item))
w.Write(header_5)
}
}
w.Write(header_6)
if len(tmpl_guilds_guild_list_vars.Header.Scripts) != 0 {
for _, item := range tmpl_guilds_guild_list_vars.Header.Scripts {
w.Write(header_7)
w.Write([]byte(item))
w.Write(header_8)
}
}
w.Write(header_9)
w.Write([]byte(tmpl_guilds_guild_list_vars.CurrentUser.Session))
w.Write(header_10)
w.Write([]byte(tmpl_guilds_guild_list_vars.Header.Site.URL))
w.Write(header_11)
if !tmpl_guilds_guild_list_vars.CurrentUser.IsSuperMod {
w.Write(header_12)
}
w.Write(header_13)
w.Write(menu_0)
w.Write(menu_1)
w.Write([]byte(tmpl_guilds_guild_list_vars.Header.Site.ShortName))
w.Write(menu_2)
if tmpl_guilds_guild_list_vars.CurrentUser.Loggedin {
w.Write(menu_3)
w.Write([]byte(tmpl_guilds_guild_list_vars.CurrentUser.Link))
w.Write(menu_4)
w.Write([]byte(tmpl_guilds_guild_list_vars.CurrentUser.Session))
w.Write(menu_5)
} else {
w.Write(menu_6)
}
w.Write(menu_7)
w.Write(header_14)
if tmpl_guilds_guild_list_vars.Header.Widgets.RightSidebar != "" {
w.Write(header_15)
}
w.Write(header_16)
if len(tmpl_guilds_guild_list_vars.Header.NoticeList) != 0 {
for _, item := range tmpl_guilds_guild_list_vars.Header.NoticeList {
w.Write(header_17)
w.Write([]byte(item))
w.Write(header_18)
}
}
w.Write(guilds_guild_list_0)
if len(tmpl_guilds_guild_list_vars.GuildList) != 0 {
for _, item := range tmpl_guilds_guild_list_vars.GuildList {
w.Write(guilds_guild_list_1)
w.Write([]byte(item.Link))
w.Write(guilds_guild_list_2)
w.Write([]byte(item.Name))
w.Write(guilds_guild_list_3)
w.Write([]byte(item.Desc))
w.Write(guilds_guild_list_4)
w.Write([]byte(strconv.Itoa(item.MemberCount)))
w.Write(guilds_guild_list_5)
w.Write([]byte(item.LastUpdateTime))
w.Write(guilds_guild_list_6)
}
} else {
w.Write(guilds_guild_list_7)
}
w.Write(guilds_guild_list_8)
w.Write(footer_0)
if len(tmpl_guilds_guild_list_vars.Header.Themes) != 0 {
for _, item := range tmpl_guilds_guild_list_vars.Header.Themes {
if !item.HideFromThemes {
w.Write(footer_1)
w.Write([]byte(item.Name))
w.Write(footer_2)
if tmpl_guilds_guild_list_vars.Header.ThemeName == item.Name {
w.Write(footer_3)
}
w.Write(footer_4)
w.Write([]byte(item.FriendlyName))
w.Write(footer_5)
}
}
}
w.Write(footer_6)
if tmpl_guilds_guild_list_vars.Header.Widgets.RightSidebar != "" {
w.Write(footer_7)
w.Write([]byte(string(tmpl_guilds_guild_list_vars.Header.Widgets.RightSidebar)))
w.Write(footer_8)
}
w.Write(footer_9)
return nil
}

View File

@ -3,21 +3,20 @@
// Code generated by Gosora. More below:
/* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */
package main
import "./common"
import "strconv"
import "net/http"
import "./common"
// nolint
func init() {
template_profile_handle = template_profile
//o_template_profile_handle = template_profile
ctemplates = append(ctemplates,"profile")
TmplPtrMap["profile"] = &template_profile_handle
common.TmplPtrMap["o_profile"] = template_profile
common.Template_profile_handle = Template_profile
common.Ctemplates = append(common.Ctemplates,"profile")
common.TmplPtrMap["profile"] = &common.Template_profile_handle
common.TmplPtrMap["o_profile"] = Template_profile
}
// nolint
func template_profile(tmpl_profile_vars ProfilePage, w http.ResponseWriter) error {
func Template_profile(tmpl_profile_vars common.ProfilePage, w http.ResponseWriter) error {
w.Write(header_0)
w.Write([]byte(tmpl_profile_vars.Title))
w.Write(header_1)

View File

@ -9,15 +9,14 @@ import "strconv"
// nolint
func init() {
template_topic_handle = template_topic
//o_template_topic_handle = template_topic
ctemplates = append(ctemplates,"topic")
TmplPtrMap["topic"] = &template_topic_handle
common.TmplPtrMap["o_topic"] = template_topic
common.Template_topic_handle = Template_topic
common.Ctemplates = append(common.Ctemplates,"topic")
common.TmplPtrMap["topic"] = &common.Template_topic_handle
common.TmplPtrMap["o_topic"] = Template_topic
}
// nolint
func template_topic(tmpl_topic_vars TopicPage, w http.ResponseWriter) error {
func Template_topic(tmpl_topic_vars common.TopicPage, w http.ResponseWriter) error {
w.Write(header_0)
w.Write([]byte(tmpl_topic_vars.Title))
w.Write(header_1)

View File

@ -9,15 +9,14 @@ import "./common"
// nolint
func init() {
template_topic_alt_handle = template_topic_alt
//o_template_topic_alt_handle = template_topic_alt
ctemplates = append(ctemplates,"topic_alt")
TmplPtrMap["topic_alt"] = &template_topic_alt_handle
common.TmplPtrMap["o_topic_alt"] = template_topic_alt
common.Template_topic_alt_handle = Template_topic_alt
common.Ctemplates = append(common.Ctemplates,"topic_alt")
common.TmplPtrMap["topic_alt"] = &common.Template_topic_alt_handle
common.TmplPtrMap["o_topic_alt"] = Template_topic_alt
}
// nolint
func template_topic_alt(tmpl_topic_alt_vars TopicPage, w http.ResponseWriter) error {
func Template_topic_alt(tmpl_topic_alt_vars common.TopicPage, w http.ResponseWriter) error {
w.Write(header_0)
w.Write([]byte(tmpl_topic_alt_vars.Title))
w.Write(header_1)

View File

@ -3,21 +3,20 @@
// Code generated by Gosora. More below:
/* This file was automatically generated by the software. Please don't edit it as your changes may be overwritten at any moment. */
package main
import "net/http"
import "./common"
import "strconv"
import "net/http"
// nolint
func init() {
template_topics_handle = template_topics
//o_template_topics_handle = template_topics
ctemplates = append(ctemplates,"topics")
TmplPtrMap["topics"] = &template_topics_handle
common.TmplPtrMap["o_topics"] = template_topics
common.Template_topics_handle = Template_topics
common.Ctemplates = append(common.Ctemplates,"topics")
common.TmplPtrMap["topics"] = &common.Template_topics_handle
common.TmplPtrMap["o_topics"] = Template_topics
}
// nolint
func template_topics(tmpl_topics_vars TopicsPage, w http.ResponseWriter) error {
func Template_topics(tmpl_topics_vars common.TopicsPage, w http.ResponseWriter) error {
w.Write(header_0)
w.Write([]byte(tmpl_topics_vars.Title))
w.Write(header_1)