visionmedia/jade — pull request #1426 by leptonix (2014-02-21 13:36:48+00:00)
Title: add option to preprocess parse tree

The preprocessor option makes it possible to transform the parse tree before it is compiled to javascript code.

For example, this could be used to expose internal mixin functions:

```
h1 Some template
mixin abc(arg1, arg2)
ul
li= arg1
li= arg2
+abc('xyz', 'zyx')
div ...
```

```
fn = jade.compile template,
preprocessor: (ast, Node) ->
reordered = _.remove ast.nodes, (node) -> node.type == 'Mixin' and not node.call
reordered.push new Node.Code """
if (arguments.length > 1) {
jade_mixins[arguments[0]].apply(jade_mixins, Array.prototype.slice.call(arguments, 1));
return buf.join('');
}
"""
ast.nodes = reordered.concat ast.nodes
ast

```

`fn(locals)` would just call the template.

`fn('abc', 'hello', 'world')` would call the mixin abc with parameters 'hello' and 'world'.