Updating documentation with endtime's suggestions.

This commit is contained in:
Jeremy Ashkenas 2010-07-17 10:28:06 -04:00
parent ec2d358ae3
commit 9814fc2e1c
14 changed files with 132 additions and 116 deletions

View File

@ -17,7 +17,7 @@ EOS
desc "Build the documentation page"
task :doc do
source = 'documentation/index.html.erb'
child = fork { exec "bin/coffee -c documentation/coffee/*.coffee -o documentation/js -w" }
child = fork { exec "bin/coffee -cw -o documentation/js documentation/coffee/*.coffee" }
at_exit { Process.kill("INT", child) }
Signal.trap("INT") { exit }
loop do

View File

@ -0,0 +1,4 @@
###
CoffeeScript Compiler v0.7.2
Released under the MIT License
###

View File

@ -1,3 +1,9 @@
task 'test', 'run each of the unit tests', ->
for test in files
fs.readFile test, (err, code) -> eval coffee.compile code
fs: require 'fs'
option '-o', '--output [DIR]', 'directory for compiled code'
task 'build:parser', 'rebuild the Jison parser', ->
require 'jison'
code: require('./lib/grammar').parser.generate()
dir: options.output or 'lib'
fs.writeFile "$dir/parser.js", code

View File

@ -59,7 +59,7 @@
<a href="#try">Try/Catch/Finally</a>
<a href="#comparisons">Chained Comparisons</a>
<a href="#interpolation">String and RegExp Interpolation</a>
<a href="#strings">Multiline Strings and Heredocs</a>
<a href="#strings">Multiline Strings, Heredocs, and Block Comments</a>
<a href="#cake">Cake, and Cakefiles</a>
<a href="#scripts">"text/coffeescript" Script Tags</a>
<a href="#resources">Resources</a>
@ -778,7 +778,7 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
<p>
<span id="strings" class="bookmark"></span>
<b class="header">Multiline Strings and Heredocs</b>
<b class="header">Multiline Strings, Heredocs, and Block Comments</b>
Multiline strings are allowed in CoffeeScript.
</p>
<%= code_for('strings', 'mobyDick') %>
@ -792,6 +792,13 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
<p>
Double-quoted heredocs, like double-quoted strings, allow interpolation.
</p>
<p>
Sometimes you'd like to pass a block comment through to the generated
JavaScript. For example, when you need to embed a licensing header at
the top of a file. Block comments, which mirror the synax for heredocs,
are preserved in the generated code.
</p>
<%= code_for('block_comment') %>
<h2>
<span id="cake" class="bookmark"></span>
@ -799,18 +806,22 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
</h2>
<p>
CoffeeScript includes a simple build system similar to Make and Rake. Naturally,
CoffeeScript includes a simple build system similar to
<a href="http://www.gnu.org/software/make/">Make</a> and
<a href="http://rake.rubyforge.org/">Rake</a>. Naturally,
it's called Cake, and is used for the build and test tasks for the CoffeeScript
language itself. Tasks are defined in a file named <tt>Cakefile</tt>, and
can be invoked by running <tt>cake taskname</tt> from within the directory.
To print a list of all the tasks, just run <tt>cake</tt>.
To print a list of all the tasks and options, just run <tt>cake</tt>.
</p>
<p>
Task definitions are written in CoffeeScript, so you can put arbitrary code
in your Cakefile. Define a task with a name, a long description, and the
function to invoke when the task is run. Here's a hypothetical task
that uses the Node.js API.
function to invoke when the task is run. If your task takes a command-line
option, you can define the option with short and long flags, and it will
be made available in the <tt>options</tt> object. Here's a task that uses
the Node.js API to rebuild CoffeeScript's parser:
</p>
<%= code_for('cake_tasks') %>

View File

@ -0,0 +1,6 @@
(function(){
/*
CoffeeScript Compiler v0.7.2
Released under the MIT License
*/
})();

View File

