summaryrefslogtreecommitdiffstats
path: root/lib/models/plugin.js
diff options
context:
space:
mode:
authorSamy Pesse <samypesse@gmail.com>2016-04-30 20:15:08 +0200
committerSamy Pesse <samypesse@gmail.com>2016-04-30 20:15:08 +0200
commit36b49c66c6b75515bc84dd678fd52121a313e8d2 (patch)
treebc7e0f703d4557869943ec7f9495cac7a5027d4f /lib/models/plugin.js
parent87db7cf1d412fa6fbd18e9a7e4f4755f2c0c5547 (diff)
parent80b8e340dadc54377ff40500f86b1de631395806 (diff)
downloadgitbook-36b49c66c6b75515bc84dd678fd52121a313e8d2.zip
gitbook-36b49c66c6b75515bc84dd678fd52121a313e8d2.tar.gz
gitbook-36b49c66c6b75515bc84dd678fd52121a313e8d2.tar.bz2
Merge branch 'fixes'
Diffstat (limited to 'lib/models/plugin.js')
-rw-r--r--lib/models/plugin.js152
1 files changed, 152 insertions, 0 deletions
diff --git a/lib/models/plugin.js b/lib/models/plugin.js
new file mode 100644
index 0000000..dd7bc90
--- /dev/null
+++ b/lib/models/plugin.js
@@ -0,0 +1,152 @@
+var Immutable = require('immutable');
+
+var TemplateBlock = require('./templateBlock');
+var PREFIX = require('../constants/pluginPrefix');
+var DEFAULT_VERSION = '*';
+
+var Plugin = Immutable.Record({
+ name: String(),
+
+ // Requirement version (ex: ">1.0.0")
+ version: String(DEFAULT_VERSION),
+
+ // Path to load this plugin
+ path: String(),
+
+ // Depth of this plugin in the dependency tree
+ depth: Number(0),
+
+ // Content of the "package.json"
+ package: Immutable.Map(),
+
+ // Content of the package itself
+ content: Immutable.Map()
+}, 'Plugin');
+
+Plugin.prototype.getName = function() {
+ return this.get('name');
+};
+
+Plugin.prototype.getPath = function() {
+ return this.get('path');
+};
+
+Plugin.prototype.getVersion = function() {
+ return this.get('version');
+};
+
+Plugin.prototype.getPackage = function() {
+ return this.get('package');
+};
+
+Plugin.prototype.getContent = function() {
+ return this.get('content');
+};
+
+Plugin.prototype.getDepth = function() {
+ return this.get('depth');
+};
+
+/**
+ Return the ID on NPM for this plugin
+
+ @return {String}
+*/
+Plugin.prototype.getNpmID = function() {
+ return Plugin.nameToNpmID(this.getName());
+};
+
+/**
+ Check if a plugin is loaded
+
+ @return {Boolean}
+*/
+Plugin.prototype.isLoaded = function() {
+ return Boolean(this.getPackage().size > 0);
+};
+
+/**
+ Return map of hooks
+ @return {Map<String:Function>}
+*/
+Plugin.prototype.getHooks = function() {
+ return this.getContent().get('hooks') || Immutable.Map();
+};
+
+/**
+ Return infos about resources for a specific type
+
+ @param {String} type
+ @return {Map<String:Mixed>}
+*/
+Plugin.prototype.getResources = function(type) {
+ if (type != 'website' && type != 'ebook') {
+ throw new Error('Invalid assets type ' + type);
+ }
+
+ var content = this.getContent();
+ return (content.get(type)
+ || (type == 'website'? content.get('book') : null)
+ || Immutable.Map());
+};
+
+/**
+ Return map of filters
+ @return {Map<String:Function>}
+*/
+Plugin.prototype.getFilters = function() {
+ return this.getContent().get('filters');
+};
+
+/**
+ Return map of blocks
+ @return {Map<String:TemplateBlock>}
+*/
+Plugin.prototype.getBlocks = function() {
+ var blocks = this.getContent().get('blocks');
+ blocks = blocks || Immutable.Map();
+
+ return blocks
+ .map(function(block, blockName) {
+ return TemplateBlock.create(blockName, block);
+ });
+};
+
+/**
+ Return a specific hook
+
+ @param {String} name
+ @return {Function|undefined}
+*/
+Plugin.prototype.getHook = function(name) {
+ return this.getHooks().get(name);
+};
+
+/**
+ Create a plugin from a string
+
+ @param {String}
+ @return {Plugin}
+*/
+Plugin.createFromString = function(s) {
+ var parts = s.split('@');
+ var name = parts[0];
+ var version = parts.slice(1).join('@');
+
+ return new Plugin({
+ name: name,
+ version: version || DEFAULT_VERSION
+ });
+};
+
+/**
+ Return NPM id for a plugin name
+
+ @param {String}
+ @return {String}
+*/
+Plugin.nameToNpmID = function(s) {
+ return PREFIX + s;
+};
+
+module.exports = Plugin;