Macaron
Macros for CoffeeScript. Try online: http://f.github.io/macaron/
Installation
npm install macaron
Using Macaron Grunt Task
Macaron has a Grunt plugin written by Ahmet Aygün.
npm install grunt-macaron --save-dev
Please read the README file of grunt-macaron for installation instructions.
Overview
Create a Macro library:
# macros.coffee = $tmp = y y = x x = $tmp
Write your Coffee using macros like functions:
# main.coffee x = 1y = 2consolelog "before: x is , y is "swap xyconsolelog "after: x is , y is "
Compile them on Terminal ..:
$ macaron macros.coffee main.coffeebefore: x is 1, y is 2after: x is 2, y is 1
.. Or in your CoffeeScript Code:
# mycoffee.coffee Macaron = require 'macaron'macros = compiledJS = macroscompileFile 'macros.coffee''main.coffee'bare: no consolelog compiledJS
coffee mycoffee.coffee
Usage
macaron [MACROS FILE] [SOURCE FILES...] [COFFEE OPTIONS]
Basic Compilation
It basically replaces the code with the macro code.
// $ macaron examples/macros.coffee examples/source.coffeevar x y _tmp$1;x = 1;y = 2;console; // swap x, y macro starts here_tmp$1 = y;y = x;x = _tmp$1;// ends here console;
Using Code Blocks
You can also use code blocks to use efficiently. To do this, just use splats
of CoffeeScript (...
)
# Create a macro named do_something which accepts a code block = hello = "world" do -> block
Then you can simply call like a callback
# Call the macro with a code block do_something -> consolelog hello
It will generate that code:
var hello; hello = "world"; { return console;};
Composing
You can compose macros.
= hello = "world" world = "hello" swap helloworld # Calling Scope Macro consolelog helloworld
Replace Macros
Replace macros are so stupid ones, you can just pass the code to replace.
The code won't be parsed by Macaron. To define replace macros, use do
keyword.
This macro type doesn't take any parameters since there are no parse process.
macro.strict = do -> "use strict"
You can use do
keyword to call these macros:
do strict
It will generate the output:
"use strict";
Hygiene
You can keep your variables safe using $
prefix on your variables.
# macros.coffee = $tmp = y y = x x = $tmp
# main.coffee x = 2y = 3swap xyconsolelog $tmp
When you run it, it will generate an error:
ReferenceError: $tmp is not defined
Escape Hygiene
You can always disable hygiene using fat-arrow (=>
) or just don't use $
prefix.
= # disabling hygienic variables $tmp = y y = x x = $tmp
Literal Macros
You can use Literal macros using literal
definition keyword. It takes two arguments,
one is a regular expression, another is the function.
literal / is plus / variable = first + second literal /tell the / channel parameters
With these literal macros you can now write some talkative declarations:
"a is 3 plus 4""tell console.log the a"
It will generate the output:
var a;a = 3 + 4;console;
You can wrap matches into quotes using @
(this
) prefix on parameters.
literal /tell my is / userkey= value
You can now use the macros easily:
"tell my name is fka" #=> It will be compiled to `user["name"] = "fka"`
Examples
# macros.coffee = value = variable valueforEach -> $item = arguments0 name = $item eachBlock
Using this macro:
each 123item-> consolelog item
And it'll generate that code:
value = 1 2;value;
Reading from STDIN
You can simply use standard input to run macaron:
echo "x = 1; y = 2; swap x, y; console.log x, y" | macaron -scb examples/macros.coffee | node
Command Line Usage
Usage: coffee ./bin/macaron [MACRO FILE] [SOURCE FILES...] [OPTIONS]
Options:
-b, --bare [default: true]
-c, --compile [default: false]
--concat [string] [default: true]
TODO
- Browserify Transform
License
MIT: f.mit-license.org
The Idea
A fork of davidpadbury/stirred-coffee, based on the blog post about it.