-
Notifications
You must be signed in to change notification settings - Fork 9
/
view.go
210 lines (194 loc) · 5.3 KB
/
view.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"bytes"
"html/template"
"net/http"
"sort"
"strings"
"time"
"github.com/gorilla/mux"
)
type TemplateData struct {
CurrentView string
LoggedIn bool
CanUpdatePatch bool
UserName string
StaticRoot string
Headline string
HeadlinePrefix string
FinalJS template.JS
Patch MasherPatch
Patches []MasherPatch
Referer string
ViewingUser MasherUser
Tutorial template.HTML
}
var masherTemplates *template.Template
var tutorialTemplates *template.Template
func InitTemplates() {
var err error
funcs := template.FuncMap{
"HumanDate": func(d int64) string {
return time.Unix(d, 0).Format("2006/01/02")
},
}
masherTemplates, err = template.New("masher").Funcs(funcs).ParseFiles(
"templates/layout.html",
"templates/patch.html",
"templates/user.html",
"templates/browse.html",
"templates/about.html",
"templates/learn.html",
"templates/404.html")
if err != nil {
panic(err)
}
tutorialTemplates, err = template.New("masher").Funcs(funcs).ParseGlob("templates/tutorial/*.html")
if err != nil {
panic(err)
}
}
func BaseTemplateData(w http.ResponseWriter, r *http.Request) TemplateData {
ret := TemplateData{
StaticRoot: MasherConfig.StaticRoot}
session, err := SessionStore.Get(r, "sess")
if err != nil {
panic(err)
}
if ret.UserName, ret.LoggedIn = session.Values["name"].(string); !ret.LoggedIn {
ret.UserName = ""
}
return ret
}
func ViewPatch(w http.ResponseWriter, r *http.Request) {
var err error
params := mux.Vars(r)
data := BaseTemplateData(w, r)
data.Patch, err = RetrievePatch(params["id"])
if err != nil {
ViewNotFound(w, r)
return
}
if data.Patch.Author == data.UserName {
data.CanUpdatePatch = true
}
data.CurrentView = "Patch"
data.Headline = data.Patch.Title + " by " + data.Patch.Author
err = masherTemplates.ExecuteTemplate(w, "layout.html", data)
if err != nil {
panic(err)
}
}
func ViewUser(w http.ResponseWriter, r *http.Request) {
var err error
params := mux.Vars(r)
data := BaseTemplateData(w, r)
data.ViewingUser, err = RetrieveUser(params["user"])
if err != nil {
ViewNotFound(w, r)
return
}
data.Patches, err = RetrievePatches(SearchFilter{Author: data.ViewingUser.Name})
if err != nil {
ViewNotFound(w, r)
return
}
sort.Slice(data.Patches, func(i, j int) bool {
return data.Patches[i].DateCreated > data.Patches[j].DateCreated
})
data.HeadlinePrefix = "Viewing: "
data.Headline = "patches by " + data.ViewingUser.Name
data.CurrentView = "User"
err = masherTemplates.ExecuteTemplate(w, "layout.html", data)
if err != nil {
panic(err)
}
}
func ViewContinue(w http.ResponseWriter, r *http.Request) {
data := BaseTemplateData(w, r)
data.CurrentView = "Patch"
data.HeadlinePrefix = "Editing: "
data.Headline = "new patch"
data.Patch.Files = map[string]string{"main.sp": "# You have just one Sporth. Make something.\n\n"}
data.FinalJS = " restoreAutosave(); "
err := masherTemplates.ExecuteTemplate(w, "layout.html", data)
if err != nil {
panic(err)
}
}
func ViewNew(w http.ResponseWriter, r *http.Request) {
data := BaseTemplateData(w, r)
data.CurrentView = "Patch"
data.HeadlinePrefix = "Editing: "
data.Headline = "new patch"
data.Patch.Files = map[string]string{"main.sp": "# You have just one Sporth. Make something.\n\n"}
err := masherTemplates.ExecuteTemplate(w, "layout.html", data)
if err != nil {
panic(err)
}
}
func ViewBrowse(w http.ResponseWriter, r *http.Request) {
var err error
data := BaseTemplateData(w, r)
data.Patches, err = RetrievePatches(SearchFilter{})
if err != nil {
ViewNotFound(w, r)
return
}
sort.Slice(data.Patches, func(i, j int) bool {
return data.Patches[i].DateCreated > data.Patches[j].DateCreated
})
data.HeadlinePrefix = "Viewing: "
data.Headline = "all patches"
data.CurrentView = "Browse"
err = masherTemplates.ExecuteTemplate(w, "layout.html", data)
if err != nil {
panic(err)
}
}
func ViewAbout(w http.ResponseWriter, r *http.Request) {
data := BaseTemplateData(w, r)
data.Headline = "About"
data.CurrentView = "About"
err := masherTemplates.ExecuteTemplate(w, "layout.html", data)
if err != nil {
panic(err)
}
}
func ViewLearn(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
tutPage := params["page"]
if tutPage == "" {
tutPage = "index"
}
tutPage = tutPage + ".html"
tutorialBuf := &bytes.Buffer{}
data := BaseTemplateData(w, r)
data.CurrentView = "Learn"
err := tutorialTemplates.ExecuteTemplate(tutorialBuf, tutPage, data)
if err != nil {
ViewNotFound(w, r)
return
}
var title = strings.Split(strings.SplitAfter(tutorialBuf.String(), "<title>")[1], "</title>")[0]
if title == "" {
title = strings.Split(strings.SplitAfter(tutorialBuf.String(), "<h1>")[1], "</h1>")[0]
}
data.Headline = title
data.Tutorial = template.HTML(strings.Split(strings.SplitAfter(tutorialBuf.String(), "<body>")[1], "</body>")[0])
data.Tutorial = "<!-- start content -->" + data.Tutorial + "<!-- end content -->"
err = masherTemplates.ExecuteTemplate(w, "layout.html", data)
if err != nil {
panic(err)
}
}
func ViewNotFound(w http.ResponseWriter, r *http.Request) {
data := BaseTemplateData(w, r)
data.Headline = "404 - Not Found"
data.CurrentView = "404"
w.WriteHeader(http.StatusNotFound)
err := masherTemplates.ExecuteTemplate(w, "layout.html", data)
if err != nil {
panic(err)
}
}