/
html.go
359 lines (318 loc) · 8.57 KB
/
html.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package html
import (
"bytes"
"fmt"
"html"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"time"
"github.com/pkg/errors"
"github.com/fgahr/termchan/tchan"
"github.com/fgahr/termchan/tchan/output"
"github.com/fgahr/termchan/tchan/util"
)
type TemplateSet struct {
welcome *template.Template
post *template.Template
board *template.Template
thread *template.Template
error *template.Template
}
func placeholders() template.FuncMap {
nothing := func(v interface{}) string { return "" }
return template.FuncMap{
"formatBoard": nothing,
"formatPost": nothing,
"highlight": nothing,
"timeANSIC": nothing,
}
}
// UseDefaults resets the template set to its defaults.
func (t *TemplateSet) UseDefaults() {
// Needs to be replaced before template execution
t.welcome = template.Must(
template.New("welcome.template").
Funcs(placeholders()).
Parse(output.DefaultWelcome))
t.post = template.Must(
template.New("post.template").
Funcs(placeholders()).
Parse(output.DefaultPost))
t.board = template.Must(
template.New("board.template").
Funcs(placeholders()).
Parse(output.DefaultBoard))
t.thread = template.Must(
template.New("thread.template").
Funcs(placeholders()).
Parse(output.DefaultThread))
t.error = template.Must(
template.New("error.template").
Funcs(placeholders()).
Parse(output.DefaultError))
}
func parseTemplateFile(name string, dir string) (*template.Template, error) {
path := filepath.Join(dir, name)
if exists, err := util.FileExists(path); err != nil {
return nil, errors.Wrapf(err, "unable to check out template file %s", path)
} else if !exists {
// Nothing to do here
return nil, nil
}
f, err := os.Open(path)
if err != nil {
return nil, errors.Wrapf(err, "unable to open template file %s", path)
}
defer f.Close()
content, err := ioutil.ReadAll(f)
if err != nil {
return nil, errors.Wrapf(err, "unable to read from template file %s", path)
}
tmpl, err := template.New(name).Funcs(placeholders()).Parse(string(content))
if err != nil {
return nil, errors.Wrapf(err, "error parsing template file %s", path)
}
return tmpl, nil
}
// Read attempts to read recognized templates from within the given directory.
// Missing template files will be substituted with the defaults.
func (t *TemplateSet) Read(dir string) error {
// Reset to defaults, then look for alternatives
t.UseDefaults()
if exists, err := util.DirExists(dir); err != nil {
return errors.Wrapf(err, "unable to check out template directory %s", dir)
} else if !exists {
// Nothing to do, just use the defaults
return nil
}
if tmpl, err := parseTemplateFile("welcome.template", dir); err != nil {
return err
} else if tmpl != nil {
t.welcome = tmpl
}
if tmpl, err := parseTemplateFile("post.template", dir); err != nil {
return err
} else if tmpl != nil {
t.post = tmpl
}
if tmpl, err := parseTemplateFile("thread.template", dir); err != nil {
return err
} else if tmpl != nil {
t.thread = tmpl
}
if tmpl, err := parseTemplateFile("board.template", dir); err != nil {
return err
} else if tmpl != nil {
t.board = tmpl
}
if tmpl, err := parseTemplateFile("error.template", dir); err != nil {
return err
} else if tmpl != nil {
t.error = tmpl
}
return nil
}
type Writer struct {
req *http.Request
out http.ResponseWriter
temp TemplateSet
}
func NewWriter(r *http.Request, w http.ResponseWriter, ts TemplateSet) *Writer {
return &Writer{req: r, out: w, temp: ts}
}
func (w *Writer) withHeaderAndFooter(f func() error) error {
if _, err := w.out.Write([]byte(header)); err != nil {
return errors.Wrap(err, "writing HTML header failed")
}
if err := f(); err != nil {
return err
}
if _, err := w.out.Write([]byte(footer)); err != nil {
return errors.Wrap(err, "writing HTML footer failed")
}
return nil
}
func formatBoard(board tchan.Board) template.HTML {
return template.HTML(fmt.Sprintf(
"/<span class=%q>%s</span>/ - <span class=%q>%s</span>",
board.Style, board.Name, board.Style, board.Descr))
}
func (w *Writer) WriteWelcome(boards []tchan.Board) error {
return w.withHeaderAndFooter(func() error {
payload := struct {
Defaults // embedded
Boards []tchan.Board
Hostname string
}{
Defaults: defaults,
Boards: boards,
Hostname: w.req.Host,
}
return w.temp.welcome.
Funcs(template.FuncMap{"formatBoard": formatBoard}).
Execute(w.out, payload)
})
}
func (w *Writer) WriteThread(thread tchan.Thread) error {
return w.withHeaderAndFooter(func() error {
payload := struct {
Defaults // embedded
tchan.Thread // embedded
}{
Defaults: defaults,
Thread: thread,
}
return w.temp.thread.
Funcs(template.FuncMap{
"formatPost": w.postFormatter(thread.Board.Style),
"formatBoard": w.boardFormatter(),
"highlight": w.highlighter(thread.Board.Style),
"timeANSIC": w.timeFormatter(time.ANSIC),
}).
Execute(w.out, payload)
})
}
func (w *Writer) WriteBoard(board tchan.BoardOverview) error {
return w.withHeaderAndFooter(func() error {
payload := struct {
Defaults // embedded
tchan.BoardOverview // embedded
}{
Defaults: defaults,
BoardOverview: board,
}
return w.temp.board.
Funcs(template.FuncMap{
"formatPost": w.postFormatter(board.Style),
"formatBoard": w.boardFormatter(),
"highlight": w.highlighter(board.Style),
"timeANSIC": w.timeFormatter(time.ANSIC),
}).
Execute(w.out, payload)
})
}
func (w *Writer) WriteError(status int, err error) error {
return w.withHeaderAndFooter(func() error {
w.out.WriteHeader(status)
payload := struct {
Defaults // embedded
Status int
Error string
}{
Defaults: defaults,
Status: status,
Error: err.Error(),
}
return w.temp.error.
Funcs(template.FuncMap{
"timeANSIC": w.timeFormatter(time.ANSIC),
"highlight": w.highlighter("red"),
}).
Execute(w.out, payload)
})
}
func (w *Writer) postFormatter(styleName string) func(tchan.Post) template.HTML {
return func(p tchan.Post) template.HTML {
p.Content = html.EscapeString(p.Content)
payload := struct {
Defaults // embedded
tchan.Post // embedded
}{
Defaults: defaults,
Post: p,
}
buf := bytes.Buffer{}
err := w.temp.post.Funcs(template.FuncMap{
"highlight": w.highlighter(styleName),
"timeANSIC": w.timeFormatter(time.ANSIC),
}).Execute(&buf, payload)
if err != nil {
log.Println(err)
return ""
}
return template.HTML(buf.String())
}
}
func (w *Writer) boardFormatter() func(tchan.Board) template.HTML {
return func(b tchan.Board) template.HTML {
return w.formatBoard(b)
}
}
func (w *Writer) highlighter(styleName string) func(interface{}) template.HTML {
return func(v interface{}) template.HTML {
return template.HTML(fmt.Sprintf("<span class=%q>%v</span>", styleName, v))
}
}
func (w *Writer) timeFormatter(format string) func(time.Time) template.HTML {
return func(t time.Time) template.HTML {
return template.HTML(t.Format(format))
}
}
func (w *Writer) formatBoard(board tchan.Board) template.HTML {
return template.HTML(fmt.Sprintf(
"/<span class=%q>%s</span>/ - <span class=%q>%s</span>",
board.Style, board.Name, board.Style, board.Descr))
}
type Defaults struct {
FgBlack template.HTML
FgRed template.HTML
FgGreen template.HTML
FgYellow template.HTML
FgBlue template.HTML
FgMagenta template.HTML
FgCyan template.HTML
FgWhite template.HTML
End template.HTML
Separator struct {
Single template.HTML
Double template.HTML
}
}
var defaults Defaults = Defaults{
FgBlack: "<span class=\"black\">",
FgRed: "<span class=\"red\">",
FgGreen: "<span class=\"green\">",
FgYellow: "<span class=\"yellow\">",
FgBlue: "<span class=\"blue\">",
FgMagenta: "<span class=\"magenta\">",
FgCyan: "<span class=\"cyan\">",
FgWhite: "<span class=\"white\">",
End: "</span>",
Separator: struct {
Single template.HTML
Double template.HTML
}{
Single: "<span class=\"black\">--------------------------------------------------------------------------------</span>",
Double: "<span class=\"black\">================================================================================</span>",
},
}
const header = `<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>termchan</title>
<style>
<!--
body { color: #ffffff; background-color: #202020; }
.black { color: #000000; }
.red { color: #ff0000; }
.green { color: #00ff00; }
.yellow { color: #ffff00; }
.blue { color: #0000ff; }
.magenta { color: #ff00ff; }
.cyan { color: #00ffff; }
.white { color: #ffffff; }
-->
</style>
</head>
<body>
<pre>
`
const footer = `</pre>
</body>
</html>
`