forked from Polymer/old-docs-site
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_api_docs_1.js
More file actions
304 lines (263 loc) · 9.5 KB
/
generate_api_docs_1.js
File metadata and controls
304 lines (263 loc) · 9.5 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
/**
* Run from `npm run generate-api-docs`
*/
const {Analyzer, FSUrlLoader, PackageUrlResolver, generateAnalysis} = require('polymer-analyzer');
const clone = require('clone');
const fs = require('fs');
const {exec} = require('child_process');
const escape = require('html-escape');
const path = require('path');
const apiDocsPath = '../app/1.0/docs/api/';
const rootNamespace = 'Polymer';
// TODO: Check out an actual release SHA to generate docs off of.
const releaseSha = '1.x';
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise ', p, ' reason: ', reason);
});
cleanUp(installPolymer);
// installPolymer();
// checkoutRef();
// generateDocs();
function cleanUp(callback) {
const command = 'rm -rf ./temp';
console.log(`Running ${command}...`);
exec(command, callback);
}
function installPolymer() {
console.log('Done.');
const command = 'git clone https://github.com/Polymer/polymer.git temp';
console.log(`Running ${command}...`);
exec(command, checkoutRef);
}
function checkoutRef() {
console.log('Done.');
const command = `cd temp && git checkout ${releaseSha} && cd ..`;
console.log(`Running ${command}...`);
exec(command, generateDocs);
}
function generateDocs() {
console.log('Done.');
const command = `rm -r ${apiDocsPath}*`;
console.log(`Running ${command}...`);
exec(command, runAnalyzer);
}
function runAnalyzer() {
console.log('Done.');
const isInTests = /(\b|\/|\\)(test)(\/|\\)/;
const isNotTest = (f) => !isInTests.test(f.sourceRange.file);
const analyzer = new Analyzer({
urlLoader: new FSUrlLoader(path.resolve('temp')),
urlResolver: new PackageUrlResolver(),
});
analyzer.analyzePackage()
.then((_package) => {
console.log('Analyzer done');
const metadata = generateAnalysis(_package, '', isNotTest);
const json = JSON.stringify(metadata, null, 2);
fs.writeFileSync('analysis.json', json);
function generateNamespace(namespace) {
console.log(`generating namespace ${namespace && namespace.name}`);
const overview = {
name: namespace.name,
description: namespace.description,
summary: namespace.summary,
namespaces: [],
elements: [],
classes: [],
mixins: [],
behaviors: [],
functions: namespace.functions, // already summarized
};
console.log(`Processing ${namespace.elements && namespace.elements.length} elements`);
if (namespace.elements) {
for (const element of namespace.elements) {
console.log(`adding ${getElementName(element)} to ${namespace.name}`);
const summary = {
name: element.name,
tagname: element.tagname,
summary: element.summary,
};
overview.elements.push(summary);
const fileContents = elementPage(element);
const filename = path.join(apiDocsPath, getElementUrl(element) + '.html');
console.log('Writing', filename);
fs.writeFileSync(filename, fileContents);
}
}
console.log(`Processing ${namespace.classes && namespace.classes.length} classes`);
if (namespace.classes) {
for (const klass of namespace.classes) {
if (!klass.name) {
continue;
}
console.log(`adding ${klass.name} to ${namespace.name}`);
const summary = {
name: klass.name,
summary: klass.summary,
};
overview.classes.push(summary);
const fileContents = classPage(klass);
const filename = path.join(apiDocsPath, getClassUrl(klass) + '.html');
console.log('Writing', filename);
fs.writeFileSync(filename, fileContents);
}
}
console.log(`Processing ${namespace.mixins && namespace.mixins.length} mixins`);
if (namespace.mixins) {
for (const mixin of namespace.mixins) {
console.log(`adding ${mixin.name} to ${namespace.name}`);
const summary = {
name: mixin.name,
summary: mixin.summary,
};
overview.mixins.push(summary);
const fileContents = mixinPage(mixin);
const filename = path.join(apiDocsPath, getMixinUrl(mixin) + '.html');
console.log('Writing', filename);
fs.writeFileSync(filename, fileContents);
}
}
const behaviors = ((namespace.metadata || {}).polymer || {}).behaviors || [];
console.log(`Processing ${behaviors.length} behaviors`);
for (const behavior of behaviors) {
console.log(`adding ${behavior.name} to ${namespace.name}`);
const summary = {
name: behavior.name,
summary: behavior.summary,
};
overview.behaviors.push(summary);
const fileContents = behaviorPage(behavior);
const filename = path.join(apiDocsPath, getBehaviorUrl(behavior) + '.html');
console.log('Writing', filename);
fs.writeFileSync(filename, fileContents);
}
console.log(`Processing ${namespace.namespaces && namespace.namespaces.length} namespaces`);
if (namespace.namespaces) {
for (const nestedNamespace of namespace.namespaces) {
console.log(`adding ${nestedNamespace.name} to ${namespace.name}`);
const summary = {
name: nestedNamespace.name,
summary: nestedNamespace.summary,
};
overview.namespaces.push(summary);
generateNamespace(nestedNamespace);
}
}
const fileContents = namespacePage(overview);
let filename;
if (namespace.name === 'Polymer' || !namespace.name) {
filename = 'index.html';
} else {
filename = getNamespaceUrl(namespace) + '.html';
}
const filepath = path.join(apiDocsPath, filename);
console.log('Writing', filepath);
fs.writeFileSync(filepath, fileContents);
}
fs.mkdirSync(path.join(apiDocsPath, 'elements'));
fs.mkdirSync(path.join(apiDocsPath, 'classes'));
fs.mkdirSync(path.join(apiDocsPath, 'mixins'));
fs.mkdirSync(path.join(apiDocsPath, 'behaviors'));
fs.mkdirSync(path.join(apiDocsPath, 'namespaces'));
// We know we just have 1 namespace: Polymer
generateNamespace(metadata);
cleanUp(function() {
console.log('Done.');
console.log('\nAPI docs completed with great success');
});
}, (e) => {
console.error('Error running analyzePackage()', e);
});
}
function elementPage(element) {
const name = getElementName(element);
const jsonString = escape(JSON.stringify(element));
return `{% set markdown = "true" %}
{% set title = "${name}" %}
{% extends "templates/base-devguide.html" %}
{% block title %} API Reference - ${name}{% endblock %}
{% block content %}
<iron-doc-element base-href="/1.0/docs/api" descriptor="${jsonString}"></iron-doc-element>
{% endblock %}`;
}
function classPage(klass) {
const name = klass.name;
const jsonString = escape(JSON.stringify(klass));
return `{% set markdown = "true" %}
{% set title = "${name}" %}
{% extends "templates/base-devguide.html" %}
{% block title %} API Reference - ${name}{% endblock %}
{% block content %}
<iron-doc-class base-href="/1.0/docs/api" descriptor="${jsonString}"></iron-doc-class>
{% endblock %}`;
}
function mixinPage(mixin) {
const name = mixin.name;
const jsonString = escape(JSON.stringify(mixin));
return `{% set markdown = "true" %}
{% set title = "${name}" %}
{% extends "templates/base-devguide.html" %}
{% block title %} API Reference - ${name}{% endblock %}
{% block content %}
<iron-doc-mixin base-href="/1.0/docs/api" descriptor="${jsonString}"></iron-doc-mixin>
{% endblock %}`;
}
function behaviorPage(behavior) {
const name = behavior.name;
const jsonString = escape(JSON.stringify(behavior));
return `{% set markdown = "true" %}
{% set title = "${name}" %}
{% extends "templates/base-devguide.html" %}
{% block title %} API Reference - ${name}{% endblock %}
{% block content %}
<iron-doc-behavior base-href="/1.0/docs/api" descriptor="${jsonString}"></iron-doc-behavior>
{% endblock %}`;
}
function namespacePage(namespace) {
const name = namespace.name;
const jsonString = escape(JSON.stringify(namespace));
return `{% set markdown = "true" %}
{% set title = "${name}" %}
{% extends "templates/base-devguide.html" %}
{% block title %} API Reference - ${name}{% endblock %}
{% block content %}
<iron-doc-namespace base-href="/1.0/docs/api" descriptor="${jsonString}"></iron-doc-namespace>
{% endblock %}`;
}
function getNamespaceName(name) {
if (typeof name === 'string') {
const parts = name.split('.');
console.log(name, ':', parts);
if (parts.length > 1) {
return parts.slice(0, parts.length - 1).join('.');
}
}
return rootNamespace;
}
function getElementName(element) {
let name = '';
if (element.tagname) {
name += `<${element.tagname}>`;
if (element.name) {
name += ` (${element.name})`;
}
} else if (element.name) {
name += element.name;
}
return name;
}
function getElementUrl(element) {
return `/elements/${element.name || element.tagname}`;
}
function getClassUrl(klass) {
return `/classes/${klass.name}`;
}
function getMixinUrl(mixin) {
return `/mixins/${mixin.name}`;
}
function getBehaviorUrl(behavior) {
return `/behaviors/${behavior.name}`;
}
function getNamespaceUrl(namespace) {
return `/namespaces/${namespace.name}`;
}