-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild-data.js
356 lines (325 loc) · 11.1 KB
/
build-data.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
const fs = require('fs')
const fse = require('fs-extra')
const fm = require('front-matter')
const _ = require('lodash')
const config = require('./package.json')
// Markdown it options
const MarkdownIt = require('markdown-it')
const md = new MarkdownIt({ html: true })
md.use(require('markdown-it-container'), 'alert')
.use(require('markdown-it-multimd-table'))
md.renderer.rules.table_open = function () {
return '<v-simple-table class="mb-8"><template v-slot:default>'
}
md.renderer.rules.table_close = function () {
return '</template></v-simple-table>'
}
md.renderer.rules.blockquote_open = function () {
return '<v-card class="blockquote-card"><v-card-text>'
}
md.renderer.rules.blockquote_close = function () {
return '</v-card-text></v-card>'
}
md.renderer.rules.container_alert_open = function () {
return '<v-alert value type="info">';
}
md.renderer.rules.container_alert_close = function () {
return '</v-alert>';
}
md.renderer.rules.image = function (tokens, idx, options, env, self) {
const token = tokens[idx]
const srcIndex = token.attrIndex('src');
const src = token.attrs[srcIndex][1]
return `<v-img src="${src}" />`
}
md.renderer.rules.link_close = function(tokens, idx) {
const pToken = tokens[idx - 2]
const href = pToken.attrs[0][1]
if (/^\//.test(href)) {
return `</nuxt-link>`
} else {
return '</a>'
}
}
const setLinkLocalePrefix = (lang = null) => {
md.renderer.rules.link_open = function(tokens, idx) {
const token = tokens[idx]
let href = token.attrs[0][1]
if (/^\//.test(href)) {
if (lang) {
href = `/${lang}${href}`
}
return `<nuxt-link to="${href}">`
} else {
return `<a href="${href}" target="_blank">`
}
}
}
const ignore = ['messages', 'subspecies']
const staticData = ['about.json']
const staticMdData = ['guides-index.md', 'manual-index.md']
const versionDir = `./docs/v${config.version.replace(/\./g,'')}`
if (!fs.existsSync(versionDir)) {
fs.mkdirSync(versionDir);
}
const textPath = `./text`
const dataPath = `./data`
const langs = fs.readdirSync(textPath)
const dataDirs = fs.readdirSync(dataPath)
for (const lang of langs) {
const targetPath = `${versionDir}/${lang}`
setLinkLocalePrefix(lang === 'en' ? null : lang)
// create the target if it doesn't exist
if (!fs.existsSync(targetPath)) {
fs.mkdirSync(targetPath);
}
// copy any staticData
for (const f of staticData) {
fs.copyFile( `${dataPath}/${f}`, `${targetPath}/${f}`, (err) => {
if (err) throw err;
});
}
// copy and convert any static md files
for (const f of staticMdData) {
const fc = fm(fs.readFileSync(`${dataPath}/${f}`, 'utf8'))
let item = fc.attributes
fs.writeFileSync(`${targetPath}/${f.replace('.md', '.json')}`, JSON.stringify(item, null, 2))
}
const textSourcePath = `${textPath}/${lang}`
const processedModels = []
// process data dirs
for (const dir of dataDirs) {
if (ignore.includes(dir) || staticData.includes(dir) || staticMdData.includes(dir)) {
continue
}
// don't render the changelog in anything but english
if (dir === 'changelog' && lang !== 'en') {
continue
}
const modelDataPath = `${dataPath}/${dir}`
const modelTextPath = `${textSourcePath}/${dir}`
const modelTargetFile = `${targetPath}/${dir}.json`
const modelFns = fs.readdirSync(modelDataPath)
const items = []
for (const file of modelFns) {
let item = combineItem(file,dir, `${modelDataPath}/${file}`, `${modelTextPath}/${file}`)
// changelog
if (dir === 'changelog') {
item.date = new Date(item.date)
item.url = `/changelog/${item.slug}`
}
// species
if (dir === 'species') {
if (item.subspecies) {
const subspecies = fm(fs.readFileSync(`${dataPath}/subspecies/${item.subspecies}.md`, 'utf8'))
item.subspecies = {
name: subspecies.attributes.name,
html: md.render(subspecies.body)
}
} else {
item.subspecies = false
}
}
// spells
if (dir === 'powers') {
if (!item.version) {
console.log('no version for: ' + item.name)
continue
}
if (!item.tags) {
console.log('no tags for: ' + item.name)
continue
}
}
items.push(item)
}
// edges
if (dir === 'edges') {
items.push(...generateExaltedLineages().sort((a, b) => a.name < b.name ? -1 : 1))
}
fs.writeFileSync(modelTargetFile, JSON.stringify(items, null, 2))
processedModels.push(dir)
}
// compile the messages into a json file
const messages = require(`${textSourcePath}/messages`)
fs.writeFileSync(`${targetPath}/messages.json`, JSON.stringify(messages, null, 2))
// process text dirs
const textDirs = fs.readdirSync(textSourcePath)
for (const dir of textDirs) {
if (ignore.includes(dir) || staticData.includes(dir) || staticMdData.includes(dir) || processedModels.includes(dir)) {
continue
}
const modelTextPath = `${textSourcePath}/${dir}`
const modelTargetFile = `${targetPath}/${dir}.json`
const modelFns = fs.readdirSync(modelTextPath)
const items = modelFns.map(file => {
return combineItem(file, dir, `${modelTextPath}/${file}`)
})
fs.writeFileSync(modelTargetFile, JSON.stringify(items, null, 2))
}
}
/*
function createSpellLevels (item) {
if (item.version === 1) {
const baseMechanics = _.cloneDeep(item.mechanics[0])
for (const [index, mechanic] of item.mechanics.entries()) {
if (index === 0) {
continue
}
item.mechanics[index] = _.merge(_.cloneDeep(baseMechanics), mechanic)
}
for (const [advIndex, adv] of item.advancements.entries()) {
const baseAdvancement = adv.mechanics.length ? {...adv.mechanics[0]} : {}
for (const [mechanicIndex, mechanic] of item.mechanics.entries()) {
const toMerge = adv.mechanics[mechanicIndex] || baseAdvancement
item.advancements[advIndex].mechanics[mechanicIndex] = _.merge({ ...mechanic}, toMerge)
}
}
}
return item
}
*/
function getSpeciesInfo () {
const species = fs.readdirSync('./data/species').map(file => {
return combineItem(file, 'species', `./data/species/${file}`, `./text/en/species/${file}`)
})
const traits = fs.readdirSync('./data/traits').map(file => {
return combineItem(file, 'traits', `./data/traits/${file}`, `./text/en/traits/${file}`)
})
return { species, traits }
}
function generateExaltedLineages () {
const { species, traits } = getSpeciesInfo()
const exaltedLineages = []
const edgeType = 'exalted-lineages'
const asiRiciSSFilter = (mechanic) => {
// console.log(mechanic)
return ['resistance', 'sense', 'comdition-immunity', 'immunity'].includes(mechanic.type)
|| mechanic.type === 'speed' && mechanic.speed !== 'walk'
|| mechanic.type.startsWith('asi')
}
for (const sp of species) {
if (sp.id === 'kett' || sp.id === 'behemoth') {
continue
}
let id = `EXL_${sp.id}`
let name = sp.name
let mechanics = []
if (['variant', 'subspecies'].includes(sp.type) && sp.id !== 'ardat-yakshi') {
const parent = species.find(i => i.id === sp.species)
const parentName = parent.name
name = `${parentName} (${name})`
if (sp.type === 'variant') {
mechanics.push(...(sp.mechanics || parent.mechanics))
} else {
mechanics.push(...[...sp.mechanics, ...parent.mechanics])
}
} else if (sp.subspecies) {
continue
} else {
mechanics.push(...sp.mechanics)
}
const exclusions = ['hermetic-suit', 'pressurized-suit', 'contra-gravitic-levitation']
const traitMechanics = traits.filter(i => i.species.includes(sp.id) && !exclusions.includes(i.id))
.reduce((acc, curr) => acc.concat(curr.mechanics), [])
mechanics.push(...traitMechanics)
mechanics = mechanics.filter(asiRiciSSFilter).sort((a, b) => a.type - b.type)
let html = ''
const asis = mechanics.filter(i => i.type === 'asi').map(i => `+${i.amount} ${i.ability.toUpperCase()}`)
html += '<div class="text-subtitle-1">Ability Score Increase</div>'
if (asis.length) {
html += `<p>${asis.join(', ')}</p>`
}
const resistances = mechanics.filter(i => i.type === 'resistance').map(i => `${i.value || ''}${i.note ? ` (${i.note})` : ''}`)
if (resistances.length) {
html += '<div class="text-subtitle-1">Resistances</div>'
html += `<p>${resistances.join(', ')}</p>`
}
const conImms = mechanics.filter(i => i.type === 'condition-immunity').map(i => `${i.value || ''}${i.note || ''}`)
if (conImms.length) {
html += '<div class="text-subtitle-1">Condition Immunities</div>'
html += `<p>${conImms.join(', ')}</p>`
}
const speeds = mechanics.filter(i => i.type === 'speed' && i.speed !== 'walk').map(i => `${i.speed}ing <me-distance length="${i.distance}" />${i.note ? ` (${i.note})` : ''}`)
if (speeds.length) {
html += '<div class="text-subtitle-1">Speeds</div>'
html += `<p>${speeds.join(', ')}</p>`
}
const senses = mechanics.filter(i => i.type === 'sense').map(i => `${i.sense} <me-distance length="${i.distance}" />`)
if (senses.length) {
html += '<div class="text-subtitle-1">Senses</div>'
html += `<p>${senses.join(', ')}</p>`
}
exaltedLineages.push({
id,
name,
mechanics,
html,
type: edgeType
})
}
return exaltedLineages
}
function combineItem(id, dir, file1, file2 = null) {
const fc = fm(fs.readFileSync(file1, 'utf8'))
let item = fc.attributes
let body = fc.body
if (file2) {
if (fs.existsSync(file2)) {
const tFc = fm(fs.readFileSync(file2, 'utf8'))
const tItem = tFc.attributes
item = _.mergeWith(item, tItem, (objValue, srcValue) => {
if (_.isArray(objValue) && _.isObject(objValue[0]) && objValue[0].id) {
const newArray = []
for (let dIndex = 0; dIndex < objValue.length; dIndex++) {
if (objValue[dIndex].id) {
const tIndex = srcValue.findIndex(i => i.id === objValue[dIndex].id)
if (tIndex > -1) {
newArray.push(_.merge(objValue[dIndex], srcValue[tIndex]))
} else {
newArray.push(objValue[dIndex])
}
}
}
return newArray.concat(srcValue.filter(i => !newArray.map(j => j.id).includes(i.id)))
}
})
body = tFc.body
}
}
item.html = md.render(body)
item.id = id.replace(/.md$/, '')
if (item.mechanics?.length) {
item.mechanics = appendResourceIds(item.mechanics, item.id, item.name, dir)
}
return item
}
function appendResourceIds (mechanics, id, name, model) {
const newMechanics = []
for (const m of mechanics) {
let newM = m
if (m.resource) {
if (!m.resource.id) {
newM = {
...m,
resource: {
...m.resource,
id
}
}
}
}
if (['action', 'reaction', 'other', 'bonus-action', 'attack'].includes(m.type)) {
newM = {
name,
moreInfo: {
model: model,
id: id
},
...newM,
}
}
newMechanics.push(newM)
}
return newMechanics
}