forked from derekjones562/5e-spelljammer.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil-book-reference.js
135 lines (118 loc) · 2.96 KB
/
util-book-reference.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
require("../js/utils.js");
require("../js/render.js");
UtilBookReference = {
getSections (refId) {
switch (refId) {
case "bookref-quick":
return [
"Character Creation",
"Equipment",
"Playing the Game",
"Combat",
"Adventuring",
];
case "bookref-dmscreen":
return [
"Running the Game",
"Combat",
"Factions",
];
default:
throw new Error(`No sections defined for book id ${refId}`);
}
},
getIndex (...refTypes) {
const index = require(`../data/books.json`);
const books = {};
index.book.forEach(b => {
books[b.id.toLowerCase()] = require(`../data/book/book-${b.id.toLowerCase()}.json`);
});
const outJson = {
reference: {},
data: {},
};
refTypes.forEach(it => outJson.reference[it.id] = {
name: it.name,
id: it.id,
contents: [],
});
let bookData = [];
function reset () {
bookData = [];
index.book.forEach(b => {
const data = {source: b.id, file: MiscUtil.copy(books[b.id.toLowerCase()])};
bookData.push(data);
});
}
refTypes.forEach(refType => {
reset();
const out = {};
function recursiveSetSource (ent, source) {
if (ent instanceof Array) {
ent.forEach(e => recursiveSetSource(e, source));
} else if (typeof ent === "object") {
if (ent.page) ent.source = source;
Object.values(ent).forEach(v => recursiveSetSource(v, source))
}
}
function isDesiredSect (ent) {
return ent.entries && ent.data && ent.data[refType.tag];
}
function recursiveAdd (ent, source) {
if (ent.entries) {
ent.entries = ent.entries.filter(nxt => recursiveAdd(nxt, source));
}
if (isDesiredSect(ent)) {
const sect = ent.data[refType.tag];
if (!out[sect]) {
out[sect] = {
sectName: UtilBookReference.getSections(refType.id)[sect - 1],
sections: [],
};
}
const toAdd = MiscUtil.copy(ent);
toAdd.type = "section";
const discard = !!toAdd.data.allowRefDupe;
recursiveSetSource(toAdd, source);
out[sect].sections.push(toAdd);
return discard;
} else {
return true;
}
}
bookData.forEach(book => {
book.file.data.forEach(chap => {
if (chap.entries) {
recursiveAdd(chap, book.source);
}
})
});
Object.keys(out).sort().forEach(i => {
const sects = out[i].sections.sort((a, b) => SortUtil.ascSort(a.name, b.name));
const header = outJson.reference[refType.id];
header.contents.push({
name: out[i].sectName,
headers: sects.map(s => s.name),
});
const toAdd = {
type: "entries",
entries: sects,
};
if (!outJson.data[refType.id]) outJson.data[refType.id] = [];
outJson.data[refType.id].push(toAdd);
});
});
const walker = MiscUtil.getWalker();
walker.walk(
outJson.data,
{
object: (obj) => {
delete obj.id; // Remove IDs to avoid duplicates
return obj;
},
},
);
return outJson;
},
};
module.exports.UtilBookReference = UtilBookReference;