Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

expose parseSource #109

Merged
merged 2 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 68 additions & 44 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,47 +148,11 @@ function getSpecificationVersion() {
* }
*/
function parse(options) {
options = _.defaults({}, options, defaults);

// extend with custom functions
app.filters = _.defaults({}, options.filters, app.filters);
app.languages = _.defaults({}, options.languages, app.languages);
app.parsers = _.defaults({}, options.parsers, app.parsers);
app.workers = _.defaults({}, options.workers, app.workers);
app.hooks = _.defaults({}, options.hooks, app.hooks);

// options
app.options = options;

// generator
app.generator = _.defaults({}, app.generator, defaultGenerator);

// packageInfos
app.packageInfos = _.defaults({}, app.packageInfos, defaultPackageInfos);

var parsedFiles = [];
var parsedFilenames = [];

try {
// Log version information
var filename = path.join(__dirname, '../', './package.json');
var packageJson = JSON.parse( fs.readFileSync( filename , 'utf8') );
app.log.verbose('apidoc-generator name: ' + app.generator.name);
app.log.verbose('apidoc-generator version: ' + app.generator.version);
app.log.verbose('apidoc-core version: ' + packageJson.version);
app.log.verbose('apidoc-spec version: ' + getSpecificationVersion());

new PluginLoader(app);

var parser = new Parser(app);
var worker = new Worker(app);
var filter = new Filter(app);

// Make them available for plugins
app.parser = parser;
app.worker = worker;
app.filter = filter;

initApp(options);
options = app.options;
var parsedFiles = [];
var parsedFilenames = [];
// if input option for source is an array of folders,
// parse each folder in the order provided.
app.log.verbose('run parser');
Expand All @@ -198,23 +162,23 @@ function parse(options) {
// is the folder currently being processed.
var folderOptions = options;
folderOptions.src = path.join(folder, './');
parser.parseFiles(folderOptions, parsedFiles, parsedFilenames);
app.parser.parseFiles(folderOptions, parsedFiles, parsedFilenames);
});
}
else {
// if the input option for source is a single folder, parse as usual.
options.src = path.join(options.src, './');
parser.parseFiles(options, parsedFiles, parsedFilenames);
app.parser.parseFiles(options, parsedFiles, parsedFilenames);
}

if (parsedFiles.length > 0) {
// process transformations and assignments
app.log.verbose('run worker');
worker.process(parsedFiles, parsedFilenames, app.packageInfos);
app.worker.process(parsedFiles, parsedFilenames, app.packageInfos);

// cleanup
app.log.verbose('run filter');
var blocks = filter.process(parsedFiles, parsedFilenames);
var blocks = app.filter.process(parsedFiles, parsedFilenames);

// sort by group ASC, name ASC, version DESC
blocks.sort(function(a, b) {
Expand Down Expand Up @@ -299,6 +263,65 @@ function parse(options) {
}
}

/**
* parseSource
*
* @param {string} source the source code string.
* @param {Object} options Overwrite default options.
*/
function parseSource(source, options) {
try {
initApp(options);
return app.parser.parseSource(source, app.options.encoding, app.options.filename);
} catch (e) {
app.log.error(e.message);
}
}

/**
* initApp
*
* @param {Object} options Overwrite default options.
*/
function initApp(options) {

options = _.defaults({}, options, defaults);
// extend with custom functions
app.filters = _.defaults({}, options.filters, app.filters);
app.languages = _.defaults({}, options.languages, app.languages);
app.parsers = _.defaults({}, options.parsers, app.parsers);
app.workers = _.defaults({}, options.workers, app.workers);
app.hooks = _.defaults({}, options.hooks, app.hooks);

// options
app.options = options;

// generator
app.generator = _.defaults({}, app.generator, defaultGenerator);

// packageInfos
app.packageInfos = _.defaults({}, app.packageInfos, defaultPackageInfos);

// Log version information
var filename = path.join(__dirname, '../', './package.json');
var packageJson = JSON.parse( fs.readFileSync( filename , 'utf8') );
app.log.verbose('apidoc-generator name: ' + app.generator.name);
app.log.verbose('apidoc-generator version: ' + app.generator.version);
app.log.verbose('apidoc-core version: ' + packageJson.version);
app.log.verbose('apidoc-spec version: ' + getSpecificationVersion());

new PluginLoader(app);

var parser = new Parser(app);
var worker = new Worker(app);
var filter = new Filter(app);

// Make them available for plugins
app.parser = parser;
app.worker = worker;
app.filter = filter;
}

/**
* Set generator informations.
*
Expand Down Expand Up @@ -401,6 +424,7 @@ function applyHook(name /* , ...args */) {
module.exports = {
getSpecificationVersion: getSpecificationVersion,
parse : parse,
parseSource : parseSource,
setGeneratorInfos : setGeneratorInfos,
setLogger : setLogger,
setMarkdownParser : setMarkdownParser,
Expand Down
8 changes: 8 additions & 0 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ Parser.prototype.parseFile = function(filename, encoding) {
// TODO: Not sure if this is correct. Without skipDecodeWarning we got string errors
// https://github.com/apidoc/apidoc-core/pull/25
var fileContent = fs.readFileSync(filename, { encoding: 'binary' });
return self.parseSource(fileContent,encoding,filename);
};

/**
* Execute Sourceparsing
*/
Parser.prototype.parseSource = function(fileContent,encoding,filename) {
var self = this;
iconv.skipDecodeWarning = true;
self.src = iconv.decode(fileContent, encoding);
app.log.debug('size: ' + self.src.length);
Expand Down
87 changes: 87 additions & 0 deletions test/parse_source_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Test: parseSource
*/
var deepEqual = require('deep-equal');
var apidoc = require("../lib/index");

function log() {
}

var logger = {
debug: log,
verbose: log,
info: log,
warn: log,
error: log,
};

describe("parseSource", function () {
var testCases = [
{
source:
"/**" +
"\n * @api {post} /api/school/students/:studentId/cloth " +
"\n * @apiName createCloth " +
"\n * @apiGroup cloth " +
"\n * @apiParam (body) {String} [name] " +
"\n * @apiSuccess {Number} code 200 " +
"\n * @apiSuccessExample {json} Success-Response: " +
"\n * { " +
"\n * status: 200 " +
"\n * } " +
"\n*/ ",
expected: {
global: {},
local: {
type: "post",
url: "/api/school/students/:studentId/cloth",
title: "",
name: "createCloth",
group: "cloth",
parameter: {
fields: {
body: [
{
group: "body",
type: "String",
field: "name",
optional: true,
description: "",
},
],
},
},
success: {
fields: {
"Success 200": [
{
group: "Success 200",
type: "Number",
optional: false,
field: "code",
description: "200",
},
],
},
examples: [
{
title: "Success-Response: ",
content: "{ \n status: 200 \n}",
type: "json",
},
],
},
},
index: 1,
},
},
];
it("case 1: should pass all test cases", function (done) {
testCases.forEach(function (testCase) {
apidoc.setLogger(logger);
var parsed = apidoc.parseSource(Buffer.from(testCase.source));
deepEqual(parsed, testCase.expected);
});
done();
});
});