@ -1,15 +1,12 @@
(function(){
task('test', 'run each of the unit tests', function() {
var _a, _b, _c, _d;
_a = []; _c = files;
for (_b = 0, _d = _c.length; _b < _d; _b++) {
(function() {
var test = _c[_b];
return _a.push(fs.readFile(test, function(err, code) {
return eval(coffee.compile(code));
}));
})();
}
return _a;
var fs;
fs = require('fs');
option('-o', '--output [DIR]', 'directory for compiled code');
task('build:parser', 'rebuild the Jison parser', function() {
var code, dir;
require('jison');
code = require('./lib/grammar').parser.generate();
dir = options.output || 'lib';
return fs.writeFile(("" + dir + "/parser.js"), code);
});
})();

View File

@ -11,7 +11,6 @@
Animal.prototype.move = function(meters) {
return alert(this.name + " moved " + meters + "m.");
};
Snake = function(name) {
this.name = name;
return this;
@ -21,7 +20,6 @@
alert("Slithering...");
return Snake.__superClass__.move.call(this, 5);
};
Horse = function(name) {
this.name = name;
return this;
@ -31,7 +29,6 @@
alert("Galloping...");
return Horse.__superClass__.move.call(this, 45);
};
sam = new Snake("Sammy the Python");
tom = new Horse("Tommy the Palomino");
sam.move();

View File

@ -4,11 +4,7 @@
if (student.excellentWork) {
return "A+";
} else if (student.okayStuff) {
if (student.triedHard) {
return "B";
} else {
return "B-";
}
return student.triedHard ? "B" : "B-";
} else {
return "C";
}

View File

@ -3,9 +3,10 @@
var __hasProp = Object.prototype.hasOwnProperty;
globals = (function() {
_a = []; _b = window;
for (name in _b) { if (__hasProp.call(_b, name)) {
for (name in _b) {
if (!__hasProp.call(_b, name)) continue;
_a.push(name);
}}
}
return _a;
})().slice(0, 10);
})();

View File

@ -1,7 +0,0 @@
(function(){
// CoffeeScript on the left, JS on the right.
var square = function(x) {
return x * x;
};
})();

View File

@ -8,10 +8,11 @@
};
ages = (function() {
_a = []; _b = yearsOld;
for (child in _b) { if (__hasProp.call(_b, child)) {
for (child in _b) {
if (!__hasProp.call(_b, child)) continue;
age = _b[child];
_a.push(child + " is " + age);
}}
}
return _a;
})();
})();

View File

@ -1,8 +0,0 @@
(function(){
// Comments start with hash marks. Periods mark the end of a block.
var left_hand = raining ? umbrella : parasol;
// To signal the beginning of the next expression,
// use "then", or a newline.
left_hand = raining ? umbrella : parasol;
})();

View File

@ -1,4 +1,4 @@
(function(){
var _a;
typeof (_a = (lottery.drawWinner())) === "undefined" || _a == undefined ? undefined : _a.address == undefined ? undefined : _a.address.zipcode;
var _a, _b;
(_b = (typeof (_a = (lottery.drawWinner())) === "undefined" || _a == undefined ? undefined : _a.address)) == undefined ? undefined : _b.zipcode;
})();

View File

