forked from apidoc/apidoc-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Plugin Loader. Extend filter, parser, worker for plugin usage. Ad…
…d hooks for found elements.
- Loading branch information
Showing
7 changed files
with
227 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# apiDoc Hooks | ||
|
||
|
||
## parser-find-elements | ||
|
||
Called on each found element. Returns a new list of elements (replace elements). | ||
Used to inject annotationes from an external schema. | ||
|
||
Parameter: `(elements, element, block, filename)` | ||
* {array} elements Found elements in a block without the current element. | ||
* {array} element Contains the source, name (lowercase), sourceName (original), content. | ||
* {string} block Current source block. | ||
* {string} filename Current filename. | ||
|
||
File: `parser.js` | ||
Function: `_findElements` | ||
|
||
|
||
|
||
## parser-find-element-{name} | ||
|
||
Called on each found element and returns the modified element. | ||
Used to modify a specific element. | ||
|
||
{name} is the found element.name (lowercase). | ||
|
||
Parameter: `(element, block, filename)` | ||
* {array} element Contains the source, name (lowercase), sourceName (original), content. | ||
* {string} block Current source block. | ||
* {string} filename Current filename. | ||
|
||
File: `parser.js` | ||
Function: `_findElements` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
var _ = require('lodash'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var util = require('util'); | ||
var glob = require('glob'); | ||
|
||
var app = {}; | ||
|
||
function PluginLoader(_app) { | ||
var self = this; | ||
|
||
// global variables | ||
app = _app; | ||
|
||
// class variables | ||
self.plugins = {}; | ||
|
||
// Try to load global apidoc-plugins (if apidoc is installed locally it tries only local) | ||
this.detectPugins(__dirname); | ||
|
||
// Try to load local apidoc-plugins | ||
this.detectPugins( path.join(process.cwd(), '/node_modules') ); | ||
|
||
if (Object.keys(this.plugins).length === 0) | ||
app.log.debug('No plugins found.'); | ||
|
||
this.loadPlugins(); | ||
} | ||
/** | ||
* Inherit | ||
*/ | ||
util.inherits(PluginLoader, Object); | ||
|
||
/** | ||
* Exports | ||
*/ | ||
module.exports = PluginLoader; | ||
|
||
/** | ||
* Detect modules start with "apidoc-plugin-". | ||
* Search up to root until found a plugin. | ||
*/ | ||
PluginLoader.prototype.detectPugins = function(dir) { | ||
var self = this; | ||
|
||
// Search from the given dir up to root. | ||
// Every dir start with "apidoc-plugin-", because for the tests of apidoc-plugin-test. | ||
var plugins = glob.sync(dir + '/apidoc-plugin-*'); | ||
if (plugins.length === 0) { | ||
dir = path.join(dir, '..'); | ||
if (dir === '/') | ||
return; | ||
return this.detectPugins(dir); | ||
} | ||
|
||
var offset = dir.length + 1; | ||
plugins.forEach( function(plugin) { | ||
var name = plugin.substr(offset); | ||
var filename = path.relative(__dirname, plugin); | ||
app.log.debug('add plugin: ' + name + ', ' + filename); | ||
self.addPlugin(name, plugin); | ||
}); | ||
}; | ||
|
||
/** | ||
* Add Plugin to plugin list. | ||
*/ | ||
PluginLoader.prototype.addPlugin = function(name, filename) { | ||
if (this.plugins[name]) | ||
app.log.debug('overwrite plugin: ' + name + ', ' + this.plugins[name]); | ||
|
||
this.plugins[name] = filename; | ||
}; | ||
|
||
/** | ||
* Load and initialize Plugins. | ||
*/ | ||
PluginLoader.prototype.loadPlugins = function() { | ||
_.forEach(this.plugins, function(filename, name) { | ||
app.log.debug('load plugin: ' + name + ', ' + filename); | ||
var plugin; | ||
try { | ||
plugin = require(filename); | ||
} catch(e) { | ||
} | ||
if (plugin && plugin.init) { | ||
plugin.init(app); | ||
} else { | ||
app.log.debug('Ignored, no init function found.'); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters