forked from oakmac/chessboardjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-website.js
364 lines (293 loc) · 9.31 KB
/
build-website.js
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
// -----------------------------------------------------------------------------
// This files builds the .html files in the website/ folder
// -----------------------------------------------------------------------------
// libraries
const fs = require('fs')
const kidif = require('kidif')
const mustache = require('mustache')
const docs = require('./data/docs.json')
const encoding = {encoding: 'utf8'}
// grab some mustache templates
const docsTemplate = fs.readFileSync('templates/docs.mustache', encoding)
const downloadTemplate = fs.readFileSync('templates/download.mustache', encoding)
const examplesTemplate = fs.readFileSync('templates/examples.mustache', encoding)
const homepageTemplate = fs.readFileSync('templates/homepage.mustache', encoding)
const singleExampleTemplate = fs.readFileSync('templates/single-example.mustache', encoding)
const headTemplate = fs.readFileSync('templates/_head.mustache', encoding)
const headerTemplate = fs.readFileSync('templates/_header.mustache', encoding)
const footerTemplate = fs.readFileSync('templates/_footer.mustache', encoding)
const latestChessboardJS = fs.readFileSync('src/chessboard.js', encoding)
const latestChessboardCSS = fs.readFileSync('src/chessboard.css', encoding)
// grab the examples
const examplesArr = kidif('examples/*.example')
const examplesObj = examplesArr.reduce(function (examplesObj, example, idx) {
examplesObj[example.id] = example
return examplesObj
}, {})
const examplesGroups = [
{
name: 'Basic Usage',
examples: [1000, 1001, 1002, 1003, 1004]
},
{
name: 'Config',
examples: [2000, 2044, 2063, 2001, 2002, 2003, 2082, 2004, 2030, 2005, 2006]
},
{
name: 'Methods',
examples: [3000, 3001, 3002, 3003, 3004, 3005, 3006, 3007]
},
{
name: 'Events',
examples: [4000, 4001, 4002, 4003, 4004, 4005, 4006, 4011, 4012]
},
{
name: 'Integration',
examples: [5000, 5001, 5002, 5003, 5004, 5005]
}
]
const homepageExample1 = ''
const homepageExample2 = `
var board2 = Chessboard('board2', {
draggable: true,
dropOffBoard: 'trash',
sparePieces: true
})
$('#startBtn').on('click', board2.start)
$('#clearBtn').on('click', board2.clear)`.trim()
function writeSrcFiles () {
fs.writeFileSync('website/js/chessboard.js', latestChessboardJS, encoding)
fs.writeFileSync('website/css/chessboard.css', latestChessboardCSS, encoding)
}
function writeHomepage () {
const headHTML = mustache.render(headTemplate, {pageTitle: 'Homepage'})
const html = mustache.render(homepageTemplate, {
footer: footerTemplate,
head: headHTML,
example2: homepageExample2
})
fs.writeFileSync('website/index.html', html, encoding)
}
function writeExamplesPage () {
const headHTML = mustache.render(headTemplate, {pageTitle: 'Examples'})
const headerHTML = mustache.render(headerTemplate, {examplesActive: true})
const html = mustache.render(examplesTemplate, {
examplesJavaScript: buildExamplesJS(),
footer: footerTemplate,
head: headHTML,
header: headerHTML,
nav: buildExamplesNavHTML()
})
fs.writeFileSync('website/examples.html', html, encoding)
}
const configTableRowsHTML = docs.config.reduce(function (html, itm) {
if (isString(itm)) return html
return html + buildConfigDocsTableRowHTML('config', itm)
}, '')
const methodTableRowsHTML = docs.methods.reduce(function (html, itm) {
if (isString(itm)) return html
return html + buildMethodRowHTML(itm)
}, '')
const errorRowsHTML = docs.errors.reduce(function (html, itm) {
if (isString(itm)) return html
return html + buildErrorRowHTML(itm)
}, '')
function writeDocsPage () {
const headHTML = mustache.render(headTemplate, {pageTitle: 'Documentation'})
const headerHTML = mustache.render(headerTemplate, {docsActive: true})
const html = mustache.render(docsTemplate, {
configTableRows: configTableRowsHTML,
errorRows: errorRowsHTML,
footer: footerTemplate,
head: headHTML,
header: headerHTML,
methodTableRows: methodTableRowsHTML
})
fs.writeFileSync('website/docs.html', html, encoding)
}
function writeDownloadPage () {
const headHTML = mustache.render(headTemplate, {pageTitle: 'Download'})
const headerHTML = mustache.render(headerTemplate, {downloadActive: true})
const html = mustache.render(downloadTemplate, {
footer: footerTemplate,
head: headHTML,
header: headerHTML
})
fs.writeFileSync('website/download.html', html, encoding)
}
function writeWebsite () {
writeSrcFiles()
writeHomepage()
writeExamplesPage()
writeDocsPage()
writeDownloadPage()
}
writeWebsite()
// -----------------------------------------------------------------------------
// HTML
// -----------------------------------------------------------------------------
function htmlEscape (str) {
return (str + '')
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/\//g, '/')
.replace(/`/g, '`')
}
function buildExampleGroupHTML (idx, groupName, examplesInGroup) {
const groupNum = idx + 1
let html = '<h4 id="groupHeader-' + groupNum + '">' + groupName + '</h4>' +
'<ul id="groupContainer-' + groupNum + '" style="display:none">'
examplesInGroup.forEach(function (exampleId) {
const example = examplesObj[exampleId]
html += '<li id="exampleLink-' + exampleId + '">' + example.name + '</id>'
})
html += '</ul>'
return html
}
function buildExamplesNavHTML () {
let html = ''
examplesGroups.forEach(function (group, idx) {
html += buildExampleGroupHTML(idx, group.name, group.examples)
})
return html
}
function buildExamplesJS () {
let txt = 'window.CHESSBOARD_EXAMPLES = {}\n\n'
examplesArr.forEach(function (ex) {
txt += 'CHESSBOARD_EXAMPLES["' + ex.id + '"] = {\n' +
' description: ' + JSON.stringify(ex.description) + ',\n' +
' html: ' + JSON.stringify(ex.html) + ',\n' +
' name: ' + JSON.stringify(ex.name) + ',\n' +
' jsStr: ' + JSON.stringify(ex.js) + ',\n' +
' jsFn: function () {\n' + ex.js + '\n }\n' +
'};\n\n'
})
return txt
}
function buildConfigDocsTableRowHTML (propType, prop) {
let html = ''
// table row
html += '<tr id="' + propType + ':' + prop.name + '">'
// property and type
html += '<td>' + buildPropertyAndTypeHTML(propType, prop.name, prop.type) + '</td>'
// default
html += '<td class="center"><p>' + buildDefaultHTML(prop.default) + '</p></td>'
// description
html += '<td>' + buildDescriptionHTML(prop.desc) + '</td>'
// examples
html += '<td>' + buildExamplesCellHTML(prop.examples) + '</td>'
html += '</tr>'
return html
}
function buildMethodRowHTML (method) {
const nameNoParens = method.name.replace(/\(.+$/, '')
let html = ''
// table row
if (method.noId) {
html += '<tr>'
} else {
html += '<tr id="methods:' + nameNoParens + '">'
}
// name
html += '<td><p><a href="docs.html#methods:' + nameNoParens + '">' +
'<code class="js plain">' + method.name + '</code></a></p></td>'
// args
if (method.args) {
html += '<td>'
method.args.forEach(function (arg) {
html += '<p>' + arg[1] + '</p>'
})
html += '</td>'
} else {
html += '<td><small>none</small></td>'
}
// description
html += '<td>' + buildDescriptionHTML(method.desc) + '</td>'
// examples
html += '<td>' + buildExamplesCellHTML(method.examples) + '</td>'
html += '</tr>'
return html
}
function buildPropertyAndTypeHTML (section, name, type) {
let html = '<p><a href="docs.html#' + section + ':' + name + '">' +
'<code class="js plain">' + name + '</code></a></p>' +
'<p class=property-type-7ae66>' + buildTypeHTML(type) + '</p>'
return html
}
function buildTypeHTML (type) {
if (!Array.isArray(type)) {
type = [type]
}
let html = ''
for (var i = 0; i < type.length; i++) {
if (i !== 0) {
html += ' <small>or</small><br />'
}
html += type[i]
}
return html
}
function buildRequiredHTML (req) {
if (!req) return 'no'
if (req === true) return 'yes'
return req
}
function buildDescriptionHTML (desc) {
if (!Array.isArray(desc)) {
desc = [desc]
}
let html = ''
desc.forEach(function (d) {
html += '<p>' + d + '</p>'
})
return html
}
function buildDefaultHTML (defaultValue) {
if (!defaultValue) {
return '<small>n/a</small>'
}
return defaultValue
}
function buildExamplesCellHTML (examplesIds) {
if (!Array.isArray(examplesIds)) {
examplesIds = [examplesIds]
}
let html = ''
examplesIds.forEach(function (exampleId) {
var example = examplesObj[exampleId]
if (!example) return
html += '<p><a href="examples.html#' + exampleId + '">' + example.name + '</a></p>'
})
return html
}
function buildErrorRowHTML (error) {
let html = ''
// table row
html += '<tr id="errors:' + error.id + '">'
// id
html += '<td class="center">' +
'<p><a href="docs.html#errors:' + error.id + '">' + error.id + '</a></p></td>'
// desc
html += '<td><p>' + error.desc + '</p></td>'
// more information
if (error.fix) {
if (!Array.isArray(error.fix)) {
error.fix = [error.fix]
}
html += '<td>'
error.fix.forEach(function (p) {
html += '<p>' + p + '</p>'
})
html += '</td>'
} else {
html += '<td><small>n/a</small></td>'
}
html += '</tr>'
return html
}
function isString (s) {
return typeof s === 'string'
}