-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-docs.js
More file actions
527 lines (470 loc) · 21.7 KB
/
generate-docs.js
File metadata and controls
527 lines (470 loc) · 21.7 KB
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* Author: John Bieling
*/
import * as tools from './modules/tools.mjs';
import { Writer } from './modules/writer.mjs';
import { promises as fs } from "fs";
import path from "path";
const SBT = "`";
const TEMPLATE_PATH = `template`;
const HELP_SCREEN = `
Usage:
node generate-docs.js <options>
Options:
--schemas=path - ...
...
--output=path - Path of a folder to store the generated markdown
files. All existing files in that folder will be
deleted.
--manifest_version - ...
--report_errors - report errors in the schema files
`;
let ADDITIONAL_TYPE_PREFIXES = [];
const ADDITIONAL_TYPE_FILES = [
"experiments.json",
"extension_types.json",
"manifest.json",
"types.json",
"events.json"
];
const TITLE_DATA = {
"release": {
prefix: "",
slug: "",
},
"esr": {
prefix: "ESR ",
slug: "esr-",
},
"beta": {
prefix: "Beta ",
slug: "beta-",
},
"daily": {
prefix: "Daily ",
slug: "daily-",
},
}
const config = tools.parseArgs();
if (!config.schemas || !config.output || !config.manifest_version) {
console.log(HELP_SCREEN);
} else {
// Clone template folder and adjust cloned files.
const schemas = await tools.getSchemaFiles(config.schemas);
const thunderbird_version = schemas.map(a => a.data.map(e => e.applicationVersion).filter(Boolean)).flat().pop();
config.thunderbird_channel = "release";
if (thunderbird_version.includes("esr")) config.thunderbird_channel = "esr";
if (thunderbird_version.includes("b")) config.thunderbird_channel = "beta";
if (thunderbird_version.includes("a")) config.thunderbird_channel = "daily";
const long_title = `WebExtension API Documentation for Thunderbird ${thunderbird_version}`;
const title = `WebExtension API Documentation & Guides (Thunderbird ${TITLE_DATA[config.thunderbird_channel].prefix}${thunderbird_version.split(".")[0]}, Manifest V${config.manifest_version})`;
const link = `https://webextension-api.thunderbird.net/en/${TITLE_DATA[config.thunderbird_channel].slug}mv${config.manifest_version}/`
// Read fluent strings for permissions.
let PERMISSION_LOCALES = await fs.readFile(path.join(config.schemas, `permissions.ftl`), "utf8");
// Parent and Child implementations are in separate files and need to be
// merged. Sub namespaces are in the same file and need to be separated.
// Filter out global type definitions.
const namespaces = new Map();
const globalTypes = new Map();
const relatedNamespaceNames = new Map();
for (let schema of schemas) {
if (ADDITIONAL_TYPE_FILES.includes(schema.file)) {
let data = schema.data.find(e => e.types);
ADDITIONAL_TYPE_PREFIXES.push(data.namespace);
data.types.forEach(t => {
globalTypes.set(`${data.namespace}.${t.id}`, t)
});
continue;
}
let manifestNamespace = schema.data.find(e => e.namespace === "manifest");
let otherNamespaces = schema.data.filter(e => e.namespace !== "manifest");
for (let entry of otherNamespaces) {
const name = entry.namespace;
const namespace = tools.mergeSchema(namespaces.get(name) ?? [], entry, manifestNamespace);
namespaces.set(name, namespace);
}
const names = relatedNamespaceNames.get(schema.file) || [];
names.push(...otherNamespaces.map(e => e.namespace));
relatedNamespaceNames.set(schema.file, names);
}
// Expand properties with "$ref": "types.Setting" into synthetic sub-namespaces.
// The Setting type has functions (get, set, clear) and events (onChange), which
// map naturally to the existing sub-namespace documentation model.
const settingType = globalTypes.get("types.Setting");
const settingSubNamespaces = new Map();
if (settingType) {
for (const [namespaceName, schema] of [...namespaces]) {
const namespaceSchema = schema.find(e => e.namespace === namespaceName);
if (!namespaceSchema?.properties) continue;
const manifestSchema = schema.find(e => e.namespace === "manifest");
const settingPropertyNames = [];
for (const [propName, propDef] of Object.entries(namespaceSchema.properties)) {
if (propDef["$ref"] !== "types.Setting") continue;
settingPropertyNames.push(propName);
const subNamespaceName = `${namespaceName}.${propName}`;
const syntheticSchema = {
namespace: subNamespaceName,
functions: structuredClone(settingType.functions || []),
events: structuredClone(settingType.events || []),
description: propDef.description,
annotations: propDef.annotations,
};
const annotationProps = propDef.annotations
?.find(a => a.additional_properties)?.additional_properties;
if (annotationProps?.readOnly) {
syntheticSchema.functions = syntheticSchema.functions
.filter(f => f.name === "get");
}
const entry = [syntheticSchema];
entry.push(manifestSchema || { namespace: "manifest" });
namespaces.set(subNamespaceName, entry);
for (const [file, names] of relatedNamespaceNames) {
if (names.includes(namespaceName)) {
names.push(subNamespaceName);
break;
}
}
if (!settingSubNamespaces.has(namespaceName)) {
settingSubNamespaces.set(namespaceName, []);
}
settingSubNamespaces.get(namespaceName).push({
name: subNamespaceName,
description: propDef.description,
annotations: propDef.annotations,
readOnly: !!annotationProps?.readOnly,
});
}
for (const propName of settingPropertyNames) {
delete namespaceSchema.properties[propName];
}
if (Object.keys(namespaceSchema.properties).length === 0) {
delete namespaceSchema.properties;
}
}
}
// Setting sub-namespaces are listed via toctree in their parent page,
// not in the top-level API list.
const settingNames = new Set([...settingSubNamespaces.values()].flat().map(s => s.name));
const apiNames = [...namespaces.keys()].filter(n => !settingNames.has(n))
await tools.copyFolder(TEMPLATE_PATH, config.output);
await tools.processFiles(config.output, /\.rst$/i, true, content => {
let rv = tools.evaluateConditionTag(content, config);
rv = tools.indentHonoringReplace(rv, "{{API_LIST}}", apiNames.sort())
rv = tools.indentHonoringReplace(rv, "{{TITLE}}", [
"=".repeat(title.length),
title,
"=".repeat(title.length),
])
// Convert $(ref:...) to :ref:`...` with escaped upper case letters.
rv = rv.replace(/\$\(ref:(.*?)\)/g, (match, ref) =>
`:ref:${SBT}${tools.escapeUppercase(ref)}${SBT}`
)
return rv;
});
await tools.processFiles(config.output, "conf.py", false, content => {
let rv = tools.evaluateConditionTag(content, config);
rv = rv.replace("{{TITLE}}", `${long_title}<br><br>Manifest V${config.manifest_version}`)
return rv;
});
await tools.processFiles(config.output, "README.md", false, content => {
return content
.replace("{{TITLE}}", title)
.replace("{{LINK}}", link);
});
// First loop over manifest schemas to extract extends and update the global
// manifest schema.
for (let [namespaceName, schema] of namespaces) {
const manifestSchema = schema.find(e => e.namespace === "manifest");
for (let localDefinition of (manifestSchema.types || [])) {
let extend = localDefinition["$extend"];
// We only care about extends here. There *are* manifests which also
// add local types to the global manifest (Theme), but we use the local
// manifest for the individual API generations.
if (extend) {
let globalDefinition = globalTypes.get(`manifest.${extend}`);
globalDefinition = tools.mergeSchemaExtensions(globalDefinition, localDefinition);
globalTypes.set(`manifest.${extend}`, globalDefinition);
}
}
}
for (let [namespaceName, schema] of namespaces) {
const manifestSchema = schema.find(e => e.namespace === "manifest");
const namespaceSchema = schema.find(e => e.namespace === namespaceName);
const parentNamespaceSchemas = namespaceName.split(".").slice(0, -1)
.map((_, i, parts) => parts.slice(0, i + 1).join("."))
.map(name => namespaces.get(name)?.find(e => e.namespace === name));
const writer = new Writer({
config,
namespaces,
namespaceName,
namespaceSchema,
parentNamespaceSchemas,
manifestSchema,
globalTypes,
PERMISSION_LOCALES,
ADDITIONAL_TYPE_PREFIXES,
RELATED_NAMESPACE_NAMES: [...relatedNamespaceNames.values()].find(e => e.includes(namespaceName)),
SETTING_SUB_NAMESPACES: settingSubNamespaces.get(namespaceName) || [],
IS_SETTING: settingNames.has(namespaceName),
SETTING_NAMES: settingNames,
})
const doc = await writer.generateApiDoc();
await fs.writeFile(
path.join(config.output, `${namespaceName}.rst`),
doc.toString(),
"utf8"
);
}
// Collect changelog data from version_added annotations.
const changelog = new Map();
const changelogResolveRef = (ref) => {
return `:ref:\`${ref} <${tools.escapeUppercase(ref)}>\``;
};
const formatChangelogDescription = str =>
Writer.convertMarkupToRst(str, { resolveRef: changelogResolveRef })
?.replace(/:doc:`/g, ":doc:`../")
.replace(/\s+/g, " ")
.trim()
|| "";
const getVersion = node =>
node?.annotations?.find(a => typeof a.version_added === "string")?.version_added;
// Recursively collect version_added entries, skipping children that
// share the same version as their parent.
function scanNode(node, parentVersion, path, results) {
const nodeVersion = getVersion(node);
const effectiveVersion = nodeVersion || parentVersion;
// Record this node if its version differs from parent and it has
// a description. Entries without descriptions are skipped.
if (nodeVersion && nodeVersion !== parentVersion && path.length > 0 && node.description) {
results.push({
version: nodeVersion,
path: [...path],
description: formatChangelogDescription(node.description),
});
}
// Scan functions.
for (const func of node.functions || []) {
const funcPath = [...path, { type: "functions", name: func.name }];
scanNode(func, effectiveVersion, funcPath, results);
for (const param of func.parameters || []) {
if (param.name === func.async) continue; // skip callback
scanNode(param, getVersion(func) || effectiveVersion,
[...funcPath, { type: "parameters", name: param.name }], results);
scanProperties(param, getVersion(param) || getVersion(func) || effectiveVersion, funcPath, param.name, results);
}
}
// Scan events.
for (const event of node.events || []) {
const eventPath = [...path, { type: "events", name: event.name }];
scanNode(event, effectiveVersion, eventPath, results);
for (const param of event.parameters || []) {
scanNode(param, getVersion(event) || effectiveVersion,
[...eventPath, { type: "parameters", name: param.name }], results);
scanProperties(param, getVersion(param) || getVersion(event) || effectiveVersion, eventPath, param.name, results);
}
}
// Scan types (including manifest types without $extend).
const types = [...(node.types || [])];
for (const type of types) {
if (type.$extend) continue;
const typePath = [...path, { type: "types", name: type.id || type.name }];
scanNode(type, effectiveVersion, typePath, results);
const typeVersion = getVersion(type) || effectiveVersion;
scanProperties(type, typeVersion, typePath, null, results);
scanEnums(type, typeVersion, typePath, results);
}
// Scan namespace-level properties.
scanProperties(node, effectiveVersion, path, null, results);
}
function scanProperties(node, parentVersion, parentPath, paramName, results) {
for (const [key, prop] of Object.entries(node.properties || {})) {
const propPath = paramName
? [...parentPath, { type: "parameters", name: `${paramName}.${key}` }]
: [...parentPath, { type: "properties", name: key }];
scanNode(prop, parentVersion, propPath, results);
// Recurse into nested object properties.
if (prop.properties) {
scanProperties(prop, getVersion(prop) || parentVersion, parentPath,
paramName ? `${paramName}.${key}` : key, results);
}
scanEnums(prop, getVersion(prop) || parentVersion, propPath, results);
}
for (const choice of node.choices || []) {
scanProperties(choice, parentVersion, parentPath, paramName, results);
}
}
function scanEnums(node, parentVersion, parentPath, results) {
if (node.enums) {
for (const [enumName, enumDef] of Object.entries(node.enums)) {
const enumVersion = getVersion(enumDef);
if (enumVersion && enumVersion !== parentVersion && enumDef.description) {
results.push({
version: enumVersion,
path: [...parentPath, { type: "enums", name: enumName }],
description: formatChangelogDescription(enumDef.description),
});
}
}
}
}
function addChangelogResults(namespaceName, results) {
for (const result of results) {
if (!changelog.has(result.version)) changelog.set(result.version, []);
let group = changelog.get(result.version).find(g => g.namespace === namespaceName && !g.isNew);
if (!group) {
group = { namespace: namespaceName, isNew: false, newItems: [], seen: new Set() };
changelog.get(result.version).push(group);
}
const name = result.path.map(p => p.name).join(".");
if (group.seen.has(name)) continue;
group.seen.add(name);
group.newItems.push({
section: result.path[0].type,
name,
description: result.description,
});
}
}
for (const [namespaceName, schema] of namespaces) {
const ns = schema.find(e => e.namespace === namespaceName);
if (!ns) continue;
const nsVersion = getVersion(ns);
// Entire namespace is new.
if (nsVersion) {
if (!changelog.has(nsVersion)) changelog.set(nsVersion, []);
changelog.get(nsVersion).push({
namespace: namespaceName,
description: formatChangelogDescription(ns.description || ""),
isNew: true,
});
}
// Scan all children recursively.
const results = [];
const manifestSchema = schema.find(e => e.namespace === "manifest");
const manifestTypes = (manifestSchema?.types || []).filter(t => t.id && !t.$extend);
const nsWithManifestTypes = { ...ns, types: [...(ns.types || []), ...manifestTypes] };
scanNode(nsWithManifestTypes, nsVersion, [], results);
// Group results by version and namespace, deduplicating by name.
addChangelogResults(namespaceName, results);
// Scan manifest $extend types for changelog entries. The doc page
// name matches the camelCase namespace name by convention (e.g.
// "oauth_provider" -> "oauthProvider", see lines 101-105).
const manifestExtendResults = [];
for (const type of (manifestSchema?.types || []).filter(t => t.$extend)) {
// Use null as parent version — the manifest namespace version is
// just a file-level container, not a meaningful parent.
scanProperties(type, null, [], null, manifestExtendResults);
}
addChangelogResults(namespaceName, manifestExtendResults);
}
// Determine minimum changelog version from the template placeholder.
const indexContent = await fs.readFile(path.join(config.output, "index.rst"), "utf8");
const minVersionMatch = indexContent.match(/\{\{CHANGELOG_LIST(?::(\d+))?\}\}/);
const minChangelogVersion = minVersionMatch?.[1] ? parseInt(minVersionMatch[1], 10) : 0;
// Generate changelog RST files (only for versions >= minimum).
const changelogDir = path.join(config.output, "changelog");
await fs.mkdir(changelogDir, { recursive: true });
const sortedVersions = [...changelog.keys()]
.filter(v => parseInt(v, 10) >= minChangelogVersion)
.sort((a, b) => {
// Sort versions numerically descending (newest first).
const partsA = a.split(".").map(Number);
const partsB = b.split(".").map(Number);
for (let i = 0; i < Math.max(partsA.length, partsB.length); i++) {
const diff = (partsB[i] || 0) - (partsA[i] || 0);
if (diff !== 0) return diff;
}
return 0;
});
for (const version of sortedVersions) {
const entries = changelog.get(version);
const title = `Changelog for Thunderbird ${version}`;
const lines = [
".. role:: permission",
"",
".. role:: value",
"",
".. role:: code",
"",
".. role:: small",
"",
"=".repeat(title.length),
title,
"=".repeat(title.length),
"",
`The following WebExtension API changes were introduced in Thunderbird ${version}.`,
"",
];
const sectionLabels = {
functions: "function",
events: "event",
types: "type",
properties: "property",
};
const sectionOrder = ["functions", "events", "types", "properties"];
const formatApiHeader = (namespaceName) => [
"",
namespaceName,
"-".repeat(namespaceName.length),
"",
".. raw:: html",
"",
` <div class="changelog-api-header">`,
` <span class="changelog-api-label">api</span>`,
` <a href="../${namespaceName}.html">${namespaceName}</a>`,
` </div>`,
"",
];
const sortedEntries = entries
.filter(api => api.description || (api.newItems && api.newItems.length > 0))
.sort((a, b) => a.namespace.localeCompare(b.namespace));
for (const api of sortedEntries) {
lines.push(...formatApiHeader(api.namespace));
if (api.isNew) {
if (api.description) {
lines.push(api.description, "");
}
} else {
const sortedItems = api.newItems.sort((a, b) => {
const orderA = sectionOrder.indexOf(a.section);
const orderB = sectionOrder.indexOf(b.section);
if (orderA !== orderB) return orderA - orderB;
return a.name.localeCompare(b.name);
});
for (const item of sortedItems) {
const label = sectionLabels[item.section] || item.section;
const ref = `:ref:\`${item.name} <${tools.escapeUppercase(`${api.namespace}.${item.name}`)}>\``;
if (item.description) {
lines.push(`- :small:\`${label}\` ${ref} — ${item.description}`);
} else {
lines.push(`- :small:\`${label}\` ${ref}`);
}
}
lines.push("");
}
}
await fs.writeFile(
path.join(changelogDir, `${version}.rst`),
lines.join("\n"),
"utf8"
);
}
// Replace {{CHANGELOG_LIST:minVersion}} in the output index.rst.
// The minVersion parameter limits which changelog entries are included.
await tools.processFiles(config.output, "index.rst", false, content => {
return content.replace(
/(\s*)\{\{CHANGELOG_LIST(?::(\d+))?\}\}/gm,
(match, indent, minVersion) => {
const min = minVersion ? parseInt(minVersion, 10) : 0;
const filtered = sortedVersions.filter(v => parseInt(v, 10) >= min);
return filtered
.map(v => `${indent}Thunderbird ${v} <changelog/${v}>`)
.join("\n");
}
);
});
}