-
Notifications
You must be signed in to change notification settings - Fork 0
/
layout.go
212 lines (197 loc) · 5.16 KB
/
layout.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
211
212
package beepboop
import (
"html/template"
"net/http"
"strings"
"github.com/Masterminds/sprig/v3"
)
var styleT = `
a {
color: black;
text-decoration: underline;
text-decoration-color: rgb(220, 53, 69);
-webkit-text-decoration-color: rgb(220, 53, 69);
}
a:hover {
color: dimgrey;
}
input[type="text"], input[type="password"] {
border: 0;
outline: 0;
background: transparent;
border-bottom: 1px solid black;
margin-bottom: 1rem;
min-width: 250px;
}
input[type="submit"], input[type="button"], button, .button {
border: 1px solid black;
border-radius: 5px;
background-color: whitesmoke;
padding: 5px 10px;
margin: 10px 0;
color: black;
text-decoration: none;
display: inline-block;
cursor: pointer;
}
input[type="submit"]:disabled, input[type="button"]:disabled, button:disabled, .button:disabled {
color: grey;
}
input[type="submit"]:not(:disabled):hover, input[type="button"]:not(:disabled):hover,
button:not(:disabled):hover, .button:not(:disabled):hover {
background-color: lightsteelblue;
}
table {
border-collapse: collapse;
margin-bottom: 1rem;
border-spacing: 0;
}
td {
padding: 10px;
border: 1px solid transparent;
}
tr:nth-child(odd) > td {
background-color: #F0F0F0;
}
tr:first-child > td {
font-weight: bold;
border-bottom: 1px solid black;
background-color: white;
}
tr:not(:first-child):hover > td {
background-color: lightsteelblue;
}
tr:not(:first-child) > td:first-child {
border-radius: 10px 0 0 10px;
border: 0;
}
tr:not(:first-child) > td:last-child {
border-radius: 0 10px 10px 0;
border: 0;
}
tr:not(:first-child) > td:only-child {
border-radius: 10px;
border: 0;
}
small {
color: dimgrey;
}
`
var layoutT = `
<!DOCTYPE html>
<html>
<head>
{{if .Title}}<title>{{.Title}}</title>{{end}}
<base href="{{.Base}}" />
{{range $name, $content := .Meta}}
<meta name="{{$name}}" content="{{$content}}" />
{{end}}
<link rel="icon" href="favicon.png" type="image/png" />
<style>
body {
background-color: white;
}
div.outer {
display: flex;
align-items: center;
justify-content: center;
}
div.inner {
background-color: white;
padding: 1rem;
display: inline-flex;
}
@media screen and (max-width: 1200px) {
body {
margin: 0;
}
div.inner, div.inner > div {
width: 100%;
}
}
@media screen and (min-width: 1200px) {
body {
margin: 1rem;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='4' height='4' viewBox='0 0 4 4'%3E%3Cpath fill='%23808080' fill-opacity='0.5' d='M1 3h1v1H1V3zm2-2h1v1H3V1z'%3E%3C/path%3E%3C/svg%3E");
}
div.inner {
border: 1px solid black;
border-radius: 15px;
}
}
{{template "style"}}
</style>
{{range .Stylesheets}}
<link rel="stylesheet" href="{{.}}" />
{{end}}
{{range .Scripts}}
<script src="{{.}}"></script>
{{end}}
</head>
<body>
<div class="outer">
<div class="inner">
<div>
{{template "page" .Data}}
</div>
</div>
</div>
</body>
</html>
`
// Layout is used to give pages a uniform layout
type Layout interface {
BindTemplate(pageTemplate string, stylesheets, scripts []string, meta map[string]string) (LayoutRenderer, error)
}
// LayoutRenderer is a function that renders a html page
type LayoutRenderer func(w http.ResponseWriter, r *http.Request, title string, data interface{}, statusCode int)
// DefaultLayout is razlink's default layout
var DefaultLayout Layout = (*layout)(template.Must(template.New("layout").Funcs(sprig.FuncMap()).Funcs(TemplateFuncs).Parse(layoutT)))
type layout template.Template
// BindTemplate creates a layout renderer function from a page template
func (l *layout) BindTemplate(pageTemplate string, stylesheets, scripts []string, meta map[string]string) (LayoutRenderer, error) {
cloneLayout, _ := (*template.Template)(l).Clone()
tmpl, err := cloneLayout.New("page").Parse(pageTemplate)
if err != nil {
return nil, err
}
if tmpl.Lookup("style") == nil {
tmpl = template.Must(tmpl.New("style").Parse(styleT))
}
return func(w http.ResponseWriter, r *http.Request, title string, data interface{}, statusCode int) {
view := struct {
Title string
Base string
Stylesheets []string
Scripts []string
Meta map[string]string
Data interface{}
}{
Title: title,
Base: GetBase(r),
Stylesheets: stylesheets,
Scripts: scripts,
Meta: meta,
Data: data,
}
w.WriteHeader(statusCode)
tmpl.ExecuteTemplate(w, "layout", &view)
}, nil
}
// GetBase returns the base target for relative URLs
func GetBase(r *http.Request) string {
slashes := strings.Count(r.URL.Path, "/")
if slashes > 1 {
return strings.Repeat("../", slashes-1)
}
return "/"
}
// ErrorRenderer is a special kind of layout renderer to render an error
type ErrorRenderer func(w http.ResponseWriter, r *http.Request, errmsg string, errcode int)
// GetErrorRenderer returns an ErrorRenderer using the given layout
func GetErrorRenderer(layout Layout) ErrorRenderer {
renderer, _ := layout.BindTemplate("<strong>{{.}}</strong>", nil, nil, nil)
return func(w http.ResponseWriter, r *http.Request, errmsg string, errcode int) {
renderer(w, r, errmsg, errmsg, errcode)
}
}