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

Commit 9fc11a7

Browse files
wucongwoodgear
authored andcommitted
expose parseSource
1 parent 8ead7e7 commit 9fc11a7

File tree

3 files changed

+164
-44
lines changed

3 files changed

+164
-44
lines changed

lib/index.js

Lines changed: 68 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -148,47 +148,11 @@ function getSpecificationVersion() {
148148
* }
149149
*/
150150
function parse(options) {
151-
options = _.defaults({}, options, defaults);
152-
153-
// extend with custom functions
154-
app.filters = _.defaults({}, options.filters, app.filters);
155-
app.languages = _.defaults({}, options.languages, app.languages);
156-
app.parsers = _.defaults({}, options.parsers, app.parsers);
157-
app.workers = _.defaults({}, options.workers, app.workers);
158-
app.hooks = _.defaults({}, options.hooks, app.hooks);
159-
160-
// options
161-
app.options = options;
162-
163-
// generator
164-
app.generator = _.defaults({}, app.generator, defaultGenerator);
165-
166-
// packageInfos
167-
app.packageInfos = _.defaults({}, app.packageInfos, defaultPackageInfos);
168-
169-
var parsedFiles = [];
170-
var parsedFilenames = [];
171-
172151
try {
173-
// Log version information
174-
var filename = path.join(__dirname, '../', './package.json');
175-
var packageJson = JSON.parse( fs.readFileSync( filename , 'utf8') );
176-
app.log.verbose('apidoc-generator name: ' + app.generator.name);
177-
app.log.verbose('apidoc-generator version: ' + app.generator.version);
178-
app.log.verbose('apidoc-core version: ' + packageJson.version);
179-
app.log.verbose('apidoc-spec version: ' + getSpecificationVersion());
180-
181-
new PluginLoader(app);
182-
183-
var parser = new Parser(app);
184-
var worker = new Worker(app);
185-
var filter = new Filter(app);
186-
187-
// Make them available for plugins
188-
app.parser = parser;
189-
app.worker = worker;
190-
app.filter = filter;
191-
152+
initApp(options);
153+
options = app.options;
154+
var parsedFiles = [];
155+
var parsedFilenames = [];
192156
// if input option for source is an array of folders,
193157
// parse each folder in the order provided.
194158
app.log.verbose('run parser');
@@ -198,23 +162,23 @@ function parse(options) {
198162
// is the folder currently being processed.
199163
var folderOptions = options;
200164
folderOptions.src = path.join(folder, './');
201-
parser.parseFiles(folderOptions, parsedFiles, parsedFilenames);
165+
app.parser.parseFiles(folderOptions, parsedFiles, parsedFilenames);
202166
});
203167
}
204168
else {
205169
// if the input option for source is a single folder, parse as usual.
206170
options.src = path.join(options.src, './');
207-
parser.parseFiles(options, parsedFiles, parsedFilenames);
171+
app.parser.parseFiles(options, parsedFiles, parsedFilenames);
208172
}
209173

