const stream = require('stream');
const reader = require('./reader.js');
const header = `
## Table of Content
Function |
Description |
Documentation |
`;
const footer = `
`
function JSDocHeaderId({extraReturn, returnType, functionName, functionArgs}) {
code = '';
// function name
if (extraReturn) {
code += `[${returnType}, extra] = cephes.${functionName}(`;
} else {
code += `${returnType} = cephes.${functionName}(`;
}
// function arguments
for (const {type, isPointer, isArray, fullType, name} of functionArgs) {
if (isPointer) continue;
if (isArray && type === 'double') {
code += `${name}: Float64Array, `;
} else if (!isArray) {
code += `${name}: ${type}, `;
} else {
throw new Error(`unsupported type: ${fullType}`);
}
}
// Remove training comma
code = code.slice(0, -2);
// finish function header
code += ')';
return code.replace(/[ ]/g, '-').replace(/[=,.():\[\]]/g, '').toLowerCase();
}
class TocGenerator extends stream.Transform {
constructor() {
super({ objectMode: true });
this.push(header);
this._previuseCategory = '';
}
_transform(data, encoding, done) {
const {
category, description,
returnType, functionName, functionArgs
} = data;
// Check if there is extra data returned
const extraReturn = functionArgs.some((arg) => arg.isPointer);
//
// Start code generation
//
let code = '';
//
// Add category header
//
if (this._previuseCategory !== category) {
this._previuseCategory = category;
code += ' \n';
code += ` ${category} | \n`;
code += '
\n';
}
//
// function
//
code += ' \n';
// function name
code += ' ';
code += `${functionName}(`;
// function arguments
for (const {isPointer, name} of functionArgs) {
if (isPointer) continue;
code += `${name}, `;
}
// Remove training comma
code = code.slice(0, -2);
// finish function
code += ')';
code += ' | \n';
//
// Description
//
code += ` ${description} | \n`;
//
// Documentation
//
code += ' ';
code += `c-doc`;
code += ' • ';
code += `js-doc`;
code += ' | \n';
code += '
\n';
this.push(code);
done(null);
}
_flush(done) {
this.push(footer);
done(null);
}
}
process.stdin
.pipe(reader())
.pipe(new TocGenerator())
.pipe(process.stdout)