@ -45,7 +45,7 @@
<a href="#try">Try/Catch/Finally</a>
<a href="#comparisons">Chained Comparisons</a>
<a href="#interpolation">String and RegExp Interpolation</a>
<a href="#strings">Multiline Strings and Heredocs</a>
<a href="#strings">Multiline Strings, Heredocs, and Block Comments</a>
<a href="#cake">Cake, and Cakefiles</a>
<a href="#scripts">"text/coffeescript" Script Tags</a>
<a href="#resources">Resources</a>
@ -942,10 +942,11 @@ yearsOld <span class="Keyword">=</span> {
};
ages <span class="Keyword">=</span> (<span class="Storage">function</span>() {
_a <span class="Keyword">=</span> []; _b <span class="Keyword">=</span> yearsOld;
<span class="Keyword">for</span> (child <span class="Keyword">in</span> _b) { <span class="Keyword">if</span> (__hasProp.<span class="LibraryFunction">call</span>(_b, child)) {
<span class="Keyword">for</span> (child <span class="Keyword">in</span> _b) {
<span class="Keyword">if</span> (<span class="Keyword">!</span>__hasProp.<span class="LibraryFunction">call</span>(_b, child)) <span class="Keyword">continue</span>;
age <span class="Keyword">=</span> _b[child];
_a.<span class="LibraryFunction">push</span>(child <span class="Keyword">+</span> <span class="String"><span class="String">&quot;</span> is <span class="String">&quot;</span></span> <span class="Keyword">+</span> age);
}}
}
<span class="Keyword">return</span> _a;
})();
</pre><button onclick='javascript: var _a, _b, age, ages, child, yearsOld;
@ -957,10 +958,11 @@ yearsOld = {
};
ages = (function() {
_a = []; _b = yearsOld;
for (child in _b) { if (__hasProp.call(_b, child)) {
for (child in _b) {
if (!__hasProp.call(_b, child)) continue;
age = _b[child];
_a.push(child + " is " + age);
}}
}
return _a;
})();
;alert(ages.join(", "));'>run: ages.join(", ")</button><br class='clear' /></div>
@ -1031,11 +1033,7 @@ eldest<span class="Keyword">:</span> <span class="Keyword">if</span> <span class
<span class="Keyword">if</span> (student.excellentWork) {
<span class="Keyword">return</span> <span class="String"><span class="String">&quot;</span>A+<span class="String">&quot;</span></span>;
} <span class="Keyword">else</span> <span class="Keyword">if</span> (student.okayStuff) {
<span class="Keyword">if</span> (student.triedHard) {
<span class="Keyword">return</span> <span class="String"><span class="String">&quot;</span>B<span class="String">&quot;</span></span>;
} <span class="Keyword">else</span> {
<span class="Keyword">return</span> <span class="String"><span class="String">&quot;</span>B-<span class="String">&quot;</span></span>;
}
<span class="Keyword">return</span> student.triedHard ? <span class="String"><span class="String">&quot;</span>B<span class="String">&quot;</span></span> : <span class="String"><span class="String">&quot;</span>B-<span class="String">&quot;</span></span>;
} <span class="Keyword">else</span> {
<span class="Keyword">return</span> <span class="String"><span class="String">&quot;</span>C<span class="String">&quot;</span></span>;
}
@ -1046,11 +1044,7 @@ grade = function(student) {
if (student.excellentWork) {
return "A+";
} else if (student.okayStuff) {
if (student.triedHard) {
return "B";
} else {
return "B-";
}
return student.triedHard ? "B" : "B-";
} else {
return "C";
}
@ -1085,18 +1079,20 @@ globals<span class="Keyword">:</span> (name <span class="Keyword">for</span> nam
<span class="Storage">var</span> __hasProp <span class="Keyword">=</span> <span class="LibraryClassType">Object</span>.<span class="LibraryConstant">prototype</span>.hasOwnProperty;
globals <span class="Keyword">=</span> (<span class="Storage">function</span>() {
_a <span class="Keyword">=</span> []; _b <span class="Keyword">=</span> <span class="LibraryClassType">window</span>;
<span class="Keyword">for</span> (name <span class="Keyword">in</span> _b) { <span class="Keyword">if</span> (__hasProp.<span class="LibraryFunction">call</span>(_b, name)) {
<span class="Keyword">for</span> (name <span class="Keyword">in</span> _b) {
<span class="Keyword">if</span> (<span class="Keyword">!</span>__hasProp.<span class="LibraryFunction">call</span>(_b, name)) <span class="Keyword">continue</span>;
_a.<span class="LibraryFunction">push</span>(name);
}}
}
<span class="Keyword">return</span> _a;
})().<span class="LibraryFunction">slice</span>(<span class="Number">0</span>, <span class="Number">10</span>);
</pre><button onclick='javascript: var _a, _b, globals, name;
var __hasProp = Object.prototype.hasOwnProperty;
globals = (function() {
_a = []; _b = window;
for (name in _b) { if (__hasProp.call(_b, name)) {
for (name in _b) {
if (!__hasProp.call(_b, name)) continue;
_a.push(name);
}}
}
return _a;
})().slice(0, 10);
;alert(globals);'>run: globals</button><br class='clear' /></div>
@ -1173,8 +1169,8 @@ speed = (typeof speed !== "undefined" && speed !== null) ? speed : 140;
the <b>TypeError</b> that would be raised otherwise.
</p>
<div class='code'><pre class="idle">lottery.drawWinner()<span class="Keyword">?</span>.address<span class="Keyword">?</span>.zipcode
</pre><pre class="idle"><span class="Storage">var</span> _a;
<span class="Keyword">typeof</span> (_a <span class="Keyword">=</span> (lottery.drawWinner())) <span class="Keyword">===</span> <span class="String"><span class="String">&quot;</span>undefined<span class="String">&quot;</span></span> <span class="Keyword">||</span> _a <span class="Keyword">==</span> undefined ? undefined : _a.address <span class="Keyword">==</span> undefined ? undefined : _a.address.zipcode;
</pre><pre class="idle"><span class="Storage">var</span> _a, _b;
(_b <span class="Keyword">=</span> (<span class="Keyword">typeof</span> (_a <span class="Keyword">=</span> (lottery.drawWinner())) <span class="Keyword">===</span> <span class="String"><span class="String">&quot;</span>undefined<span class="String">&quot;</span></span> <span class="Keyword">||</span> _a <span class="Keyword">==</span> undefined ? undefined : _a.address)) <span class="Keyword">==</span> undefined ? undefined : _b.zipcode;
</pre><br class='clear' /></div>
<p>
Soaking up nulls is similar to Ruby's
@ -1245,7 +1241,6 @@ tom.move()
<span class="LibraryClassType">Animal</span>.<span class="LibraryConstant">prototype</span>.<span class="FunctionName">move</span> = <span class="Storage">function</span>(<span class="FunctionArgument">meters</span>) {
<span class="Keyword">return</span> <span class="LibraryFunction">alert</span>(<span class="Variable">this</span>.<span class="LibraryConstant">name</span> <span class="Keyword">+</span> <span class="String"><span class="String">&quot;</span> moved <span class="String">&quot;</span></span> <span class="Keyword">+</span> meters <span class="Keyword">+</span> <span class="String"><span class="String">&quot;</span>m.<span class="String">&quot;</span></span>);
};
<span class="FunctionName">Snake</span> = <span class="Storage">function</span>(<span class="FunctionArgument">name</span>) {
<span class="Variable">this</span>.<span class="LibraryConstant">name</span> <span class="Keyword">=</span> name;
<span class="Keyword">return</span> <span class="Variable">this</span>;
@ -1255,7 +1250,6 @@ __extends(Snake, Animal);
<span class="LibraryFunction">alert</span>(<span class="String"><span class="String">&quot;</span>Slithering...<span class="String">&quot;</span></span>);
<span class="Keyword">return</span> Snake.__superClass__.move.<span class="LibraryFunction">call</span>(<span class="Variable">this</span>, <span class="Number">5</span>);
};
<span class="FunctionName">Horse</span> = <span class="Storage">function</span>(<span class="FunctionArgument">name</span>) {
<span class="Variable">this</span>.<span class="LibraryConstant">name</span> <span class="Keyword">=</span> name;
<span class="Keyword">return</span> <span class="Variable">this</span>;
@ -1265,7 +1259,6 @@ __extends(Horse, Animal);
<span class="LibraryFunction">alert</span>(<span class="String"><span class="String">&quot;</span>Galloping...<span class="String">&quot;</span></span>);
<span class="Keyword">return</span> Horse.__superClass__.move.<span class="LibraryFunction">call</span>(<span class="Variable">this</span>, <span class="Number">45</span>);
};
sam <span class="Keyword">=</span> <span class="Keyword">new</span> <span class="TypeName">Snake</span>(<span class="String"><span class="String">&quot;</span>Sammy the Python<span class="String">&quot;</span></span>);
tom <span class="Keyword">=</span> <span class="Keyword">new</span> <span class="TypeName">Horse</span>(<span class="String"><span class="String">&quot;</span>Tommy the Palomino<span class="String">&quot;</span></span>);
sam.move();
@ -1282,7 +1275,6 @@ Animal = function() { };
Animal.prototype.move = function(meters) {
return alert(this.name + " moved " + meters + "m.");
};
Snake = function(name) {
this.name = name;
return this;
@ -1292,7 +1284,6 @@ Snake.prototype.move = function() {
alert("Slithering...");
return Snake.__superClass__.move.call(this, 5);
};
Horse = function(name) {
this.name = name;
return this;
@ -1302,7 +1293,6 @@ Horse.prototype.move = function() {
alert("Galloping...");
return Horse.__superClass__.move.call(this, 45);
};
sam = new Snake("Sammy the Python");
tom = new Horse("Tommy the Palomino");
sam.move();
@ -1649,7 +1639,7 @@ dates = (new RegExp(("\\d+" + sep + "\\d+" + sep + "\\d+"), "g"));
<p>
<span id="strings" class="bookmark"></span>
<b class="header">Multiline Strings and Heredocs</b>
<b class="header">Multiline Strings, Heredocs, and Block Comments</b>
Multiline strings are allowed in CoffeeScript.
</p>
<div class='code'><pre class="idle">mobyDick<span class="Keyword">:</span> <span class="String"><span class="String">&quot;</span>Call me Ishmael. Some years ago --</span>
@ -1692,6 +1682,21 @@ html <span class="Keyword">=</span> <span class="String"><span class="String">'<
<p>
Double-quoted heredocs, like double-quoted strings, allow interpolation.
</p>
<p>
Sometimes you'd like to pass a block comment through to the generated
JavaScript. For example, when you need to embed a licensing header at
the top of a file. Block comments, which mirror the synax for heredocs,
are preserved in the generated code.
</p>
<div class='code'><pre class="idle"><span class="Comment"><span class="Comment">###</span></span>
<span class="Comment">CoffeeScript Compiler v0.7.2</span>
<span class="Comment">Released under the MIT License</span>
<span class="Comment"><span class="Comment">###</span></span>
</pre><pre class="idle"><span class="Comment"><span class="Comment">/*</span></span>
<span class="Comment">CoffeeScript Compiler v0.7.2</span>
<span class="Comment">Released under the MIT License</span>
<span class="Comment"><span class="Comment">*/</span></span>
</pre><br class='clear' /></div>
<h2>
<span id="cake" class="bookmark"></span>
@ -1699,34 +1704,41 @@ html <span class="Keyword">=</span> <span class="String"><span class="String">'<
</h2>
<p>
CoffeeScript includes a simple build system similar to Make and Rake. Naturally,
CoffeeScript includes a simple build system similar to
<a href="http://www.gnu.org/software/make/">Make</a> and
<a href="http://rake.rubyforge.org/">Rake</a>. Naturally,
it's called Cake, and is used for the build and test tasks for the CoffeeScript
language itself. Tasks are defined in a file named <tt>Cakefile</tt>, and
can be invoked by running <tt>cake taskname</tt> from within the directory.
To print a list of all the tasks, just run <tt>cake</tt>.
To print a list of all the tasks and options, just run <tt>cake</tt>.
</p>
<p>
Task definitions are written in CoffeeScript, so you can put arbitrary code
in your Cakefile. Define a task with a name, a long description, and the
function to invoke when the task is run. Here's a hypothetical task
that uses the Node.js API.
function to invoke when the task is run. If your task takes a command-line
option, you can define the option with short and long flags, and it will
be made available in the <tt>options</tt> object. Here's a task that uses
the Node.js API to rebuild CoffeeScript's parser:
</p>
<div class='code'><pre class="idle">task <span class="String"><span class="String">'</span>test<span class="String">'</span></span>, <span class="String"><span class="String">'</span>run each of the unit tests<span class="String">'</span></span>, <span class="Storage">-&gt;</span>
<span class="Keyword">for</span> test <span class="Keyword">in</span> files
fs.readFile test, <span class="FunctionArgument">(</span><span class="FunctionArgument">err, code</span><span class="FunctionArgument">)</span> <span class="Storage">-&gt;</span> eval coffee.compile code
</pre><pre class="idle">task(<span class="String"><span class="String">'</span>test<span class="String">'</span></span>, <span class="String"><span class="String">'</span>run each of the unit tests<span class="String">'</span></span>, <span class="Storage">function</span>() {
<span class="Storage">var</span> _a, _b, _c, _d;
_a <span class="Keyword">=</span> []; _c <span class="Keyword">=</span> files;
<span class="Keyword">for</span> (_b <span class="Keyword">=</span> <span class="Number">0</span>, _d <span class="Keyword">=</span> _c.<span class="LibraryConstant">length</span>; _b <span class="Keyword">&lt;</span> _d; _b<span class="Keyword">++</span>) {
(<span class="Storage">function</span>() {
<span class="Storage">var</span> test <span class="Keyword">=</span> _c[_b];
<span class="Keyword">return</span> _a.<span class="LibraryFunction">push</span>(fs.readFile(test, <span class="Storage">function</span>(err, code) {
<span class="Keyword">return</span> <span class="LibraryFunction">eval</span>(coffee.<span class="LibraryFunction">compile</span>(code));
}));
})();
}
<span class="Keyword">return</span> _a;
<div class='code'><pre class="idle">fs<span class="Keyword">:</span> require <span class="String"><span class="String">'</span>fs<span class="String">'</span></span>
option <span class="String"><span class="String">'</span>-o<span class="String">'</span></span>, <span class="String"><span class="String">'</span>--output [DIR]<span class="String">'</span></span>, <span class="String"><span class="String">'</span>directory for compiled code<span class="String">'</span></span>
task <span class="String"><span class="String">'</span>build:parser<span class="String">'</span></span>, <span class="String"><span class="String">'</span>rebuild the Jison parser<span class="String">'</span></span>, <span class="Storage">-&gt;</span>
require <span class="String"><span class="String">'</span>jison<span class="String">'</span></span>
code<span class="Keyword">:</span> require(<span class="String"><span class="String">'</span>./lib/grammar<span class="String">'</span></span>).parser.generate()
dir<span class="Keyword">:</span> options.output <span class="Keyword">or</span> <span class="String"><span class="String">'</span>lib<span class="String">'</span></span>
fs.writeFile <span class="String"><span class="String">&quot;</span><span class="String"><span class="String">$</span>dir</span>/parser.js<span class="String">&quot;</span></span>, code
</pre><pre class="idle"><span class="Storage">var</span> fs;
fs <span class="Keyword">=</span> require(<span class="String"><span class="String">'</span>fs<span class="String">'</span></span>);
option(<span class="String"><span class="String">'</span>-o<span class="String">'</span></span>, <span class="String"><span class="String">'</span>--output [DIR]<span class="String">'</span></span>, <span class="String"><span class="String">'</span>directory for compiled code<span class="String">'</span></span>);
task(<span class="String"><span class="String">'</span>build:parser<span class="String">'</span></span>, <span class="String"><span class="String">'</span>rebuild the Jison parser<span class="String">'</span></span>, <span class="Storage">function</span>() {
<span class="Storage">var</span> code, dir;
require(<span class="String"><span class="String">'</span>jison<span class="String">'</span></span>);
code <span class="Keyword">=</span> require(<span class="String"><span class="String">'</span>./lib/grammar<span class="String">'</span></span>).parser.generate();
dir <span class="Keyword">=</span> options.output <span class="Keyword">||</span> <span class="String"><span class="String">'</span>lib<span class="String">'</span></span>;
<span class="Keyword">return</span> fs.writeFile((<span class="String"><span class="String">&quot;</span><span class="String">&quot;</span></span> <span class="Keyword">+</span> dir <span class="Keyword">+</span> <span class="String"><span class="String">&quot;</span>/parser.js<span class="String">&quot;</span></span>), code);
});
</pre><br class='clear' /></div>