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"
|
2018-04-22 12:33:56 +00:00
|
|
|
"io"
|
2017-10-14 07:39:22 +00:00
|
|
|
"log"
|
2018-03-21 05:56:33 +00:00
|
|
|
"path/filepath"
|
2018-03-12 04:52:47 +00:00
|
|
|
"strconv"
|
2018-03-21 05:56:33 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2017-10-14 07:39:22 +00:00
|
|
|
"time"
|
2017-11-11 04:06:16 +00:00
|
|
|
|
2018-10-27 03:21:02 +00:00
|
|
|
"github.com/Azareal/Gosora/common/alerts"
|
2018-11-01 06:43:56 +00:00
|
|
|
"github.com/Azareal/Gosora/common/phrases"
|
2018-11-17 02:36:02 +00:00
|
|
|
"github.com/Azareal/Gosora/common/templates"
|
2017-10-14 07:39:22 +00:00
|
|
|
)
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2018-12-14 04:08:53 +00:00
|
|
|
var Ctemplates []string // TODO: Use this to filter out top level templates we don't need
|
2019-02-10 05:52:26 +00:00
|
|
|
var DefaultTemplates = template.New("")
|
|
|
|
var DefaultTemplateFuncMap map[string]interface{}
|
|
|
|
|
|
|
|
//var Templates = template.New("")
|
2018-04-22 12:33:56 +00:00
|
|
|
var PrebuildTmplList []func(User, *Header) CTmpl
|
2017-11-10 03:33:11 +00:00
|
|
|
|
2018-12-14 04:08:53 +00:00
|
|
|
func skipCTmpl(key string) bool {
|
|
|
|
for _, tmpl := range Ctemplates {
|
|
|
|
if strings.HasSuffix(key, "/"+tmpl+".html") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-02-10 05:52:26 +00:00
|
|
|
func genIntTmpl(name string) func(pi interface{}, w io.Writer) error {
|
|
|
|
return func(pi interface{}, w io.Writer) error {
|
|
|
|
mapping, ok := Themes[DefaultThemeBox.Load().(string)].TemplatesMap[name]
|
|
|
|
if !ok {
|
|
|
|
mapping = name
|
|
|
|
}
|
|
|
|
return DefaultTemplates.ExecuteTemplate(w, mapping+".html", pi)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-10 05:52:26 +00:00
|
|
|
// TODO: Refactor the template trees to not need these
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2019-02-10 05:52:26 +00:00
|
|
|
var Template_topic_handle = genIntTmpl("topic")
|
|
|
|
var Template_topic_guest_handle = Template_topic_handle
|
|
|
|
var Template_topic_member_handle = Template_topic_handle
|
|
|
|
var Template_topic_alt_handle = genIntTmpl("topic")
|
|
|
|
var Template_topic_alt_guest_handle = Template_topic_alt_handle
|
|
|
|
var Template_topic_alt_member_handle = Template_topic_alt_handle
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2019-02-10 05:52:26 +00:00
|
|
|
var Template_topics_handle = genIntTmpl("topics")
|
2018-11-28 21:46:53 +00:00
|
|
|
var Template_topics_guest_handle = Template_topics_handle
|
|
|
|
var Template_topics_member_handle = Template_topics_handle
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2019-02-10 05:52:26 +00:00
|
|
|
var Template_forum_handle = genIntTmpl("forum")
|
2018-12-14 04:08:53 +00:00
|
|
|
var Template_forum_guest_handle = Template_forum_handle
|
|
|
|
var Template_forum_member_handle = Template_forum_handle
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2019-02-10 05:52:26 +00:00
|
|
|
var Template_forums_handle = genIntTmpl("forums")
|
2018-12-17 04:58:55 +00:00
|
|
|
var Template_forums_guest_handle = Template_forums_handle
|
|
|
|
var Template_forums_member_handle = Template_forums_handle
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2019-02-10 05:52:26 +00:00
|
|
|
var Template_profile_handle = genIntTmpl("profile")
|
2018-12-14 04:08:53 +00:00
|
|
|
var Template_profile_guest_handle = Template_profile_handle
|
|
|
|
var Template_profile_member_handle = Template_profile_handle
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// nolint
|
2019-02-10 05:52:26 +00:00
|
|
|
var Template_create_topic_handle = genIntTmpl("create_topic")
|
|
|
|
var Template_login_handle = genIntTmpl("login")
|
|
|
|
var Template_register_handle = genIntTmpl("register")
|
|
|
|
var Template_error_handle = genIntTmpl("error")
|
|
|
|
var Template_ip_search_handle = genIntTmpl("ip_search")
|
|
|
|
var Template_account_handle = genIntTmpl("account")
|
2018-10-27 03:21:02 +00:00
|
|
|
|
2018-06-24 13:49:29 +00:00
|
|
|
func tmplInitUsers() (User, User, User) {
|
2018-07-28 12:52:23 +00:00
|
|
|
avatar, microAvatar := BuildAvatar(62, "")
|
2019-04-02 07:43:11 +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, "", avatar, microAvatar, "", "", "", "", 0, 0, 0, "0.0.0.0.0", "", 0}
|
2018-07-28 12:52:23 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Do a more accurate level calculation for this?
|
2018-07-28 12:52:23 +00:00
|
|
|
avatar, microAvatar = BuildAvatar(1, "")
|
2019-04-02 07:43:11 +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, "", avatar, microAvatar, "", "", "", "", 58, 1000, 0, "127.0.0.1", "", 0}
|
2018-07-28 12:52:23 +00:00
|
|
|
|
|
|
|
avatar, microAvatar = BuildAvatar(2, "")
|
2019-04-02 07:43:11 +00:00
|
|
|
user3 := User{2, BuildProfileURL("admin-fred", 62), "Admin Fred", "fred@localhost", 1, true, true, true, true, false, false, AllPerms, make(map[string]bool), "", true, "", avatar, microAvatar, "", "", "", "", 42, 900, 0, "::1", "", 0}
|
2018-06-24 13:49:29 +00:00
|
|
|
return user, user2, user3
|
|
|
|
}
|
|
|
|
|
|
|
|
func tmplInitHeaders(user User, user2 User, user3 User) (*Header, *Header, *Header) {
|
2018-04-22 12:33:56 +00:00
|
|
|
header := &Header{
|
2019-02-28 07:28:17 +00:00
|
|
|
Site: Site,
|
|
|
|
Settings: SettingBox.Load().(SettingMap),
|
|
|
|
Themes: Themes,
|
|
|
|
Theme: Themes[DefaultThemeBox.Load().(string)],
|
|
|
|
CurrentUser: user,
|
|
|
|
NoticeList: []string{"test"},
|
2019-04-02 07:43:11 +00:00
|
|
|
Stylesheets: []string{"panel.css"},
|
|
|
|
Scripts: []string{"whatever.js"},
|
|
|
|
PreScriptsAsync: []string{"whatever.js"},
|
|
|
|
ScriptsAsync: []string{"whatever.js"},
|
2017-08-27 09:33:45 +00:00
|
|
|
Widgets: PageWidgets{
|
|
|
|
LeftSidebar: template.HTML("lalala"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-11-17 02:36:02 +00:00
|
|
|
buildHeader := func(user User) *Header {
|
|
|
|
var head = &Header{Site: Site}
|
|
|
|
*head = *header
|
|
|
|
head.CurrentUser = user
|
|
|
|
return head
|
|
|
|
}
|
2018-05-27 09:36:35 +00:00
|
|
|
|
2018-11-17 02:36:02 +00:00
|
|
|
return header, buildHeader(user2), buildHeader(user3)
|
2018-06-24 13:49:29 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 05:08:10 +00:00
|
|
|
type TmplLoggedin struct {
|
|
|
|
Stub string
|
|
|
|
Guest string
|
|
|
|
Member string
|
|
|
|
}
|
|
|
|
|
2018-12-17 04:58:55 +00:00
|
|
|
type nobreak interface{}
|
|
|
|
|
2019-02-10 05:52:26 +00:00
|
|
|
type TItem struct {
|
|
|
|
Expects string
|
|
|
|
ExpectsInt interface{}
|
|
|
|
LoggedIn bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type TItemHold map[string]TItem
|
|
|
|
|
|
|
|
func (hold TItemHold) Add(name string, expects string, expectsInt interface{}) {
|
|
|
|
hold[name] = TItem{expects, expectsInt, true}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hold TItemHold) AddStd(name string, expects string, expectsInt interface{}) {
|
|
|
|
hold[name] = TItem{expects, expectsInt, false}
|
|
|
|
}
|
|
|
|
|
2018-06-24 13:49:29 +00:00
|
|
|
// ? - Add template hooks?
|
|
|
|
func CompileTemplates() error {
|
2019-02-10 05:52:26 +00:00
|
|
|
log.Print("Compiling the templates")
|
|
|
|
// TODO: Implement per-theme template overrides here too
|
|
|
|
var overriden = make(map[string]map[string]bool)
|
|
|
|
for _, theme := range Themes {
|
|
|
|
overriden[theme.Name] = make(map[string]bool)
|
|
|
|
log.Printf("theme.OverridenTemplates: %+v\n", theme.OverridenTemplates)
|
|
|
|
for _, override := range theme.OverridenTemplates {
|
|
|
|
overriden[theme.Name][override] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Printf("overriden: %+v\n", overriden)
|
|
|
|
|
2018-06-24 13:49:29 +00:00
|
|
|
var config tmpl.CTemplateConfig
|
|
|
|
config.Minify = Config.MinifyTemplates
|
|
|
|
config.Debug = Dev.DebugMode
|
|
|
|
config.SuperDebug = Dev.TemplateDebug
|
|
|
|
|
2019-02-28 07:28:17 +00:00
|
|
|
c := tmpl.NewCTemplateSet("normal")
|
2018-06-24 13:49:29 +00:00
|
|
|
c.SetConfig(config)
|
|
|
|
c.SetBaseImportMap(map[string]string{
|
2018-10-27 03:21:02 +00:00
|
|
|
"io": "io",
|
|
|
|
"github.com/Azareal/Gosora/common": "github.com/Azareal/Gosora/common",
|
2018-06-24 13:49:29 +00:00
|
|
|
})
|
|
|
|
c.SetBuildTags("!no_templategen")
|
2019-02-10 05:52:26 +00:00
|
|
|
c.SetOverrideTrack(overriden)
|
|
|
|
c.SetPerThemeTmpls(make(map[string]bool))
|
2018-06-24 13:49:29 +00:00
|
|
|
|
2019-02-10 05:52:26 +00:00
|
|
|
log.Print("Compiling the default templates")
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
err := compileTemplates(&wg, c, "")
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-10 05:52:26 +00:00
|
|
|
oroots := c.GetOverridenRoots()
|
|
|
|
log.Printf("oroots: %+v\n", oroots)
|
2018-11-26 05:08:10 +00:00
|
|
|
|
2019-02-10 05:52:26 +00:00
|
|
|
log.Print("Compiling the per-theme templates")
|
|
|
|
for theme, tmpls := range oroots {
|
2019-02-28 07:28:17 +00:00
|
|
|
c.ResetLogs("normal-" + theme)
|
2019-02-10 05:52:26 +00:00
|
|
|
c.SetThemeName(theme)
|
|
|
|
c.SetPerThemeTmpls(tmpls)
|
|
|
|
log.Print("theme: ", theme)
|
|
|
|
log.Printf("perThemeTmpls: %+v\n", tmpls)
|
|
|
|
err = compileTemplates(&wg, c, theme)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
2019-02-10 05:52:26 +00:00
|
|
|
writeTemplateList(c, &wg, "./")
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2019-02-10 05:52:26 +00:00
|
|
|
func compileCommons(c *tmpl.CTemplateSet, header *Header, header2 *Header, out TItemHold) error {
|
|
|
|
// TODO: Add support for interface{}s
|
|
|
|
_, user2, user3 := tmplInitUsers()
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
// Convienience function to save a line here and there
|
|
|
|
var htitle = func(name string) *Header {
|
|
|
|
header.Title = name
|
|
|
|
return header
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
2019-02-10 05:52:26 +00:00
|
|
|
/*var htitle2 = func(name string) *Header {
|
|
|
|
header2.Title = name
|
|
|
|
return header2
|
|
|
|
}*/
|
2017-08-27 09:33:45 +00:00
|
|
|
|
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
|
|
|
}
|
2018-09-24 07:52:06 +00:00
|
|
|
|
2017-08-27 09:33:45 +00:00
|
|
|
var topicsList []*TopicsRow
|
2018-12-27 05:42:41 +00:00
|
|
|
topicsList = append(topicsList, &TopicsRow{1, "topic-title", "Topic Title", "The topic content.", 1, false, false, now, now, user3.ID, 1, 1, "", "127.0.0.1", 1, 0, 1, 1, 0, "classname", "", &user2, "", 0, &user3, "General", "/forum/general.2"})
|
2019-02-10 05:52:26 +00:00
|
|
|
topicListPage := TopicListPage{htitle("Topic List"), topicsList, forumList, Config.DefaultForum, TopicListSort{"lastupdated", false}, Paginator{[]int{1}, 1, 1}}
|
|
|
|
out.Add("topics", "common.TopicListPage", topicListPage)
|
2017-08-27 09:33:45 +00:00
|
|
|
|
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)
|
2019-02-10 05:52:26 +00:00
|
|
|
forumPage := ForumPage{htitle("General Forum"), topicsList, forumItem, Paginator{[]int{1}, 1, 1}}
|
|
|
|
out.Add("forum", "common.ForumPage", forumPage)
|
|
|
|
out.Add("forums", "common.ForumsPage", ForumsPage{htitle("Forum List"), forumList})
|
2017-08-27 09:33:45 +00:00
|
|
|
|
2019-02-10 05:52:26 +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}
|
|
|
|
avatar, microAvatar := BuildAvatar(62, "")
|
|
|
|
miniAttach := []*MiniAttachment{&MiniAttachment{Path: "/"}}
|
|
|
|
topic := TopicUser{1, "blah", "Blah", "Hey there!", 0, false, false, now, now, 1, 1, 0, "", "127.0.0.1", 1, 0, 1, 0, "classname", poll.ID, "weird-data", BuildProfileURL("fake-user", 62), "Fake User", Config.DefaultGroup, avatar, microAvatar, 0, "", "", "", "", "", 58, false, miniAttach}
|
|
|
|
var replyList []ReplyUser
|
|
|
|
replyList = append(replyList, ReplyUser{0, 0, "Yo!", "Yo!", 0, "alice", "Alice", Config.DefaultGroup, now, 0, 0, avatar, microAvatar, "", 0, "", "", "", "", 0, "127.0.0.1", false, 1, 1, "", "", miniAttach})
|
|
|
|
tpage := TopicPage{htitle("Topic Name"), replyList, topic, &Forum{ID: 1, Name: "Hahaha"}, poll, Paginator{[]int{1}, 1, 1}}
|
|
|
|
tpage.Forum.Link = BuildForumURL(NameToSlug(tpage.Forum.Name), tpage.Forum.ID)
|
|
|
|
out.Add("topic", "common.TopicPage", tpage)
|
|
|
|
out.Add("topic_alt", "common.TopicPage", tpage)
|
|
|
|
return nil
|
|
|
|
}
|
2018-03-08 03:59:47 +00:00
|
|
|
|
2019-02-10 05:52:26 +00:00
|
|
|
func compileTemplates(wg *sync.WaitGroup, c *tmpl.CTemplateSet, themeName string) error {
|
|
|
|
// Schemas to train the template compiler on what to expect
|
|
|
|
// TODO: Add support for interface{}s
|
|
|
|
user, user2, user3 := tmplInitUsers()
|
|
|
|
header, header2, _ := tmplInitHeaders(user, user2, user3)
|
|
|
|
now := time.Now()
|
2017-11-11 06:33:08 +00:00
|
|
|
|
2019-02-10 05:52:26 +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}*/
|
|
|
|
avatar, microAvatar := BuildAvatar(62, "")
|
|
|
|
miniAttach := []*MiniAttachment{&MiniAttachment{Path: "/"}}
|
|
|
|
var replyList []ReplyUser
|
|
|
|
//topic := TopicUser{1, "blah", "Blah", "Hey there!", 0, false, false, now, now, 1, 1, 0, "", "127.0.0.1", 1, 0, 1, 0, "classname", poll.ID, "weird-data", BuildProfileURL("fake-user", 62), "Fake User", Config.DefaultGroup, avatar, microAvatar, 0, "", "", "", "", "", 58, false, miniAttach}
|
|
|
|
// TODO: Do we want the UID on this to be 0?
|
|
|
|
avatar, microAvatar = BuildAvatar(0, "")
|
|
|
|
replyList = append(replyList, ReplyUser{0, 0, "Yo!", "Yo!", 0, "alice", "Alice", Config.DefaultGroup, now, 0, 0, avatar, microAvatar, "", 0, "", "", "", "", 0, "127.0.0.1", false, 1, 1, "", "", miniAttach})
|
2018-03-08 03:59:47 +00:00
|
|
|
|
2019-02-10 05:52:26 +00:00
|
|
|
// Convienience function to save a line here and there
|
|
|
|
var htitle = func(name string) *Header {
|
|
|
|
header.Title = name
|
|
|
|
return header
|
|
|
|
}
|
|
|
|
tmpls := TItemHold(make(map[string]TItem))
|
|
|
|
err := compileCommons(c, header, header2, tmpls)
|
2018-03-08 03:59:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-10 05:52:26 +00:00
|
|
|
ppage := ProfilePage{htitle("User 526"), replyList, user, 0, 0} // TODO: Use the score from user to generate the currentScore and nextScore
|
|
|
|
tmpls.Add("profile", "common.ProfilePage", ppage)
|
|
|
|
|
|
|
|
tmpls.AddStd("login", "common.Page", Page{htitle("Login Page"), tList, nil})
|
|
|
|
tmpls.AddStd("register", "common.Page", Page{htitle("Registration Page"), tList, "nananana"})
|
|
|
|
tmpls.AddStd("error", "common.ErrorPage", ErrorPage{htitle("Error"), "A problem has occurred in the system."})
|
|
|
|
|
|
|
|
ipSearchPage := IPSearchPage{htitle("IP Search"), map[int]*User{1: &user2}, "::1"}
|
|
|
|
tmpls.AddStd("ip_search", "common.IPSearchPage", ipSearchPage)
|
|
|
|
|
2018-12-17 04:58:55 +00:00
|
|
|
var inter nobreak
|
|
|
|
accountPage := Account{header, "dashboard", "account_own_edit", inter}
|
2019-02-10 05:52:26 +00:00
|
|
|
tmpls.AddStd("account", "common.Account", accountPage)
|
2018-10-27 03:21:02 +00:00
|
|
|
|
2018-11-26 05:08:10 +00:00
|
|
|
var writeTemplate = func(name string, content interface{}) {
|
2018-03-21 05:56:33 +00:00
|
|
|
log.Print("Writing template '" + name + "'")
|
2018-11-26 05:08:10 +00:00
|
|
|
var writeTmpl = func(name string, content string) {
|
|
|
|
if content == "" {
|
2019-02-10 05:52:26 +00:00
|
|
|
return //log.Fatal("No content body for " + name)
|
2018-11-26 05:08:10 +00:00
|
|
|
}
|
2018-03-21 05:56:33 +00:00
|
|
|
err := writeFile("./template_"+name+".go", content)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-11-26 05:08:10 +00:00
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2019-02-10 05:52:26 +00:00
|
|
|
tname := themeName
|
|
|
|
if tname != "" {
|
|
|
|
tname = "_" + tname
|
|
|
|
}
|
2018-11-26 05:08:10 +00:00
|
|
|
switch content := content.(type) {
|
|
|
|
case string:
|
2019-02-10 05:52:26 +00:00
|
|
|
writeTmpl(name+tname, content)
|
2018-11-26 05:08:10 +00:00
|
|
|
case TmplLoggedin:
|
2019-02-10 05:52:26 +00:00
|
|
|
writeTmpl(name+tname, content.Stub)
|
|
|
|
writeTmpl(name+tname+"_guest", content.Guest)
|
|
|
|
writeTmpl(name+tname+"_member", content.Member)
|
2018-11-26 05:08:10 +00:00
|
|
|
}
|
2018-03-21 05:56:33 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2018-03-08 03:59:47 +00:00
|
|
|
// Let plugins register their own templates
|
|
|
|
DebugLog("Registering the templates for the plugins")
|
2019-02-10 05:52:26 +00:00
|
|
|
config := c.GetConfig()
|
2018-03-08 03:59:47 +00:00
|
|
|
config.SkipHandles = true
|
|
|
|
c.SetConfig(config)
|
2017-11-11 04:06:16 +00:00
|
|
|
for _, tmplfunc := range PrebuildTmplList {
|
2018-04-22 12:33:56 +00:00
|
|
|
tmplItem := tmplfunc(user, header)
|
2019-02-10 05:52:26 +00:00
|
|
|
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
|
|
|
|
}
|
2018-03-21 05:56:33 +00:00
|
|
|
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")
|
2019-02-10 05:52:26 +00:00
|
|
|
for name, titem := range tmpls {
|
|
|
|
log.Print("Writing " + name)
|
|
|
|
varList := make(map[string]tmpl.VarItem)
|
|
|
|
if titem.LoggedIn {
|
|
|
|
stub, guest, member, err := c.CompileByLoggedin(name+".html", "templates/", titem.Expects, titem.ExpectsInt, varList)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
writeTemplate(name, TmplLoggedin{stub, guest, member})
|
|
|
|
} else {
|
|
|
|
tmpl, err := c.Compile(name+".html", "templates/", titem.Expects, titem.ExpectsInt, varList)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
writeTemplate(name, tmpl)
|
|
|
|
}
|
|
|
|
}
|
2019-02-23 06:29:19 +00:00
|
|
|
/*writeTemplate("login", loginTmpl)
|
2018-03-21 05:56:33 +00:00
|
|
|
writeTemplate("register", registerTmpl)
|
|
|
|
writeTemplate("ip_search", ipSearchTmpl)
|
2019-02-10 05:52:26 +00:00
|
|
|
writeTemplate("error", errorTmpl)*/
|
2018-04-22 12:33:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-28 07:28:17 +00:00
|
|
|
// ? - Add template hooks?
|
2018-04-22 12:33:56 +00:00
|
|
|
func CompileJSTemplates() error {
|
2019-02-28 07:28:17 +00:00
|
|
|
log.Print("Compiling the JS templates")
|
|
|
|
// TODO: Implement per-theme template overrides here too
|
|
|
|
var overriden = make(map[string]map[string]bool)
|
|
|
|
for _, theme := range Themes {
|
|
|
|
overriden[theme.Name] = make(map[string]bool)
|
|
|
|
log.Printf("theme.OverridenTemplates: %+v\n", theme.OverridenTemplates)
|
|
|
|
for _, override := range theme.OverridenTemplates {
|
|
|
|
overriden[theme.Name][override] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Printf("overriden: %+v\n", overriden)
|
|
|
|
|
|
|
|
var config tmpl.CTemplateConfig
|
|
|
|
config.Minify = Config.MinifyTemplates
|
|
|
|
config.Debug = Dev.DebugMode
|
|
|
|
config.SuperDebug = Dev.TemplateDebug
|
|
|
|
config.SkipHandles = true
|
|
|
|
config.SkipTmplPtrMap = true
|
|
|
|
config.SkipInitBlock = false
|
|
|
|
config.PackageName = "tmpl"
|
|
|
|
|
|
|
|
c := tmpl.NewCTemplateSet("js")
|
|
|
|
c.SetConfig(config)
|
|
|
|
c.SetBuildTags("!no_templategen")
|
|
|
|
c.SetOverrideTrack(overriden)
|
|
|
|
c.SetPerThemeTmpls(make(map[string]bool))
|
|
|
|
|
|
|
|
log.Print("Compiling the default templates")
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
err := compileJSTemplates(&wg, c, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
oroots := c.GetOverridenRoots()
|
|
|
|
log.Printf("oroots: %+v\n", oroots)
|
|
|
|
|
|
|
|
log.Print("Compiling the per-theme templates")
|
|
|
|
for theme, tmpls := range oroots {
|
|
|
|
c.SetThemeName(theme)
|
|
|
|
c.SetPerThemeTmpls(tmpls)
|
|
|
|
log.Print("theme: ", theme)
|
|
|
|
log.Printf("perThemeTmpls: %+v\n", tmpls)
|
|
|
|
err = compileJSTemplates(&wg, c, theme)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var dirPrefix = "./tmpl_client/"
|
|
|
|
writeTemplateList(c, &wg, dirPrefix)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func compileJSTemplates(wg *sync.WaitGroup, c *tmpl.CTemplateSet, themeName string) error {
|
|
|
|
user, user2, user3 := tmplInitUsers()
|
|
|
|
header, _, _ := tmplInitHeaders(user, user2, user3)
|
|
|
|
now := time.Now()
|
|
|
|
var varList = make(map[string]tmpl.VarItem)
|
|
|
|
|
|
|
|
c.SetBaseImportMap(map[string]string{
|
|
|
|
"io": "io",
|
|
|
|
"github.com/Azareal/Gosora/common/alerts": "github.com/Azareal/Gosora/common/alerts",
|
|
|
|
})
|
|
|
|
|
|
|
|
// TODO: Check what sort of path is sent exactly and use it here
|
|
|
|
alertItem := alerts.AlertItem{Avatar: "", ASID: 1, Path: "/", Message: "uh oh, something happened"}
|
|
|
|
alertTmpl, err := c.Compile("alert.html", "templates/", "alerts.AlertItem", alertItem, varList)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.SetBaseImportMap(map[string]string{
|
|
|
|
"io": "io",
|
|
|
|
"github.com/Azareal/Gosora/common": "github.com/Azareal/Gosora/common",
|
|
|
|
})
|
|
|
|
// TODO: Fix the import loop so we don't have to use this hack anymore
|
|
|
|
c.SetBuildTags("!no_templategen,tmplgentopic")
|
|
|
|
|
|
|
|
tmpls := TItemHold(make(map[string]TItem))
|
|
|
|
|
|
|
|
var topicsRow = &TopicsRow{1, "topic-title", "Topic Title", "The topic content.", 1, false, false, now, now, user3.ID, 1, 1, "", "127.0.0.1", 1, 0, 1, 0, 1, "classname", "", &user2, "", 0, &user3, "General", "/forum/general.2"}
|
|
|
|
tmpls.AddStd("topics_topic", "common.TopicsRow", topicsRow)
|
|
|
|
|
|
|
|
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}
|
|
|
|
avatar, microAvatar := BuildAvatar(62, "")
|
|
|
|
miniAttach := []*MiniAttachment{&MiniAttachment{Path: "/"}}
|
|
|
|
topic := TopicUser{1, "blah", "Blah", "Hey there!", 62, false, false, now, now, 1, 1, 0, "", "127.0.0.1", 1, 0, 1, 0, "classname", poll.ID, "weird-data", BuildProfileURL("fake-user", 62), "Fake User", Config.DefaultGroup, avatar, microAvatar, 0, "", "", "", "", "", 58, false, miniAttach}
|
|
|
|
var replyList []ReplyUser
|
|
|
|
// TODO: Do we really want the UID here to be zero?
|
|
|
|
avatar, microAvatar = BuildAvatar(0, "")
|
|
|
|
replyList = append(replyList, ReplyUser{0, 0, "Yo!", "Yo!", 0, "alice", "Alice", Config.DefaultGroup, now, 0, 0, avatar, microAvatar, "", 0, "", "", "", "", 0, "127.0.0.1", false, 1, 1, "", "", miniAttach})
|
|
|
|
|
|
|
|
varList = make(map[string]tmpl.VarItem)
|
|
|
|
header.Title = "Topic Name"
|
|
|
|
tpage := TopicPage{header, replyList, topic, &Forum{ID: 1, Name: "Hahaha"}, poll, Paginator{[]int{1}, 1, 1}}
|
|
|
|
tpage.Forum.Link = BuildForumURL(NameToSlug(tpage.Forum.Name), tpage.Forum.ID)
|
|
|
|
tmpls.AddStd("topic_posts", "common.TopicPage", tpage)
|
|
|
|
tmpls.AddStd("topic_alt_posts", "common.TopicPage", tpage)
|
|
|
|
|
|
|
|
itemsPerPage := 25
|
|
|
|
_, page, lastPage := PageOffset(20, 1, itemsPerPage)
|
|
|
|
pageList := Paginate(20, itemsPerPage, 5)
|
|
|
|
tmpls.AddStd("paginator", "common.Paginator", Paginator{pageList, page, lastPage})
|
|
|
|
|
2019-04-11 05:46:57 +00:00
|
|
|
tmpls.AddStd("topic_c_edit_post", "common.TopicCEditPost", TopicCEditPost{Source: "", Ref: ""})
|
|
|
|
|
2019-02-28 07:28:17 +00:00
|
|
|
var dirPrefix = "./tmpl_client/"
|
|
|
|
var writeTemplate = func(name string, content string) {
|
|
|
|
log.Print("Writing template '" + name + "'")
|
|
|
|
if content == "" {
|
|
|
|
return //log.Fatal("No content body")
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
tname := themeName
|
|
|
|
if tname != "" {
|
|
|
|
tname = "_" + tname
|
|
|
|
}
|
|
|
|
err := writeFile(dirPrefix+"template_"+name+tname+".go", content)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Print("Writing the templates")
|
|
|
|
for name, titem := range tmpls {
|
|
|
|
log.Print("Writing " + name)
|
|
|
|
varList := make(map[string]tmpl.VarItem)
|
|
|
|
tmpl, err := c.Compile(name+".html", "templates/", titem.Expects, titem.ExpectsInt, varList)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
writeTemplate(name, tmpl)
|
|
|
|
}
|
|
|
|
writeTemplate("alert", alertTmpl)
|
|
|
|
/*//writeTemplate("forum", forumTmpl)
|
|
|
|
writeTemplate("topic_posts", topicPostsTmpl)
|
|
|
|
writeTemplate("topic_alt_posts", topicAltPostsTmpl)
|
|
|
|
writeTemplateList(c, &wg, dirPrefix)*/
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-10 05:52:26 +00:00
|
|
|
func getTemplateList(c *tmpl.CTemplateSet, wg *sync.WaitGroup, prefix string) string {
|
2019-02-28 07:28:17 +00:00
|
|
|
DebugLog("in getTemplateList")
|
2019-02-10 05:52:26 +00:00
|
|
|
pout := "\n// nolint\nfunc init() {\n"
|
|
|
|
var tFragCount = make(map[string]int)
|
|
|
|
var bodyMap = make(map[string]string) //map[body]fragmentPrefix
|
|
|
|
//var tmplMap = make(map[string]map[string]string) // map[tmpl]map[body]fragmentPrefix
|
|
|
|
var tmpCount = 0
|
|
|
|
for _, frag := range c.FragOut {
|
|
|
|
front := frag.TmplName + "_frags[" + strconv.Itoa(frag.Index) + "]"
|
2019-02-28 07:28:17 +00:00
|
|
|
DebugLog("front: ", front)
|
|
|
|
DebugLog("frag.Body: ", frag.Body)
|
2019-02-10 05:52:26 +00:00
|
|
|
/*bodyMap, tok := tmplMap[frag.TmplName]
|
|
|
|
if !tok {
|
|
|
|
tmplMap[frag.TmplName] = make(map[string]string)
|
|
|
|
bodyMap = tmplMap[frag.TmplName]
|
|
|
|
}*/
|
|
|
|
fp, ok := bodyMap[frag.Body]
|
|
|
|
if !ok {
|
|
|
|
bodyMap[frag.Body] = front
|
|
|
|
var bits string
|
2019-02-28 07:28:17 +00:00
|
|
|
DebugLog("encoding frag.Body")
|
2019-02-10 05:52:26 +00:00
|
|
|
for _, char := range []byte(frag.Body) {
|
|
|
|
if char == '\'' {
|
|
|
|
bits += "'\\" + string(char) + "',"
|
2019-02-28 07:28:17 +00:00
|
|
|
} else if char < 32 {
|
|
|
|
bits += strconv.Itoa(int(char)) + ","
|
2019-02-10 05:52:26 +00:00
|
|
|
} else {
|
|
|
|
bits += "'" + string(char) + "',"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tmpStr := strconv.Itoa(tmpCount)
|
|
|
|
pout += "arr_" + tmpStr + " := [...]byte{" + bits + "}\n"
|
|
|
|
pout += front + " = arr_" + tmpStr + "[:]\n"
|
|
|
|
tmpCount++
|
|
|
|
//pout += front + " = []byte(`" + frag.Body + "`)\n"
|
|
|
|
} else {
|
2019-02-28 07:28:17 +00:00
|
|
|
DebugLog("encoding cached index " + fp)
|
2019-02-10 05:52:26 +00:00
|
|
|
pout += front + " = " + fp + "\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok = tFragCount[frag.TmplName]
|
|
|
|
if !ok {
|
|
|
|
tFragCount[frag.TmplName] = 0
|
|
|
|
}
|
|
|
|
tFragCount[frag.TmplName]++
|
|
|
|
}
|
|
|
|
|
|
|
|
out := "package " + c.GetConfig().PackageName + "\n\n"
|
|
|
|
var getterstr = "\n// nolint\nGetFrag = func(name string) [][]byte {\nswitch(name) {\n"
|
|
|
|
for templateName, count := range tFragCount {
|
|
|
|
out += "var " + templateName + "_frags = make([][]byte," + strconv.Itoa(count) + ")\n"
|
|
|
|
getterstr += "\tcase \"" + templateName + "\":\n"
|
|
|
|
getterstr += "\treturn " + templateName + "_frags\n"
|
|
|
|
}
|
|
|
|
getterstr += "}\nreturn nil\n}\n"
|
|
|
|
out += pout + "\n" + getterstr + "}\n"
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2018-04-22 12:33:56 +00:00
|
|
|
func writeTemplateList(c *tmpl.CTemplateSet, wg *sync.WaitGroup, prefix string) {
|
|
|
|
log.Print("Writing template list")
|
2018-03-21 05:56:33 +00:00
|
|
|
wg.Add(1)
|
2017-08-27 09:33:45 +00:00
|
|
|
go func() {
|
2019-02-10 05:52:26 +00:00
|
|
|
err := writeFile(prefix+"template_list.go", getTemplateList(c, wg, prefix))
|
2017-08-27 09:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-03-21 05:56:33 +00:00
|
|
|
wg.Done()
|
2017-08-27 09:33:45 +00:00
|
|
|
}()
|
2018-03-21 05:56:33 +00:00
|
|
|
wg.Wait()
|
2018-04-22 12:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func arithToInt64(in interface{}) (out int64) {
|
|
|
|
switch in := in.(type) {
|
|
|
|
case int64:
|
|
|
|
out = in
|
|
|
|
case int32:
|
|
|
|
out = int64(in)
|
|
|
|
case int:
|
|
|
|
out = int64(in)
|
|
|
|
case uint32:
|
|
|
|
out = int64(in)
|
|
|
|
case uint16:
|
|
|
|
out = int64(in)
|
|
|
|
case uint8:
|
|
|
|
out = int64(in)
|
|
|
|
case uint:
|
|
|
|
out = int64(in)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func arithDuoToInt64(left interface{}, right interface{}) (leftInt int64, rightInt int64) {
|
|
|
|
return arithToInt64(left), arithToInt64(right)
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 05:52:26 +00:00
|
|
|
func initDefaultTmplFuncMap() {
|
2017-09-10 16:57:22 +00:00
|
|
|
// 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{} {
|
2018-04-22 12:33:56 +00:00
|
|
|
leftInt, rightInt := arithDuoToInt64(left, right)
|
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{} {
|
2018-04-22 12:33:56 +00:00
|
|
|
leftInt, rightInt := arithDuoToInt64(left, right)
|
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{} {
|
2018-04-22 12:33:56 +00:00
|
|
|
leftInt, rightInt := arithDuoToInt64(left, right)
|
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{} {
|
2018-04-22 12:33:56 +00:00
|
|
|
leftInt, rightInt := arithDuoToInt64(left, right)
|
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
|
|
|
}
|
|
|
|
|
2018-04-22 12:33:56 +00:00
|
|
|
fmap["dock"] = func(dock interface{}, headerInt interface{}) interface{} {
|
|
|
|
return template.HTML(BuildWidget(dock.(string), headerInt.(*Header)))
|
2017-11-29 02:34:02 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 09:31:13 +00:00
|
|
|
fmap["hasWidgets"] = func(dock interface{}, headerInt interface{}) interface{} {
|
|
|
|
return HasWidgets(dock.(string), headerInt.(*Header))
|
|
|
|
}
|
|
|
|
|
2018-11-17 02:36:02 +00:00
|
|
|
fmap["elapsed"] = func(startedAtInt interface{}) interface{} {
|
|
|
|
return time.Since(startedAtInt.(time.Time)).String()
|
|
|
|
}
|
|
|
|
|
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-06-17 07:28:18 +00:00
|
|
|
// TODO: Log non-existent phrases?
|
2018-11-01 06:43:56 +00:00
|
|
|
return template.HTML(phrases.GetTmplPhrase(phraseName))
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
2018-03-08 03:59:47 +00:00
|
|
|
|
2018-12-14 04:08:53 +00:00
|
|
|
// TODO: Implement this in the template generator too
|
|
|
|
fmap["langf"] = func(phraseNameInt interface{}, args ...interface{}) interface{} {
|
|
|
|
phraseName, ok := phraseNameInt.(string)
|
|
|
|
if !ok {
|
|
|
|
panic("phraseNameInt is not a string")
|
|
|
|
}
|
|
|
|
// TODO: Log non-existent phrases?
|
|
|
|
// TODO: Optimise TmplPhrasef so we don't use slow Sprintf there
|
|
|
|
return template.HTML(phrases.GetTmplPhrasef(phraseName, args...))
|
|
|
|
}
|
|
|
|
|
2018-10-10 07:33:51 +00:00
|
|
|
fmap["level"] = func(levelInt interface{}) interface{} {
|
|
|
|
level, ok := levelInt.(int)
|
|
|
|
if !ok {
|
|
|
|
panic("levelInt is not an integer")
|
|
|
|
}
|
2018-11-01 06:43:56 +00:00
|
|
|
return template.HTML(phrases.GetLevelPhrase(level))
|
2018-10-10 07:33:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 04:08:53 +00:00
|
|
|
fmap["abstime"] = func(timeInt interface{}) interface{} {
|
|
|
|
time, ok := timeInt.(time.Time)
|
|
|
|
if !ok {
|
|
|
|
panic("timeInt is not a time.Time")
|
|
|
|
}
|
|
|
|
return time.Format("2006-01-02 15:04:05")
|
|
|
|
}
|
|
|
|
|
2018-12-27 05:42:41 +00:00
|
|
|
fmap["reltime"] = func(timeInt interface{}) interface{} {
|
|
|
|
time, ok := timeInt.(time.Time)
|
|
|
|
if !ok {
|
|
|
|
panic("timeInt is not a time.Time")
|
|
|
|
}
|
|
|
|
return RelativeTime(time)
|
|
|
|
}
|
|
|
|
|
2018-03-31 05:25:27 +00:00
|
|
|
fmap["scope"] = func(name interface{}) interface{} {
|
|
|
|
return ""
|
2018-07-13 11:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmap["dyntmpl"] = func(nameInt interface{}, pageInt interface{}, headerInt interface{}) interface{} {
|
|
|
|
header := headerInt.(*Header)
|
2018-12-08 00:45:27 +00:00
|
|
|
err := header.Theme.RunTmpl(nameInt.(string), pageInt, header.Writer)
|
2018-07-13 11:27:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ""
|
2018-03-31 05:25:27 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 05:52:26 +00:00
|
|
|
DefaultTemplateFuncMap = fmap
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadTemplates(tmpls *template.Template, themeName string) error {
|
|
|
|
tmpls.Funcs(DefaultTemplateFuncMap)
|
2018-03-21 05:56:33 +00:00
|
|
|
templateFiles, err := filepath.Glob("templates/*.html")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var templateFileMap = make(map[string]int)
|
|
|
|
for index, path := range templateFiles {
|
|
|
|
path = strings.Replace(path, "\\", "/", -1)
|
|
|
|
log.Print("templateFile: ", path)
|
2018-12-14 04:08:53 +00:00
|
|
|
if skipCTmpl(path) {
|
|
|
|
log.Print("skipping")
|
|
|
|
continue
|
|
|
|
}
|
2018-03-21 05:56:33 +00:00
|
|
|
templateFileMap[path] = index
|
|
|
|
}
|
|
|
|
|
|
|
|
overrideFiles, err := filepath.Glob("templates/overrides/*.html")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, path := range overrideFiles {
|
|
|
|
path = strings.Replace(path, "\\", "/", -1)
|
|
|
|
log.Print("overrideFile: ", path)
|
2018-12-14 04:08:53 +00:00
|
|
|
if skipCTmpl(path) {
|
|
|
|
log.Print("skipping")
|
|
|
|
continue
|
|
|
|
}
|
2018-03-21 05:56:33 +00:00
|
|
|
index, ok := templateFileMap["templates/"+strings.TrimPrefix(path, "templates/overrides/")]
|
|
|
|
if !ok {
|
|
|
|
log.Print("not ok: templates/" + strings.TrimPrefix(path, "templates/overrides/"))
|
|
|
|
templateFiles = append(templateFiles, path)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
templateFiles[index] = path
|
|
|
|
}
|
2017-11-11 05:22:33 +00:00
|
|
|
|
2019-02-10 05:52:26 +00:00
|
|
|
if themeName != "" {
|
|
|
|
overrideFiles, err := filepath.Glob("./themes/" + themeName + "/overrides/*.html")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, path := range overrideFiles {
|
|
|
|
path = strings.Replace(path, "\\", "/", -1)
|
|
|
|
log.Print("overrideFile: ", path)
|
|
|
|
if skipCTmpl(path) {
|
|
|
|
log.Print("skipping")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
index, ok := templateFileMap["templates/"+strings.TrimPrefix(path, "themes/"+themeName+"/overrides/")]
|
|
|
|
if !ok {
|
|
|
|
log.Print("not ok: templates/" + strings.TrimPrefix(path, "themes/"+themeName+"/overrides/"))
|
|
|
|
templateFiles = append(templateFiles, path)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
templateFiles[index] = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template.Must(tmpls.ParseFiles(templateFiles...))
|
|
|
|
template.Must(tmpls.ParseGlob("pages/*"))
|
2017-11-11 05:22:33 +00:00
|
|
|
return nil
|
2017-08-27 09:33:45 +00:00
|
|
|
}
|
2019-02-10 05:52:26 +00:00
|
|
|
|
|
|
|
func InitTemplates() error {
|
|
|
|
DebugLog("Initialising the template system")
|
|
|
|
initDefaultTmplFuncMap()
|
|
|
|
|
|
|
|
// The interpreted templates...
|
|
|
|
DebugLog("Loading the template files...")
|
|
|
|
return loadTemplates(DefaultTemplates, "")
|
|
|
|
}
|