Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing preprocessing plugins #2429

Merged
merged 3 commits into from
Feb 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/less/parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,23 @@ var Parser = function Parser(context, imports, fileInfo) {
// @param [additionalData] An optional map which can contains vars - a map (key, value) of variables to apply
//
parse: function (str, callback, additionalData) {
var root, error = null, globalVars, modifyVars, preText = "";
var root, error = null, globalVars, modifyVars, ignored, preText = "";

globalVars = (additionalData && additionalData.globalVars) ? Parser.serializeVars(additionalData.globalVars) + '\n' : '';
modifyVars = (additionalData && additionalData.modifyVars) ? '\n' + Parser.serializeVars(additionalData.modifyVars) : '';

if (context.pluginManager) {
var preProcessors = context.pluginManager.getPreProcessors();
for (var i = 0; i < preProcessors.length; i++) {
str = preProcessors[i].process(str, { context: context, imports: imports, fileInfo: fileInfo });
}
}

if (globalVars || (additionalData && additionalData.banner)) {
preText = ((additionalData && additionalData.banner) ? additionalData.banner : "") + globalVars;
imports.contentsIgnoredChars[fileInfo.filename] = preText.length;
ignored = imports.contentsIgnoredChars;
ignored[fileInfo.filename] = ignored[fileInfo.filename] || 0;
ignored[fileInfo.filename] += preText.length;
}

str = str.replace(/\r\n?/g, '\n');
Expand Down
27 changes: 27 additions & 0 deletions lib/less/plugin-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
var PluginManager = function(less) {
this.less = less;
this.visitors = [];
this.preProcessors = [];
this.postProcessors = [];
this.installedPlugins = [];
this.fileManagers = [];
Expand Down Expand Up @@ -35,6 +36,20 @@ PluginManager.prototype.addPlugin = function(plugin) {
PluginManager.prototype.addVisitor = function(visitor) {
this.visitors.push(visitor);
};
/**
* Adds a pre processor object
* @param {object} preProcessor
* @param {number} priority - guidelines 1 = before import, 1000 = import, 2000 = after import
*/
PluginManager.prototype.addPreProcessor = function(preProcessor, priority) {
var indexToInsertAt;
for (indexToInsertAt = 0; indexToInsertAt < this.preProcessors.length; indexToInsertAt++) {
if (this.preProcessors[indexToInsertAt].priority >= priority) {
break;
}
}
this.preProcessors.splice(indexToInsertAt, 0, {preProcessor: preProcessor, priority: priority});
};
/**
* Adds a post processor object
* @param {object} postProcessor
Expand All @@ -56,6 +71,18 @@ PluginManager.prototype.addPostProcessor = function(postProcessor, priority) {
PluginManager.prototype.addFileManager = function(manager) {
this.fileManagers.push(manager);
};
/**
*
* @returns {Array}
* @private
*/
PluginManager.prototype.getPreProcessors = function() {
var preProcessors = [];
for (var i = 0; i < this.preProcessors.length; i++) {
preProcessors.push(this.preProcessors[i].preProcessor);
}
return preProcessors;
};
/**
*
* @returns {Array}
Expand Down