Skip to content

Commit

Permalink
Expose Less parsing as a top level feature of the less package
Browse files Browse the repository at this point in the history
Converting a Less stylesheet into an AST is a valuable piece of
functionality, and worthy of being easily accessible to consumers
of less.js.
  • Loading branch information
jackwanders committed Dec 2, 2014
1 parent 94a6501 commit cf54e01
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 35 deletions.
1 change: 1 addition & 0 deletions lib/less/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
61 changes: 61 additions & 0 deletions lib/less/parse.js
Original file line number Diff line number Diff line change
@@ -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;
};
43 changes: 8 additions & 35 deletions lib/less/render.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
};

0 comments on commit cf54e01

Please sign in to comment.