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

allow parser to inject hooks #27

Closed
wants to merge 4 commits into from
Closed
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
41 changes: 36 additions & 5 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var _ = require('lodash');
var fs = require('fs');
var path = require('path');
var util = require('util');
var glob = require('glob');

var findFiles = require('./utils/find_files');

Expand All @@ -19,6 +20,7 @@ function Parser(_app) {
// class variables
self.languages = {};
self.parsers = {};
self.hooks = {};
self.parsedFileElements = [];
self.parsedFiles = [];
self.countDeprecated = {};
Expand All @@ -38,6 +40,9 @@ function Parser(_app) {
app.log.debug('load parser: ' + parser + ', ' + filename);
self.addParser(parser, require(filename));
});

// load hooks
self.addHooks(__dirname);
}

/**
Expand All @@ -64,6 +69,26 @@ Parser.prototype.addParser = function(name, parser) {
this.parsers[name] = parser;
};

Parser.prototype.addHooks = function (dir) {
var self = this;
if (fs.readdirSync(dir).indexOf('node_modules') === -1 ) { return this.addHooks( path.join(dir,'..') ); }

var hooks = glob.sync(dir+'/node_modules/apidoc-plugin-*');
var offset = dir.length + '/node_modules/apidoc-plugin-'.length;
hooks.forEach(function(hook) {
var id = 'api' + hook.substr(offset); // 'apidoc-plugin-'.length = 12
app.log.debug('load hook: ' + id + ', ' +path.relative(__dirname, hook) + ', ');
self.addHook(id, require(hook));
});
};

/**
* Add a Hook
*/
Parser.prototype.addHook = function(name, hook) {
this.hooks[name] = hook;
};

/**
* Parse files in specified folder
*
Expand Down Expand Up @@ -119,7 +144,7 @@ Parser.prototype.parseFile = function(filename) {

// determine elements in blocks
self.elements = self.blocks.map(function(block, i) {
var elements = self._findElements(block);
var elements = self._findElements(block, filename);
app.log.debug('count elements in block ' + i + ': ' + elements.length);
return elements;
});
Expand Down Expand Up @@ -412,7 +437,8 @@ Parser.prototype._findBlockWithApiGetIndex = function(blocks) {
/**
* Get Elements of Blocks
*/
Parser.prototype._findElements = function(block) {
Parser.prototype._findElements = function(block, filename) {
var self = this;
var elements = [];

// Replace Linebreak with Unicode
Expand All @@ -432,9 +458,14 @@ Parser.prototype._findElements = function(block) {
// reverse Unicode Linebreaks
element.content = element.content.replace(/\uffff/g, '\n');
element.source = element.source.replace(/\uffff/g, '\n');

elements.push(element);


if (self.hooks[element.name]) {
var blockExtend = self.hooks[element.name](element, filename);
elements = elements.concat(self._findElements(blockExtend, filename));
} else {
elements.push(element);
}

// next Match
matches = elementsRegExp.exec(block);
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"node": ">= 0.10.0"
},
"dependencies": {
"glob": "~7.0.3",
"lodash": "~4.5.0",
"semver": "~5.1.0",
"wrench": "~1.5.8"
Expand Down