diff options
Diffstat (limited to 'node_modules/stylus/lib/functions/use.js')
| -rw-r--r-- | node_modules/stylus/lib/functions/use.js | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/node_modules/stylus/lib/functions/use.js b/node_modules/stylus/lib/functions/use.js new file mode 100644 index 00000000..da28e4d0 --- /dev/null +++ b/node_modules/stylus/lib/functions/use.js @@ -0,0 +1,74 @@ +var utils = require('../utils') + , path = require('path'); + +/** +* Use the given `plugin` +* +* Examples: +* +* use("plugins/add.js") +* +* width add(10, 100) +* // => width: 110 +*/ + +module.exports = function use(plugin, options){ + utils.assertString(plugin, 'plugin'); + + if (options) { + utils.assertType(options, 'object', 'options'); + options = parseObject(options); + } + + // lookup + plugin = plugin.string; + var found = utils.lookup(plugin, this.options.paths, this.options.filename); + if (!found) throw new Error('failed to locate plugin file "' + plugin + '"'); + + // use + var fn = require(path.resolve(found)); + if ('function' != typeof fn) { + throw new Error('plugin "' + plugin + '" does not export a function'); + } + this.renderer.use(fn(options || this.options)); +}; + +/** + * Attempt to parse object node to the javascript object. + * + * @param {Object} obj + * @return {Object} + * @api private + */ + +function parseObject(obj){ + obj = obj.vals; + for (var key in obj) { + var nodes = obj[key].nodes[0].nodes; + if (nodes && nodes.length) { + obj[key] = []; + for (var i = 0, len = nodes.length; i < len; ++i) { + obj[key].push(convert(nodes[i])); + } + } else { + obj[key] = convert(obj[key].first); + } + } + return obj; + + function convert(node){ + switch (node.nodeName) { + case 'object': + return parseObject(node); + case 'boolean': + return node.isTrue; + case 'unit': + return node.type ? node.toString() : +node.val; + case 'string': + case 'literal': + return node.val; + default: + return node.toString(); + } + } +} |