210174
if (parsedFiles.length > 0) {
211175
// process transformations and assignments
212176
app.log.verbose('run worker');
213-
worker.process(parsedFiles, parsedFilenames, app.packageInfos);
177+
app.worker.process(parsedFiles, parsedFilenames, app.packageInfos);
214178

215179
// cleanup
216180
app.log.verbose('run filter');
217-
var blocks = filter.process(parsedFiles, parsedFilenames);
181+
var blocks = app.filter.process(parsedFiles, parsedFilenames);
218182

219183
// sort by group ASC, name ASC, version DESC
220184
blocks.sort(function(a, b) {
@@ -299,6 +263,65 @@ function parse(options) {
299263
}
300264
}
301265

266+
/**
267+
* parseSource
268+
*
269+
* @param {string} source the source code string.
270+
* @param {Object} options Overwrite default options.
271+
*/
272+
function parseSource(source, options) {
273+
try {
274+
initApp(options);
275+
return app.parser.parseSource(source, app.options.encoding, app.options.filename);
276+
} catch (e) {
277+
app.log.error(e.message);
278+
}
279+
}
280+
281+
/**
282+
* initApp
283+
*
284+
* @param {Object} options Overwrite default options.
285+
*/
286+
function initApp(options) {
287+
288+
options = _.defaults({}, options, defaults);
289+
// extend with custom functions
290+
app.filters = _.defaults({}, options.filters, app.filters);
291+
app.languages = _.defaults({}, options.languages, app.languages);
292+
app.parsers = _.defaults({}, options.parsers, app.parsers);
293+
app.workers = _.defaults({}, options.workers, app.workers);
294+
app.hooks = _.defaults({}, options.hooks, app.hooks);
295+
296+
// options
297+
app.options = options;
298+
299+
// generator
300+
app.generator = _.defaults({}, app.generator, defaultGenerator);
301+
302+
// packageInfos
303+
app.packageInfos = _.defaults({}, app.packageInfos, defaultPackageInfos);
304+
305+
// Log version information
306+
var filename = path.join(__dirname, '../', './package.json');
307+
var packageJson = JSON.parse( fs.readFileSync( filename , 'utf8') );
308+
app.log.verbose('apidoc-generator name: ' + app.generator.name);
309+
app.log.verbose('apidoc-generator version: ' + app.generator.version);
310+
app.log.verbose('apidoc-core version: ' + packageJson.version);
311+
app.log.verbose('apidoc-spec version: ' + getSpecificationVersion());
312+
313+
new PluginLoader(app);
314+
315+
var parser = new Parser(app);
316+
var worker = new Worker(app);
317+
var filter = new Filter(app);
318+
319+
// Make them available for plugins
320+
app.parser = parser;
321+
app.worker = worker;
322+
app.filter = filter;
323+
}
324+
302325
/**
303326
* Set generator informations.
304327
*
@@ -401,6 +424,7 @@ function applyHook(name /* , ...args */) {
401424
module.exports = {
402425
getSpecificationVersion: getSpecificationVersion,
403426
parse : parse,
427+
parseSource : parseSource,
404428
setGeneratorInfos : setGeneratorInfos,
405429
setLogger : setLogger,
406430
setMarkdownParser : setMarkdownParser,

lib/parser.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ Parser.prototype.parseFile = function(filename, encoding) {
125125
// TODO: Not sure if this is correct. Without skipDecodeWarning we got string errors
126126
// https://github.com/apidoc/apidoc-core/pull/25
127127
var fileContent = fs.readFileSync(filename, { encoding: 'binary' });
128+
return self.parseSource(fileContent,encoding,filename);
129+
};
130+
131+
/**
132+
* Execute Sourceparsing
133+
*/
134+
Parser.prototype.parseSource = function(fileContent,encoding,filename) {
135+
var self = this;
128136
iconv.skipDecodeWarning = true;
129137
self.src = iconv.decode(fileContent, encoding);
130138
app.log.debug('size: ' + self.src.length);

test/parse_source_test.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Test: parseSource
3+
*/
4+
var deepEqual = require('deep-equal');
5+
var apidoc = require("../lib/index");
6+
7+
function log() {
8+
// console.log(arguments);
9+
}
10+
11+
var logger = {
12+
debug: log,
13+
verbose: log,
14+
info: log,
15+
warn: log,
16+
error: log,
17+
};
18+
19+
describe("parseSource", function () {
20+
var testCases = [
21+
{
22+
source:
23+
"/**" +
24+
"\n * @api {post} /api/school/students/:studentId/cloth " +
25+
"\n * @apiName createCloth " +
26+
"\n * @apiGroup cloth " +
27+
"\n * @apiParam (body) {String} [name] " +
28+
"\n * @apiSuccess {Number} code 200 " +
29+
"\n * @apiSuccessExample {json} Success-Response: " +
30+
"\n * { " +
31+
"\n * status: 200 " +
32+
"\n * } " +
33+
"\n*/ ",
34+
expected: {
35+
global: {},
36+
local: {
37+
type: "post",
38+
url: "/api/school/students/:studentId/cloth",
39+
title: "",
40+
name: "createCloth",
41+
group: "cloth",
42+
parameter: {
43+
fields: {
44+
body: [
45+
{
46+
group: "body",
47+
type: "String",
48+
field: "name",
49+
optional: true,
50+
description: "",
51+
},
52+
],
53+
},
54+
},
55+
success: {
56+
fields: {
57+
"Success 200": [
58+
{
59+
group: "Success 200",
60+
type: "Number",
61+
optional: false,
62+
field: "code",
63+
description: "200",
64+
},
65+
],
66+
},
67+
examples: [
68+
{
69+
title: "Success-Response: ",
70+
content: "{ \n status: 200 \n}",
71+
type: "json",
72+
},
73+
],
74+
},
75+
},
76+
index: 1,
77+
},
78+
},
79+
];
80+
it("case 1: should pass all test cases", function (done) {
81+
testCases.forEach(function (testCase) {
82+
apidoc.setLogger(logger);
83+
var parsed = apidoc.parseSource(Buffer.from(testCase.source));
84+
deepEqual(parsed, testCase.expected);
85+
});
86+
done();
87+
});
88+
});

0 commit comments

Comments
 (0)