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.
This commit is contained in:
jackwanders
2014-12-01 19:37:13 -05:00
parent 94a6501ade
commit cf54e01743
3 changed files with 70 additions and 35 deletions

View File

@@ -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'),

61
lib/less/parse.js Normal file
View File

@@ -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;
};

View File

@@ -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;
};