From 9fc11a71a663a3e1182effca4b39982cb2a7ffdc Mon Sep 17 00:00:00 2001 From: wucong <1875486458@qq.com> Date: Tue, 20 Oct 2020 23:48:25 +0800 Subject: [PATCH 1/2] expose parseSource --- lib/index.js | 112 +++++++++++++++++++++++--------------- lib/parser.js | 8 +++ test/parse_source_test.js | 88 ++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+), 44 deletions(-) create mode 100644 test/parse_source_test.js diff --git a/lib/index.js b/lib/index.js index 6c11842..b8ff444 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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'); @@ -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) { @@ -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. * @@ -401,6 +424,7 @@ function applyHook(name /* , ...args */) { module.exports = { getSpecificationVersion: getSpecificationVersion, parse : parse, + parseSource : parseSource, setGeneratorInfos : setGeneratorInfos, setLogger : setLogger, setMarkdownParser : setMarkdownParser, diff --git a/lib/parser.js b/lib/parser.js index 4234293..eaed367 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -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); diff --git a/test/parse_source_test.js b/test/parse_source_test.js new file mode 100644 index 0000000..ad736e5 --- /dev/null +++ b/test/parse_source_test.js @@ -0,0 +1,88 @@ +/** + * Test: parseSource + */ +var deepEqual = require('deep-equal'); +var apidoc = require("../lib/index"); + +function log() { +// console.log(arguments); +} + +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(); + }); +}); From ffd99c9a9ddf9cd86fac7b12183e845b6c16a1a1 Mon Sep 17 00:00:00 2001 From: wucong Date: Wed, 28 Oct 2020 23:48:12 +0800 Subject: [PATCH 2/2] remove unused commented line --- test/parse_source_test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/parse_source_test.js b/test/parse_source_test.js index ad736e5..2887994 100644 --- a/test/parse_source_test.js +++ b/test/parse_source_test.js @@ -5,7 +5,6 @@ var deepEqual = require('deep-equal'); var apidoc = require("../lib/index"); function log() { -// console.log(arguments); } var logger = {