/
page.go
411 lines (383 loc) · 11.5 KB
/
page.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package main
import (
"bytes"
"fmt"
"html/template"
"image/png"
"path"
"sort"
"strconv"
"strings"
"github.com/anaminus/but"
"github.com/robloxapi/rbxapiref/builds"
"github.com/robloxapi/rbxapiref/documents"
"github.com/robloxapi/rbxapiref/entities"
"github.com/robloxapi/rbxapiref/fetch"
"github.com/robloxapi/rbxapiref/settings"
)
type Page struct {
// File is the path to the output file.
File string
// Meta is a set of extra metadata about the page.
Meta Meta
// Styles is a list of resources representing CSS styles.
Styles []Resource
// Scripts is a list of resources representing javascript files.
Scripts []Resource
// Resources is a list of other resources.
Resources []Resource
// DocResources is a list of document resources.
DocResources []Resource
// Template is the name of the template used to generate the page.
Template string
// Data is the data used by the template to generate the page.
Data interface{}
}
type Meta map[string]string
type Attr struct {
Name template.HTMLAttr
Value string
}
type Attrs []Attr
func (a Attrs) Find(name string) *Attr {
for i := range a {
if string(a[i].Name) == name {
return &a[i]
}
}
return nil
}
func (a *Attrs) Merge(b Attrs) {
for _, attrb := range b {
if attra := a.Find(string(attrb.Name)); attra != nil {
attra.Value = attrb.Value
continue
}
*a = append(*a, attrb)
}
}
type Resource struct {
// Name indicates the name of the source file located in the input
// resource directory, as well as the name of the generated file within
// the output resource directory.
Name string
// Content, if non-nil, specifies the content of the file directly, rather
// than reading from a source file.
Content []byte
// Embed causes the content of the resource to be embedded within a
// generated page, rather than being written to the output resource
// directory.
Embed bool
// Attr contains additional attributes of the generated HTML node
// representing the resource.
Attr Attrs
// Ignore allows the resource to exist, but otherwise be ignored when
// copying. This will prevent the resource destination from being deleted.
Ignore bool
}
func Title(sub string) string {
if sub != "" {
return sub + " " + settings.TitleSep + " " + settings.MainTitle
}
return settings.MainTitle
}
func FilterPages(pages []Page, filters []string) ([]Page, error) {
p := pages[:0]
for _, page := range pages {
if page.File == "" {
p = append(p, page)
continue
}
name := path.Clean(strings.Replace(page.File, "\\", "/", -1))
for i, filter := range filters {
for dir, file := name, ""; ; {
file = path.Join(path.Base(dir), file)
if ok, err := path.Match(filter, file); ok && err == nil {
p = append(p, page)
break
} else if err != nil {
return nil, fmt.Errorf("filter #%d: %w", i, err)
}
dir = path.Dir(dir)
if dir == "." || dir == "/" || dir == "" {
break
}
}
}
}
return p, nil
}
////////////////////////////////////////////////////////////////
func generatePageMain(data *Data) (pages []Page) {
page := Page{
Meta: Meta{
"Title": settings.MainTitle,
"Description": "Reference for the Roblox Lua API.",
"Image": "favicons/favicon-512x512.png",
},
Styles: []Resource{
{Name: "theme-light.css"},
{Name: "theme-dark.css"},
{Name: "main.css"},
{Name: "doc.css"},
},
Scripts: []Resource{
{Name: "quick-theme.js", Embed: true},
{Name: "main.js", Attr: Attrs{{"async", ""}}},
{Name: "search.js", Attr: Attrs{{"async", ""}}},
{Name: "settings.js", Attr: Attrs{{"async", ""}}},
{Name: "actions.js", Attr: Attrs{{"async", ""}}},
},
Template: "main",
}
if data.ResOnly {
page.Resources = append(page.Resources,
Resource{Name: "icon-explorer.png", Ignore: true},
)
} else {
// Fetch explorer icons.
latest := data.Manifest.Patches[len(data.Manifest.Patches)-1]
client := &fetch.Client{
Config: data.Settings.Build.Configs[latest.Config],
CacheMode: fetch.CacheTemp,
}
icon, err := client.ExplorerIcons(latest.Info.Hash)
but.IfFatalf(err, "%s: fetch icons", latest.Info.Hash)
var buf bytes.Buffer
but.IfFatal(png.Encode(&buf, icon), "encode icons file")
page.Resources = append(page.Resources,
Resource{Name: "icon-explorer.png", Content: buf.Bytes()},
)
}
page.Resources = append(page.Resources,
Resource{Name: "icon-objectbrowser.png"},
Resource{Name: "icon-devhub.png"},
Resource{Name: "favicons/favicon-512x512.png"},
Resource{Name: "favicons/favicon-32x32.png"},
Resource{Name: "favicons/favicon-16x16.png"},
Resource{Name: "favicons/favicon.ico"},
)
return []Page{page}
}
func generatePageIndex(output settings.Output) (pages []Page) {
return []Page{{
File: output.FilePath("index"),
Styles: []Resource{{Name: "index.css", Embed: true}},
Scripts: []Resource{{Name: "index.js", Attr: []Attr{{"async", ""}}}},
Template: "index",
}}
}
func generatePageAbout(output settings.Output) (pages []Page) {
return []Page{{
File: output.FilePath("about"),
Meta: Meta{
"Title": Title("About"),
"Description": "About the Roblox API Reference.",
},
Styles: []Resource{{Name: "about.css", Embed: true}},
Resources: []Resource{
{Name: "license-badge.png"},
},
Template: "about",
}}
}
func generatePageDocmon(output settings.Output, entities *entities.Entities) (pages []Page) {
return []Page{{
File: output.FilePath("docmon"),
Meta: Meta{
"Title": Title("Documentation monitor"),
"Description": "Status of documentation on the Roblox API Reference.",
},
Styles: []Resource{{Name: "docmon.css", Embed: true}},
Scripts: []Resource{{Name: "docmon.js", Attr: []Attr{{"async", ""}}}},
Template: "docmon",
Data: entities,
}}
}
func generatePageUpdates(output settings.Output, patches []builds.Patch) (pages []Page) {
if len(patches) <= 1 {
return nil
}
// Patches are displayed latest-first.
patchlist := make([]*builds.Patch, len(patches))
for i := len(patches) / 2; i >= 0; i-- {
j := len(patches) - 1 - i
patchlist[i], patchlist[j] = &patches[j], &patches[i]
}
// Exclude earliest patch.
patchlist = patchlist[:len(patchlist)-1]
type PatchSet struct {
Year int
Years []int
Patches []*builds.Patch
}
var latestPatches PatchSet
latestYear := patchlist[0].Info.Date.Year()
earliestYear := patchlist[len(patchlist)-1].Info.Date.Year()
patchesByYear := make([]PatchSet, latestYear-earliestYear+1)
years := make([]int, len(patchesByYear))
for i := range years {
years[i] = latestYear - i
}
{
// Populate patchesByYear.
i := 0
current := latestYear
for j, patch := range patchlist {
year := patch.Info.Date.Year()
if year < current {
if j > i {
patchesByYear[latestYear-current] = PatchSet{
Year: current,
Years: years,
Patches: patchlist[i:j],
}
}
current = year
i = j
}
}
if len(patchlist) > i {
patchesByYear[latestYear-current] = PatchSet{
Year: current,
Years: years,
Patches: patchlist[i:],
}
}
// Populate latestPatches.
i = len(patchlist)
epoch := patchlist[0].Info.Date.AddDate(0, -3, 0)
for j, patch := range patchlist {
if patch.Info.Date.Before(epoch) {
i = j - 1
break
}
}
latestPatches = PatchSet{
Years: years,
Patches: patchlist[:i],
}
}
styles := []Resource{{Name: "updates.css", Attr: []Attr{{"id", "updates-style"}}}}
scripts := []Resource{{Name: "updates.js", Attr: []Attr{{"async", ""}}}}
pages = make([]Page, len(patchesByYear)+1)
for i, patches := range patchesByYear {
year := strconv.Itoa(patches.Year)
pages[i] = Page{
File: output.FilePath("updates", year),
Meta: Meta{
"Title": Title("Updates in " + year),
"Description": "A list of updates to the Roblox Lua API in " + year + ".",
},
Styles: styles,
Scripts: scripts,
Template: "updates",
Data: patches,
}
}
pages[len(pages)-1] = Page{
File: output.FilePath("updates"),
Meta: Meta{
"Title": Title("Recent Updates"),
"Description": "A list of recent updates to the Roblox Lua API."},
Styles: styles,
Scripts: scripts,
Template: "updates",
Data: latestPatches,
}
return pages
}
// Normalizes the references within a document according to ParseDocReference,
// and returns any resources that the document refers to.
func NormalizeDocReferences(output settings.Output, document entities.Document) []Resource {
doc, ok := document.(documents.Linkable)
if !ok {
return nil
}
resources := map[string]*Resource{}
doc.SetLinks(func(link string) string {
scheme, path, link := output.ParseDocReference(link)
if scheme == "res" {
if _, ok := resources[path]; !ok {
resources[path] = &Resource{Name: path}
}
}
return link
})
docres := make([]Resource, 0, len(resources))
for _, resource := range resources {
docres = append(docres, *resource)
}
sort.Slice(docres, func(i, j int) bool {
return docres[i].Name < docres[j].Name
})
return docres
}
func generatePageClass(output settings.Output, classes []*entities.Class) (pages []Page) {
styles := []Resource{{Name: "class.css"}}
scripts := []Resource{{Name: "class.js", Attr: []Attr{{"async", ""}}}}
pages = make([]Page, len(classes))
for i, class := range classes {
pages[i] = Page{
File: output.FilePath("class", class.ID),
Meta: Meta{
"Title": Title(class.ID),
"Description": "Information about the " + class.ID + " class in the Roblox Lua API."},
Styles: styles,
Scripts: scripts,
DocResources: NormalizeDocReferences(output, class.Document),
Template: "class",
Data: class,
}
}
return pages
}
func generatePageEnum(output settings.Output, enums []*entities.Enum) (pages []Page) {
styles := []Resource{{Name: "enum.css"}}
scripts := []Resource{{Name: "enum.js", Attr: []Attr{{"async", ""}}}}
pages = make([]Page, len(enums))
for i, enum := range enums {
pages[i] = Page{
File: output.FilePath("enum", enum.ID),
Meta: Meta{
"Title": Title(enum.ID),
"Description": "Information about the " + enum.ID + " enum in the Roblox Lua API."},
Styles: styles,
Scripts: scripts,
DocResources: NormalizeDocReferences(output, enum.Document),
Template: "enum",
Data: enum,
}
}
return pages
}
func generatePageType(output settings.Output, types []*entities.Type) (pages []Page) {
styles := []Resource{{Name: "type.css"}}
scripts := []Resource{{Name: "type.js", Attr: []Attr{{"async", ""}}}}
pages = make([]Page, len(types))
for i, typ := range types {
pages[i] = Page{
File: output.FilePath("type", typ.Element.Category, typ.Element.Name),
Meta: Meta{
"Title": Title(typ.ID),
"Description": "Information about the " + typ.ID + " type in the Roblox Lua API."},
Styles: styles,
Scripts: scripts,
DocResources: NormalizeDocReferences(output, typ.Document),
Template: "type",
Data: typ,
}
}
return pages
}
func GeneratePages(data *Data) (pages []Page) {
pages = append(pages, generatePageMain(data)...)
pages = append(pages, generatePageIndex(data.Settings.Output)...)
pages = append(pages, generatePageAbout(data.Settings.Output)...)
pages = append(pages, generatePageDocmon(data.Settings.Output, data.Entities)...)
pages = append(pages, generatePageUpdates(data.Settings.Output, data.Manifest.Patches)...)
pages = append(pages, generatePageClass(data.Settings.Output, data.Entities.ClassList)...)
pages = append(pages, generatePageEnum(data.Settings.Output, data.Entities.EnumList)...)
pages = append(pages, generatePageType(data.Settings.Output, data.Entities.TypeList)...)
return pages
}