You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It's not really a problem but it was the only place I found to ask...
I was wondering if there's a nice way to "plugin" new functions into the less.js "tree.functions" ?
For the moment i've hacked into the source, not very nice...
For the record, i need a rgbstr (Color => RGB String) function to make a bg_image_gradient mixin,
something like this:
The line return new(less.tree.Quoted)('"' + str + '"', str); gives you a quoted object, so you'll need to call an attribute or method to get a value. Here's a brief annotation for the source file for Quotedlib/less/tree/quoted.js.
// [1] Defines Quoted attributes; note the paramstree.Quoted=function(str,content,escaped,i){this.escaped=escaped;this.value=content||'';this.quote=str.charAt(0);this.index=i;};// [2] Quoted.toCSS();tree.Quoted.prototype={toCSS: function(){
...
},// [3] Build me this biznass when no method/attribute is calledeval: function(env){
...
// [4] Here's what you returnedreturnnew(tree.Quoted)(this.quote+value+this.quote,value,this.escaped,this.index);}};
Absolutely. The more we could document about the inner workings of the LESS parser, the easier it would be for other JS developers to contribute patches. The ability to do this was news to me until you labeled it.
Is it possible to pass some kind of "extra" object into less during parsing stage that could be used then by function? I want to have a helper asset_path and asset_data_uri in my less file, so now I know how to add helpers, but this helpers are "dependant" on another object (Mincer.Environment in my case and which might not be uniq or singleton).
Activity
cloudhead commentedon Oct 16, 2010
Actually you should just be able to add it directly, after including
less:Try it, it should work.
guillaume86 commentedon Oct 16, 2010
Well, i don't know why, but it doesn't work, the output is still "rgbstr(#ccc)".
Here's my function (it's working when written inside the object):
less.tree.functions.rgbstr = function (color) {
var str = parseInt(color.rgb[0]) + ',' + parseInt(color.rgb[1]) + ',' + parseInt(color.rgb[2]);
return new(less.tree.Quoted)('"' + str + '"', str);
};
guilhermeaiolfi commentedon Jul 29, 2011
Same here. Any tips?
fny commentedon Apr 4, 2012
The line
return new(less.tree.Quoted)('"' + str + '"', str);gives you a quoted object, so you'll need to call an attribute or method to get a value. Here's a brief annotation for the source file for Quotedlib/less/tree/quoted.js.Take this dummy mockup:
Calling
less.tree.functions.rgbstr("hotpink")returns this:Just ask for what you want and its yours:
less.tree.functions.rgbstr("hotpink").value // "hotpink"lukeapage commentedon Jul 17, 2012
Might be nice to put this on the wiki?
matthew-dean commentedon Jul 18, 2012
Absolutely. The more we could document about the inner workings of the LESS parser, the easier it would be for other JS developers to contribute patches. The ability to do this was news to me until you labeled it.
ixti commentedon Jul 24, 2012
Is it possible to pass some kind of "extra" object into less during parsing stage that could be used then by function? I want to have a helper
asset_pathandasset_data_uriin my less file, so now I know how to add helpers, but this helpers are "dependant" on another object (Mincer.Environmentin my case and which might not be uniq or singleton).lukeapage commentedon Jan 2, 2013
more info is now on the homepage - setting less.functions before including less will include those functions into less.
would be nice to add an example, will add this request to lesscss.org - less/old-lesscss.org#54