From cf54e01743a92ec05767edcd2dd5352ec5a0d9c6 Mon Sep 17 00:00:00 2001 From: jackwanders Date: Mon, 1 Dec 2014 19:37:13 -0500 Subject: [PATCH] Expose Less parsing as a top level feature of the less package Converting a Less stylesheet into an AST is a valuable piece of functionality, and worthy of being easily accessible to consumers of less.js. --- lib/less/index.js | 1 + lib/less/parse.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++ lib/less/render.js | 43 ++++++-------------------------- 3 files changed, 70 insertions(+), 35 deletions(-) create mode 100644 lib/less/parse.js diff --git a/lib/less/index.js b/lib/less/index.js index 51fd54cd..226831dd 100644 --- a/lib/less/index.js +++ b/lib/less/index.js @@ -17,6 +17,7 @@ module.exports = function(environment, fileManagers) { ParseTree: (ParseTree = require('./parse-tree')(SourceMapBuilder)), ImportManager: (ImportManager = require('./import-manager')(environment)), render: require("./render")(environment, ParseTree, ImportManager), + parse: require("./parse")(environment, ParseTree, ImportManager), LessError: require('./less-error'), transformTree: require('./transform-tree'), utils: require('./utils'), diff --git a/lib/less/parse.js b/lib/less/parse.js new file mode 100644 index 00000000..09918240 --- /dev/null +++ b/lib/less/parse.js @@ -0,0 +1,61 @@ +var PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise, + contexts = require("./contexts"), + Parser = require('./parser/parser'), + PluginManager = require('./plugin-manager'); + +module.exports = function(environment, ParseTree, ImportManager) { + var parse = function (input, options, callback) { + options = options || {}; + + if (typeof(options) === 'function') { + callback = options; + options = {}; + } + + if (!callback) { + var self = this; + return new PromiseConstructor(function (resolve, reject) { + parse.call(self, input, options, function(err, output) { + if (err) { + reject(err); + } else { + resolve(output); + } + }); + }); + } else { + var context, + rootFileInfo, + pluginManager = new PluginManager(this); + + pluginManager.addPlugins(options.plugins); + options.pluginManager = pluginManager; + + context = new contexts.Parse(options); + + if (options.rootFileInfo) { + rootFileInfo = options.rootFileInfo; + } else { + var filename = options.filename || "input"; + var entryPath = filename.replace(/[^\/\\]*$/, ""); + rootFileInfo = { + filename: filename, + relativeUrls: context.relativeUrls, + rootpath: context.rootpath || "", + currentDirectory: entryPath, + entryPath: entryPath, + rootFilename: filename + }; + } + + var imports = new ImportManager(context, rootFileInfo); + + new Parser(context, imports, rootFileInfo) + .parse(input, function (e, root) { + if (e) { return callback(e); } + callback(null, root, imports, options); + }, options); + } + }; + return parse; +}; diff --git a/lib/less/render.js b/lib/less/render.js index 843ddc47..b4565a59 100644 --- a/lib/less/render.js +++ b/lib/less/render.js @@ -1,11 +1,8 @@ -var PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise, - contexts = require("./contexts"), - Parser = require('./parser/parser'), - PluginManager = require('./plugin-manager'); +var PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise; module.exports = function(environment, ParseTree, ImportManager) { var render = function (input, options, callback) { - options = options || {}; + var parse = require('./parse')(environment, ParseTree, ImportManager); if (typeof(options) === 'function') { callback = options; @@ -24,44 +21,20 @@ module.exports = function(environment, ParseTree, ImportManager) { }); }); } else { - var context, - rootFileInfo, - pluginManager = new PluginManager(this); + parse(input, options, function(err, root, imports, options) { + if (err) { return callback(err); } - pluginManager.addPlugins(options.plugins); - options.pluginManager = pluginManager; - - context = new contexts.Parse(options); - - if (options.rootFileInfo) { - rootFileInfo = options.rootFileInfo; - } else { - var filename = options.filename || "input"; - var entryPath = filename.replace(/[^\/\\]*$/, ""); - rootFileInfo = { - filename: filename, - relativeUrls: context.relativeUrls, - rootpath: context.rootpath || "", - currentDirectory: entryPath, - entryPath: entryPath, - rootFilename: filename - }; - } - - var imports = new ImportManager(context, rootFileInfo); - - new Parser(context, imports, rootFileInfo) - .parse(input, function (e, root) { - if (e) { return callback(e); } var result; try { var parseTree = new ParseTree(root, imports); result = parseTree.toCSS(options); } - catch (err) { return callback( err); } + catch (err) { return callback(err); } + callback(null, result); - }, options); + }); } }; + return render; };