forked from bakape/shamichan
-
Notifications
You must be signed in to change notification settings - Fork 1
/
templates.go
87 lines (75 loc) · 1.71 KB
/
templates.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
//go:generate qtc
// Package templates generates and stores HTML templates
package templates
import (
"bytes"
"fmt"
"html"
"meguca/config"
"meguca/lang"
"sync"
)
var (
indexTemplates = make(map[string][3][]byte, 7)
mu sync.RWMutex
)
// Compile reads template HTML from disk, injects dynamic variables,
// hashes and stores them
func Compile() error {
t := make(map[string][3][]byte, len(lang.Packs))
for id := range lang.Packs {
ln := lang.Packs[id]
firstPass := renderIndex(ln)
split := bytes.Split([]byte(firstPass), []byte("$$$"))
t[id] = [3][]byte{split[0], split[1], split[2]}
}
mu.Lock()
indexTemplates = t
mu.Unlock()
return nil
}
// Board renders board page HTML for noscript browsers. withIndex specifies, if
// the rendered board page should be embedded in the index page
func Board(
b string,
ln lang.Pack,
page, total int,
minimal, catalog bool,
threadHTML []byte,
) []byte {
boardConf := config.GetBoardConfigs(b)
title := html.EscapeString(fmt.Sprintf("/%s/ - %s", b, boardConf.Title))
html := renderBoard(
threadHTML,
b, title,
boardConf,
page, total,
catalog,
ln,
)
if minimal {
return []byte(html)
}
return execIndex(html, title, ln.ID)
}
// Thread renders thread page HTML for noscript browsers
func Thread(ln lang.Pack, id uint64, board string, minimal bool, postHTML []byte) []byte {
html := renderThread(postHTML, id, board, ln)
if minimal {
return []byte(html)
}
return execIndex(html, "", ln.ID)
}
// Execute and index template in the second pass
func execIndex(html, title, lang string) []byte {
mu.RLock()
t := indexTemplates[lang]
mu.RUnlock()
return bytes.Join([][]byte{
t[0],
[]byte(title),
t[1],
[]byte(html),
t[2],
}, nil)
}