-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathAPIProcessor.ts
More file actions
233 lines (215 loc) · 8.09 KB
/
APIProcessor.ts
File metadata and controls
233 lines (215 loc) · 8.09 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
import * as fs from 'fs';
import * as _ from 'lodash';
import * as path from 'path';
import { BaseProcesser } from './BaseProcesser';
import * as consts from './const';
const docsJson = JSON.parse(fs.readFileSync(path.join(__dirname, '../docs.json'), 'utf8'));
interface SidebarDefinition {
children: [string, string];
collapsable: boolean;
title: string;
}
/**
* Processor used to process API docs located under lib/src/
*/
export class APIProcessor extends BaseProcesser {
public apiChildren = [];
private vuepressExtraConfigPath = path.join(__dirname, '../md_out/.vuepress/apiExtra.json');
private themePath = path.join(__dirname, '../themes/markdown');
private entityPageFile = 'entity_page.hbs';
private homePageFile = 'api_readme.hbs';
private apiOutputPath = path.join(__dirname, '../md_out/api');
private srcApiHomeTheme = path.join(this.themePath, this.homePageFile);
private destApiHomePage = path.join(__dirname, '../md_out/api/README.md');
private pathDelimeter = '.';
// Class and Function
private entityKindWhitelist = [consts.kindNumberClass, consts.kindNumberFunction]; // Whitelisting kinds when grabbing class or method
private moduleNameBlackList = ['"'];
/**
* Run the processor
* @param hbs
*/
public run(hbs): void {
// Creating required dir
this.createDir();
// Order API children
this.apiChildren = this.retrieveOrderedAPIs(docsJson);
// Construct the sidebar configs
this.buildSidebar(this.apiChildren);
// Create the API homepage
this.processHomePage(hbs, this.apiChildren);
// Process API pages
this.processAPIEntityPage(hbs, this.apiChildren);
}
/**
* Build the sidebar json for the API pages
* @param apiChildren
*/
private buildSidebar(apiChildren): void {
const extraConfig = {
apiSidebar: this.buildSidebarJSON(apiChildren),
};
// Writing extraConfig object as .vuepress/apiExtra.json
fs.writeFileSync(this.vuepressExtraConfigPath, JSON.stringify(extraConfig), 'utf-8');
}
/**
* Build a sidebar JSON for nested navigations
* e.g. [{"title":"cluster","collapsable":false,"children":[["./cluster.KMeans","KMeans"]]}
* @param apiChildren
* @returns {Array}
*/
private buildSidebarJSON(apiChildren): SidebarDefinition[] {
return _.reduce(
apiChildren,
(sum, child) => {
const [module, name] = child.name.split('.');
const existingGroupIndex = _.findIndex(sum, (o) => o.text === module);
if (existingGroupIndex === -1) {
// If there's no existing module group according to the current child's name
// create a new definition and append it to the sum
const newDefinition = {
children: [{ link: `/api/${child.name}`, text: name }],
collapsable: true,
text: module,
};
return _.concat(sum, [newDefinition]);
} else {
// If there's an existing module definition,
// then append the current child's definition to the children list
const existing = sum[existingGroupIndex];
const newChildren = _.concat(existing.children, [{ link: `/api/${child.name}`, text: name }]);
const updated = _.set(existing, 'children', newChildren);
return _.set(sum, `[${existingGroupIndex}]`, updated);
}
},
[],
);
}
/**
* Util funciton to clean any unwanted chars
* @param name
* @returns {string}
*/
private cleanName = (name) => {
return _.reduce(
this.moduleNameBlackList,
(filteredName, blackKey) => {
return _.replace(filteredName, blackKey, '');
},
name,
);
};
/**
* Retrieve ordered APIs using raw docs generated from typedoc --json
* @param docs
* @returns {any[]}
*/
private retrieveOrderedAPIs(docs): any {
// Aggregate children
const aggregatedFirstChildren = _.reduce(
docs.children,
(aggregation, moduleChild) => {
// Looping the first children layer
// Group child according the module name
const [module] = moduleChild.name.split('/');
// Clean any unwanted chars from the module name
const cleanedModuleName = this.cleanName(module);
// Grabbing each class or method of the module
// Also it squashes the entities by moduelName.entityName e.g. preprocessing.OneHotEncoder
const squashedEntityList = _.reduce(
moduleChild.children,
(entityList, entityChild) => {
// Filter by entityKindWhitelist and skips if isIgnore comment is set
if (this.entityKindWhitelist.indexOf(entityChild.kind) !== -1 && !this.isIgnore(entityChild)) {
// each function or class name
const entityName = entityChild.name;
const fullEntityName = [cleanedModuleName, entityName].join(this.pathDelimeter);
const newEntityChild = _.set(entityChild, 'name', fullEntityName);
return _.concat(entityList, [newEntityChild]);
}
return entityList;
},
[],
);
// Filter out undefined entity appended as a result of whitelisting during the reduce
const filteredEntityList = _.filter(squashedEntityList, (x) => !_.isUndefined(x));
// Concat the squashedEntityList to the _.reduce aggregation
// Also applies a shallow flatten as squashedEntityList is an array
return _.flatten(_.concat(aggregation, filteredEntityList));
},
[],
);
// Ordering each entity by its name
return _.orderBy(aggregatedFirstChildren, ['name']);
}
/**
* Create API directory if not exist
*/
private createDir(): void {
// 1.2. creating the second portion: /Users/jasons/Desktop/machinelearnjs/docs/md_out/pages
if (!fs.existsSync(this.apiOutputPath)) {
fs.mkdirSync(this.apiOutputPath);
}
}
/**
* Process API folder's homepage aka README
*/
private processHomePage(hbs, apiChildren): void {
const grouped = _.groupBy(apiChildren, (o) => o.name.split(this.pathDelimeter)[0]);
const keys = _.keys(grouped);
const restructedChildren = _.map(keys, (key) => {
return {
key,
value: _.get(grouped, key),
};
});
const apiHomePageThemeContent = fs.readFileSync(this.srcApiHomeTheme, 'utf8');
const template = hbs.compile(apiHomePageThemeContent);
const compiledPage = template(restructedChildren);
fs.appendFileSync(this.destApiHomePage, compiledPage, { flag: 'a' });
}
/**
* Check if the passed in child has a commnet "ignore"
* 1. If child does not have comment, search for an identical signature and pull out @ignore tag if it exists
* @param child
*/
private isIgnore(child): boolean {
// Find ignores from given tags
const findIgnores = (givenTags) =>
_.find(givenTags, (tag) => {
return tag.tag === consts.tagTypeIgnore;
});
// If child does not have comment attribute by default, then search for a signature
if (!child.comment) {
const { signatures } = child;
const identSignature = _.find(signatures, (sig) => {
return sig.name === child.name;
});
const foundTags = _.get(identSignature, 'comment.tags', []);
const foundIgnores = findIgnores(foundTags);
return !_.isEmpty(foundIgnores);
}
// Otherwise, evaluate using the parent comment
const tags = _.get(child, 'comment.tags', []);
const ignore = findIgnores(tags);
return !_.isEmpty(ignore);
}
/**
* Processes API entity pages
* @param hbs
* @param children
*/
private processAPIEntityPage(hbs, children): void {
// themes hbs files paths
const entityPageThemePath = path.join(this.themePath, this.entityPageFile);
const entityPageThemeContent = fs.readFileSync(entityPageThemePath, 'utf8');
_.forEach(children, (entityChild) => {
// 1. pages/
// - create pages using the content
const fullPath = path.join(this.apiOutputPath, `${entityChild.name}.md`);
const template = hbs.compile(entityPageThemeContent);
const compiledPage = template(entityChild);
fs.appendFileSync(fullPath, compiledPage, { flag: 'a' });
});
}
}