-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
315 lines (284 loc) · 6.72 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"math"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"text/template"
"github.com/libretro/ludo/rdb"
)
var target = "build"
var perPage = 24
var tmpl *template.Template
// Scrub characters that are not cross-platform and/or violate the
// No-Intro filename standard.
func scrubIllegalChars(str string) string {
str = strings.Replace(str, "&", "_", -1)
str = strings.Replace(str, "*", "_", -1)
str = strings.Replace(str, "/", "_", -1)
str = strings.Replace(str, ":", "_", -1)
str = strings.Replace(str, "`", "_", -1)
str = strings.Replace(str, "<", "_", -1)
str = strings.Replace(str, ">", "_", -1)
str = strings.Replace(str, "?", "_", -1)
str = strings.Replace(str, "|", "_", -1)
str = strings.Replace(str, "\"", "_", -1)
return str
}
func reverse(s rdb.RDB) rdb.RDB {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
func loadDB(dir string) (rdb.DB, error) {
files, err := ioutil.ReadDir(dir)
if err != nil {
return rdb.DB{}, err
}
db := make(rdb.DB)
for _, f := range files {
name := f.Name()
if !strings.Contains(name, ".rdb") {
continue
}
system := name[0 : len(name)-4]
bytes, _ := ioutil.ReadFile(filepath.Join(dir, name))
db[system] = reverse(rdb.Parse(bytes))
}
return db, nil
}
func buildHome(db rdb.DB) {
os.MkdirAll(target, os.ModePerm)
os.Link("img-broken.png", filepath.Join(target, "img-broken.png"))
f, err := os.OpenFile(filepath.Join(target, "index.html"), os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
log.Fatal(err)
}
defer f.Close()
tmpl.ExecuteTemplate(f, "home.html", struct {
DB rdb.DB
}{
db,
})
}
func buildSystemPages(system string, games rdb.RDB) {
os.MkdirAll(filepath.Join(target, system), os.ModePerm)
numPages := int(math.Ceil(float64(len(games)) / float64(perPage)))
for p := 0; p < numPages; p++ {
page := fmt.Sprintf("%d", p)
f, err := os.OpenFile(filepath.Join(target, system, "index-"+page+".html"), os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
log.Fatal(err)
}
tmpl.ExecuteTemplate(f, "systempage.html", struct {
System string
Games rdb.RDB
Page int
LastPage int
}{
system,
games[p*perPage : min(p*perPage+perPage, len(games))],
p,
numPages - 1,
})
f.Close()
}
}
func buildGame(system string, game rdb.Game) {
path := filepath.Join(target, system, scrubIllegalChars(game.Name)+".html")
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
log.Fatal(err)
}
defer f.Close()
tmpl.ExecuteTemplate(f, "game.html", struct {
System string
Game rdb.Game
}{
system,
game,
})
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
var funcMap = template.FuncMap{
"mkslice": func(a []rdb.Game, start, end int) []rdb.Game {
e := min(end, len(a))
return a[start:e]
},
"Clean": scrubIllegalChars,
"Tags": func(name string) []string {
_, tags := extractTags(name)
return tags
},
"WithoutTags": func(name string) string {
sname, _ := extractTags(name)
return sname
},
"add": func(a, b int) int {
return a + b
},
"title": func(name string) string {
return strings.Title(name)
},
}
func extractTags(name string) (string, []string) {
re := regexp.MustCompile(`\(.*?\)`)
pars := re.FindAllString(name, -1)
var tags []string
for _, par := range pars {
name = strings.Replace(name, par, "", -1)
par = strings.Replace(par, "(", "", -1)
par = strings.Replace(par, ")", "", -1)
results := strings.Split(par, ",")
for _, result := range results {
tags = append(tags, strings.TrimSpace(result))
}
}
name = strings.TrimSpace(name)
return name, tags
}
func build() {
tmpl = template.Must(
template.New("main").Funcs(funcMap).ParseGlob("templates/*.html"),
)
db, err := loadDB("./database/rdb")
if err != nil {
log.Fatal(err)
}
buildHome(db)
wg := sync.WaitGroup{}
for system, games := range db {
buildSystemPages(system, games)
system := system
games := games
wg.Add(1)
go func() {
for _, game := range games {
buildGame(system, game)
}
wg.Done()
}()
}
buildTags(db, "franchise")
buildTags(db, "developer")
buildTags(db, "publisher")
buildTags(db, "genre")
buildTags(db, "origin")
wg.Wait()
}
// Given a metatag like franchise or genre or developer, will create a hierarchy
// of tag files and indexes allowing to browse the database based on this
// property.
func buildTags(db rdb.DB, tagType string) {
perTag := map[string][]rdb.Game{}
for system, games := range db {
for _, game := range games {
game.System = system
switch tagType {
case "franchise":
if game.Franchise == "" {
continue
}
perTag[game.Franchise] = append(perTag[game.Franchise], game)
case "developer":
if game.Developer == "" {
continue
}
perTag[game.Developer] = append(perTag[game.Developer], game)
case "publisher":
if game.Publisher == "" {
continue
}
perTag[game.Publisher] = append(perTag[game.Publisher], game)
case "genre":
if game.Genre == "" {
continue
}
perTag[game.Genre] = append(perTag[game.Genre], game)
case "origin":
if game.Origin == "" {
continue
}
perTag[game.Origin] = append(perTag[game.Origin], game)
}
}
}
buildTagIndex(perTag, tagType)
for tag, games := range perTag {
buildTagPage(tagType, tag, games)
}
}
// Will build a page like /franchise.html that will list all the franchises in
// the database.
func buildTagIndex(perTag map[string][]rdb.Game, tagType string) {
os.MkdirAll(filepath.Join(target, tagType), os.ModePerm)
path := filepath.Join(target, tagType, "index.html")
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
log.Fatal(err)
}
defer f.Close()
err = tmpl.ExecuteTemplate(f, "tags.html", struct {
TagType string
PerTag map[string][]rdb.Game
}{
tagType,
perTag,
})
if err != nil {
log.Fatal(err)
}
}
// Will build a page like /franchise/Bomberman.html that will list all the
// games of the bomberman franchise.
func buildTagPage(tagType, tag string, games []rdb.Game) {
path := filepath.Join(target, tagType, scrubIllegalChars(tag)+".html")
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
log.Fatal(err)
}
defer f.Close()
err = tmpl.ExecuteTemplate(f, "tag.html", struct {
TagType string
Tag string
Games []rdb.Game
}{
tagType,
tag,
games,
})
if err != nil {
log.Fatal(err)
}
}
func serve() {
fs := http.FileServer(http.Dir(target))
http.Handle("/", fs)
log.Println("Listening on http://0.0.0.0:3003")
http.ListenAndServe(":3003", nil)
}
func main() {
flag.Parse()
args := flag.Args()
switch args[0] {
case "serve":
serve()
case "build":
fallthrough
default:
build()
}
}