2017-07-11 15:57:26 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const markdownToc = require('markdown-toc');
|
|
|
|
const path = require('path');
|
|
|
|
const Documentation = require('./Documentation');
|
|
|
|
const commonmark = require('commonmark');
|
|
|
|
|
|
|
|
class MDOutline {
|
|
|
|
/**
|
2017-07-12 18:42:36 +00:00
|
|
|
* @param {!Page} page
|
2017-07-11 15:57:26 +00:00
|
|
|
* @param {string} text
|
|
|
|
* @return {!MDOutline}
|
|
|
|
*/
|
2017-07-12 18:42:36 +00:00
|
|
|
static async create(page, text) {
|
2017-07-11 15:57:26 +00:00
|
|
|
// Render markdown as HTML.
|
|
|
|
const reader = new commonmark.Parser();
|
|
|
|
const parsed = reader.parse(text);
|
|
|
|
const writer = new commonmark.HtmlRenderer();
|
|
|
|
const html = writer.render(parsed);
|
|
|
|
|
|
|
|
// Extract headings.
|
|
|
|
await page.setContent(html);
|
|
|
|
const classes = await page.evaluate(() => {
|
|
|
|
let classes = [];
|
|
|
|
let currentClass = {};
|
2017-07-12 15:01:21 +00:00
|
|
|
let member = {};
|
2017-07-11 15:57:26 +00:00
|
|
|
for (let element of document.body.querySelectorAll('h3, h4, h4 + ul > li')) {
|
|
|
|
if (element.matches('h3')) {
|
|
|
|
currentClass = {
|
|
|
|
name: element.textContent,
|
2017-07-12 15:01:21 +00:00
|
|
|
members: [],
|
2017-07-11 15:57:26 +00:00
|
|
|
};
|
|
|
|
classes.push(currentClass);
|
|
|
|
} else if (element.matches('h4')) {
|
2017-07-12 15:01:21 +00:00
|
|
|
member = {
|
2017-07-11 15:57:26 +00:00
|
|
|
name: element.textContent,
|
2017-07-13 22:54:37 +00:00
|
|
|
args: [],
|
|
|
|
hasReturn: false
|
2017-07-11 15:57:26 +00:00
|
|
|
};
|
2017-07-12 15:01:21 +00:00
|
|
|
currentClass.members.push(member);
|
2017-07-11 15:57:26 +00:00
|
|
|
} else if (element.matches('li') && element.firstChild.matches && element.firstChild.matches('code')) {
|
2017-07-12 15:01:21 +00:00
|
|
|
member.args.push(element.firstChild.textContent);
|
2017-07-13 22:54:37 +00:00
|
|
|
} else if (element.matches('li') && element.firstChild.nodeType === Element.TEXT_NODE && element.firstChild.textContent.startsWith('returns: ')) {
|
|
|
|
member.hasReturn = true;
|
2017-07-11 15:57:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return classes;
|
|
|
|
});
|
|
|
|
return new MDOutline(classes);
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(classes) {
|
|
|
|
this.classes = [];
|
|
|
|
this.errors = [];
|
|
|
|
const classHeading = /^class: (\w+)$/;
|
|
|
|
const constructorRegex = /^new (\w+)\((.*)\)$/;
|
|
|
|
const methodRegex = /^(\w+)\.(\w+)\((.*)\)$/;
|
2017-07-12 15:01:21 +00:00
|
|
|
const propertyRegex = /^(\w+)\.(\w+)$/;
|
2017-07-11 15:57:26 +00:00
|
|
|
let currentClassName = null;
|
2017-07-14 05:52:02 +00:00
|
|
|
let currentClassMembers = [];
|
2017-07-11 15:57:26 +00:00
|
|
|
for (const cls of classes) {
|
|
|
|
let match = cls.name.match(classHeading);
|
2017-07-12 18:42:36 +00:00
|
|
|
if (!match)
|
|
|
|
continue;
|
2017-07-11 15:57:26 +00:00
|
|
|
currentClassName = match[1];
|
2017-07-12 15:01:21 +00:00
|
|
|
for (let member of cls.members) {
|
|
|
|
if (constructorRegex.test(member.name)) {
|
|
|
|
let match = member.name.match(constructorRegex);
|
|
|
|
handleMethod.call(this, member, match[1], 'constructor', match[2]);
|
|
|
|
} else if (methodRegex.test(member.name)) {
|
|
|
|
let match = member.name.match(methodRegex);
|
|
|
|
handleMethod.call(this, member, match[1], match[2], match[3]);
|
|
|
|
} else if (propertyRegex.test(member.name)) {
|
|
|
|
let match = member.name.match(propertyRegex);
|
|
|
|
handleProperty.call(this, member, match[1], match[2]);
|
2017-07-11 15:57:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
flushClassIfNeeded.call(this);
|
|
|
|
}
|
|
|
|
|
2017-07-12 15:01:21 +00:00
|
|
|
function handleMethod(member, className, methodName, parameters) {
|
|
|
|
if (!currentClassName || !className || !methodName || className.toLowerCase() !== currentClassName.toLowerCase()) {
|
|
|
|
this.errors.push(`Failed to process header as method: ${member.name}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
parameters = parameters.trim().replace(/[\[\]]/g, '');
|
|
|
|
if (parameters !== member.args.join(', '))
|
|
|
|
this.errors.push(`Heading arguments for "${member.name}" do not match described ones, i.e. "${parameters}" != "${member.args.join(', ')}"`);
|
|
|
|
let args = member.args.map(arg => new Documentation.Argument(arg));
|
2017-07-13 22:54:37 +00:00
|
|
|
let method = Documentation.Member.createMethod(methodName, args, member.hasReturn, false);
|
2017-07-14 05:52:02 +00:00
|
|
|
currentClassMembers.push(method);
|
2017-07-12 15:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleProperty(member, className, propertyName) {
|
|
|
|
if (!currentClassName || !className || !propertyName || className.toLowerCase() !== currentClassName.toLowerCase()) {
|
|
|
|
this.errors.push(`Failed to process header as property: ${member.name}`);
|
|
|
|
return;
|
|
|
|
}
|
2017-07-14 05:52:02 +00:00
|
|
|
currentClassMembers.push(Documentation.Member.createProperty(propertyName));
|
2017-07-12 15:01:21 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 15:57:26 +00:00
|
|
|
function flushClassIfNeeded() {
|
|
|
|
if (currentClassName === null)
|
|
|
|
return;
|
2017-07-14 05:52:02 +00:00
|
|
|
this.classes.push(new Documentation.Class(currentClassName, currentClassMembers));
|
2017-07-11 15:57:26 +00:00
|
|
|
currentClassName = null;
|
2017-07-14 05:52:02 +00:00
|
|
|
currentClassMembers = [];
|
2017-07-11 15:57:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-12 18:42:36 +00:00
|
|
|
* @param {!Page} page
|
2017-07-11 15:57:26 +00:00
|
|
|
* @param {!Array<string>} dirPath
|
|
|
|
* @return {!Promise<{documentation: !Documentation, errors: !Array<string>}>}
|
|
|
|
*/
|
2017-07-12 18:42:36 +00:00
|
|
|
module.exports = async function(page, dirPath) {
|
2017-07-11 15:57:26 +00:00
|
|
|
let filePaths = fs.readdirSync(dirPath)
|
|
|
|
.filter(fileName => fileName.endsWith('.md'))
|
|
|
|
.map(fileName => path.join(dirPath, fileName));
|
|
|
|
let classes = [];
|
|
|
|
let errors = [];
|
|
|
|
for (let filePath of filePaths) {
|
|
|
|
const markdownText = fs.readFileSync(filePath, 'utf8');
|
|
|
|
const newMarkdownText = markdownToc.insert(markdownText);
|
|
|
|
if (markdownText !== newMarkdownText)
|
|
|
|
errors.push('Markdown TOC is outdated, run `yarn generate-toc`');
|
2017-07-12 18:42:36 +00:00
|
|
|
let outline = await MDOutline.create(page, markdownText);
|
2017-07-11 15:57:26 +00:00
|
|
|
classes.push(...outline.classes);
|
|
|
|
errors.push(...outline.errors);
|
|
|
|
}
|
|
|
|
const documentation = new Documentation(classes);
|
|
|
|
return { documentation, errors };
|
|
|
|
};
|
|
|
|
|