-
Notifications
You must be signed in to change notification settings - Fork 203
Functionplugins
Stefan Bergh edited this page Feb 8, 2017
·
4 revisions
To extend dotless through its plugin mechanism 3 different parts are required.
- A function plugin for registering functions
- A function implementation (or many)
- Configuration
Define a named object of type IFunctionPlugin responsible for registering functions
[DisplayName("UtilPlugins")]
public class UtilPlugins : IFunctionPlugin
{
public Dictionary<string, Type> GetFunctions()
{
return new Dictionary<string, Type>
{
{ "rnd", typeof(RandomFunction) }
};
}
}Here a single function named “rnd” is registered mapping it to the RandomFunction class.
Define an object of type Function and implement the Evaluate method.
public class RandomFunction : Function
{
readonly Random random = new Random();
protected override Node Evaluate(Env env)
{
Guard.ExpectNumArguments(2, Arguments.Count(), this, Location);
Guard.ExpectNode<Number>(Arguments[0], this, Arguments[0].Location);
Guard.ExpectNode<Number>(Arguments[1], this, Arguments[1].Location);
var min = Arguments[0] as Number;
var max = Arguments[1] as Number;
return new Number(random.NextDouble() * (max.Value - min.Value) + min.Value, max.Unit);
}
}Here we expect exactly to arguments both of type Number. We return a Number with a random value between the two arguments. The unit of second specified Number is used for our return value.
Register a plugin by adding the following to your web.config using the name of your plugin.
<?xml version="1.0"?>
<configuration>
...
<dotless minifyCss="true|false" cache="true|false">
<plugin name="UtilPlugins" assembly="Company.Plugins" />
</dotless>
...
</configuration>Register a plugin by adding the plugin class to your dotless configuration.
DotlessConfiguration config = ...;
config.Plugins.Add(new GenericPluginConfigurator<UtilPlugins>());input
margin: rnd(100px, 200px)output
margin: 167px /* a random value between 100px and 200px */