forked from stevenleeg/gobb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.go
155 lines (130 loc) · 4.01 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package utils
import (
"fmt"
"go/build"
"html/template"
"io/ioutil"
"net/http"
"path"
"path/filepath"
"strings"
"time"
"github.com/russross/blackfriday"
"github.com/stevenleeg/gobb/config"
"github.com/stevenleeg/gobb/models"
)
// Returns a list of all available themes
func ListTemplates() []string {
names := []string{"default"}
static_path, _ := config.Config.GetString("gobb", "base_path")
files, _ := ioutil.ReadDir(path.Join(static_path, "templates"))
for _, f := range files {
if !f.IsDir() {
continue
}
names = append(names, f.Name())
}
return names
}
func tplAdd(first, second int) int {
return first + second
}
func tplParseMarkdown(input string) template.HTML {
byte_slice := []byte(input)
return template.HTML(string(blackfriday.MarkdownBasic(byte_slice)))
}
func tplGetCurrentUser(r *http.Request) func() *models.User {
return func() *models.User {
return GetCurrentUser(r)
}
}
func tplGetStringSetting(key string) string {
val, _ := models.GetStringSetting(key)
return val
}
func tplIsValidTime(in time.Time) bool {
return in.Year() > 1
}
func tplParseFaviconType(url string) string {
split := strings.Split(url, ".")
if len(split) == 0 {
return ""
}
return split[len(split)-1]
}
var default_funcmap = template.FuncMap{
"TimeRelativeToNow": TimeRelativeToNow,
"Add": tplAdd,
"ParseMarkdown": tplParseMarkdown,
"IsValidTime": tplIsValidTime,
"GetStringSetting": tplGetStringSetting,
"ParseFaviconType": tplParseFaviconType,
}
func RenderTemplate(
out http.ResponseWriter,
r *http.Request,
tpl_file string,
context map[string]interface{},
funcs template.FuncMap) {
current_user := GetCurrentUser(r)
site_name, _ := config.Config.GetString("gobb", "site_name")
base_url, _ := config.Config.GetString("gobb", "base_url")
ga_tracking_id, _ := config.Config.GetString("googleanalytics", "tracking_id")
ga_account, _ := config.Config.GetString("googleanalytics", "account")
stylesheet := ""
if (current_user != nil) && current_user.StylesheetUrl.Valid && current_user.StylesheetUrl.String != "" {
stylesheet = current_user.StylesheetUrl.String
} else if current_user == nil || !current_user.StylesheetUrl.Valid || current_user.StylesheetUrl.String == "" {
global_theme, _ := models.GetStringSetting("theme_stylesheet")
if global_theme != "" {
stylesheet = global_theme
}
}
favicon_url, _ := models.GetStringSetting("favicon_url")
send := map[string]interface{}{
"current_user": current_user,
"request": r,
"site_name": site_name,
"ga_tracking_id": ga_tracking_id,
"ga_account": ga_account,
"stylesheet": stylesheet,
"favicon_url": favicon_url,
"base_url": base_url,
}
// Merge the global template variables with the local context
for key, val := range context {
send[key] = val
}
// Same with the function map
func_map := default_funcmap
func_map["GetCurrentUser"] = tplGetCurrentUser(r)
for key, val := range funcs {
func_map[key] = val
}
// Get the base template path
selected_template, _ := models.GetStringSetting("template")
var base_path string
if selected_template == "default" {
pkg, _ := build.Import("github.com/stevenleeg/gobb/gobb", ".", build.FindOnly)
base_path = filepath.Join(pkg.SrcRoot, pkg.ImportPath, "../templates/")
} else {
base_path, _ = config.Config.GetString("gobb", "base_path")
base_path = filepath.Join(base_path, "templates", selected_template)
}
base_tpl := filepath.Join(base_path, "base.html")
rend_tpl := filepath.Join(base_path, tpl_file)
tpl, err := template.New("tpl").Funcs(func_map).ParseFiles(base_tpl, rend_tpl)
if err != nil {
fmt.Printf("[error] Could not parse template (%s)\n", err.Error())
}
// Attempt to execute the template we're on
err = tpl.ExecuteTemplate(out, tpl_file, send)
if err != nil {
fmt.Printf("[error] Could not parse template (%s)\n", err.Error())
}
// And now the base template
err = tpl.ExecuteTemplate(out, "base.html", send)
if err != nil {
fmt.Printf("[error] Could not parse template (%s)\n", err.Error())
}
}