Skip to content

Commit a4bdd92

Browse files
author
David Brady
committed
Initial version
- Functional yet awful layout - Site navigation - License file (CC BY 3.0) - authors/contributing/development guides - some simple, sample recipes
1 parent 7b307cd commit a4bdd92

21 files changed

+519
-82
lines changed

LICENSE-CC-BY.textile

Lines changed: 60 additions & 0 deletions
Large diffs are not rendered by default.

_config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
pygments: true
12
exclude:
23
- README
34
- CNAME
5+
- TODO.textile
6+
- script
7+
48

_layouts/default.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@
55
<link rel="stylesheet" href="/css/default.css" type="text/css">
66
</head>
77
<body>
8+
<div class="header">
9+
<h1><a href="/">CoffeeScript Cookbook</a></h1>
10+
<a href="/chapters">Chapter Index</a> |
11+
<a href="/contributing">Contributing</a> |
12+
<a href="/authors">Authors</a> |
13+
<a href="/license">License</a>
14+
</div>
815
{{ content }}
16+
<hr />
17+
<a href="/license"><img border="0" src="/images/cc_by_badge.png"></a>
918
</body>
1019
</html>
1120

_layouts/recipe.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>CoffeeScript Cookbook &raquo; {{ page.title }}</title>
5+
<link rel="stylesheet" href="/css/default.css" type="text/css">
6+
</head>
7+
<body>
8+
<div class="header">
9+
<h2>
10+
<a href="/">CoffeeScript Cookbok:</a>
11+
<a href="/chapters">Chapters</a> &raquo;
12+
<a href="/chapters/{{ page.chapter | downcase }}">{{ page.chapter }}</a> &raquo;
13+
{{ page.title }}</h2>
14+
</div>
15+
{{ content }}
16+
</body>
17+
</html>
18+

authors-guide.textile

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
layout: default
3+
title: Author's Guide
4+
---
5+
6+
h1. Author's Guide
7+
8+
h2. tl;dr: Look at Other Recipes, and Blend In
9+
10+
Look at the source of other recipe pages and follow that page structure. Start with the <a href="/developers-guide">Developer's Guide</a> to get a test version of the cookbook up and running on your machine, and get to work!
11+
12+
h2. General Guidelines
13+
14+
* Feel free to add new pages, or even chapters. Just keep them organized.
15+
* Try to write well-styled, idiomatic CoffeeScript.
16+
* Try to stay within the Problem/Solution/Discussion format. If you can't think of a good problem for your recipe... hang onto it for a while.
17+
* Sharing code that you think might only be used rarely is fine. Sharing esoteric code tricks is less so. There's a difference between sharing a reader for an obscure format and sharing a weird bit-shifting trick that does fast but inaccurate multiplication.
18+
19+
h2. Use Cookbook Problem/Solution/Discussion Format
20+
21+
A typical cookbook page will have three sections (four if you count the title):
22+
23+
* +Problem+ A one- or two-sentence description of the problem, such as "You want to access parts of a string" or "You want to format a floating point number as currency, with a leading dollar sign, two digits of precision and comma-separated triples." Where possible, phrase the problem as though speaking directly to the reader: "You want to...", "You have an X but you want a Y", etc.
24+
* +Solution+ State the solution as briefly as possible, ideally as a single sentence that identifies the strategy you would use, and give example code. It's tempting to explain the solution, but don't do it yet. Remember that the reader will look this solution up many times after the first time, and that they will be looking for a quick reference each time. You're going to explain the solution in the +Discussion+, and the first time reader will read your solution, think about it, and then proceed to the discussion if necessary. For example, for "accessing parts of a string", a good Solution sentence might be "Use CoffeeScript's array range subscripts, or JavaScript's slice function."
25+
* +Discussion+ Here you should explain why your solution works, or give further examples such as edge cases. If your solution can break on some edge cases, be sure to note them here. For example, if a percentage function crashes when given a zero, you could note this in the discussion and give a workaround. NOTE: If your solution has really dangerous edge cases, so dangerous that you would include them in the solution, please consider whether your recipe should be included at all. Remember, this Cookbook is for good code!
26+
27+
h2. Tag the page with Jekyll frontmatter
28+
29+
...that's a lot of fancy words that mean "don't forget to put the layout, chapter and page title at the top of the file in a YAML block". For example, the string interpolation page begins with
30+
31+
<tt>---
32+
layout: recipe
33+
title: String Interpolation
34+
chapter: Strings
35+
---</tt>
36+
37+
h2. Use Liquid highlighting templates
38+
39+
Turn on syntax highlighting for CoffeeScript with <tt>highlight coffeescript</tt>.
40+
41+
<tt>&lbrace;% highlight coffeescript %&rbrace;
42+
&#35; Calculate the square of a number
43+
square = (x) -> x * x
44+
45+
square(16)
46+
&#35; => 256
47+
&lbrace;% endhighlight %&rbrace;</tt>
48+
49+
This produces the following:
50+
51+
{% highlight coffeescript %}
52+
# Calculate the square of a number
53+
square = (x) -> x * x
54+
55+
square(16)
56+
# => 256
57+
{% endhighlight %}
58+
59+
60+
h1. FAQ
61+
62+
h2. I Have a Weird Recipe. Should I Share It?
63+
64+
Maybe! The real question is, is it really useful, or is it just clever? If you have a formatter for a weird Albanian GIS format, that's a real problem that almost nobody would ever have&mdash;but when somebody DOES have that problem, they REALLY need a solution. If you have a bit shifting trick that can swap two numbers using three xor-equals operations, that's a really clever solution but it's not very good CoffeeScript. (For one thing, <tt>x ^= y ^= x ^= y</tt> is not idiomatic, while <tt>[x,y]=[y,x]</tt> is. For another, there's a bug in that code. And once you fix it, there's another bug caused by extrapolating this register trick to a reference-based language where&mdash;look, it's just a bad idea, okay?)
65+
66+
If you have a cool but weird recipe, ask yourself if a reader would genuinely find it useful. Here are two very good questions to consider:
67+
68+
* Does it really solve a problem that an actual person might have?
69+
* If somebody really does have that problem, would your recipe really be the best solution?
70+
71+
If the answer to either question is no, you might have some code that is a "clever solution in search of a problem". If in doubt, ask.
72+
73+
74+
h2. What If My Recipe is Inefficient/Too Big/Too Slow?
75+
76+
If it solves a problem to which the alternative is to _not_ solve the problem, share it. Let the reader decide if they want to use it. Sometimes we want tight efficient code, other times we want a robust featureset. If the code has abysmal performance characteristics, be sure to warn the reader in the Discussion.
77+
78+
79+
h2. Can I Edit An Existing Recipe?
80+
81+
Yes. Please improve anything and everything! Be sure to test your changes and make sure that your solution really is better.
82+
83+
h2. I Have a Really Efficient Solution, But It's Not As Readable As the Existing Recipe. Should I Add It?
84+
85+
See the "Weird Recipe" note above. Do real people in the real world ever hit the performance constraint? If so, then by all means, add your strategy to the existing solution, and be sure to explain why your solution is not idiomatic. If a reader really has that problem, they'll be glad for the extra options.
86+
87+
h2. I Have A Problem/Solution, But It's Basically Just JavaScript. Should I Add It?
88+
89+
Yes! CoffeeScript compiles to JavaScript, and that means that some of its functionality comes straight from JavaScript. (For example, see <a href="/chapters/arrays/reversing-arrays">Reversing Arrays</a>.) But if you're programming in CoffeeScript and you need to reverse an array, this Cookbook should stand ready to tell you it's available to you in CoffeeScript&mdash;even if it's just a straight call into a JavaScript library.
90+
91+

authors.textile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
layout: default
3+
title: Authors
4+
---
5+
6+
h1. Authors
7+
8+
* David Brady [email protected]_
9+
* Mike Moore [email protected]_
10+
* ...You! What are you waiting for? Check out the <a href="/contributing">contributing</a> section and get cracking!

chapters/arrays/reversing-arrays.md

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
layout: recipe
3+
title: Reversing Arrays
4+
chapter: Arrays
5+
---
6+
7+
h1. Reversing an Array
8+
9+
h2. Problem
10+
11+
You want to reverse an array.
12+
13+
h2. Solution
14+
15+
Use JavaScript's Array reverse() method:
16+
17+
{% highlight coffeescript %}
18+
["one", "two", "three"].reverse()
19+
# => ["three", "two", "one"]
20+
{% endhighlight %}
21+
22+
23+
h2. Discussion
24+
25+
reverse() is a standard JavaScript method. Don't forget the parentheses.
26+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
layout: recipe
3+
title: When Function Parentheses are not Optional
4+
chapter: Functions
5+
---
6+
7+
h2. Problem
8+
9+
You want to call a function that takes no arguments, but don't want to use parentheses.
10+
11+
h2. Solution
12+
13+
Use parentheses anyway.
14+
15+
h2. Discussion
16+
17+
Like Ruby, CoffeeScript allows you to drop parentheses to method calls. Unlike Ruby, however, CoffeeScript treats a bare function name as the pointer to the function. The practical upshot of this is that if you give no arguments to a method, CoffeeScript cannot tell if you want to call the function or use it as a reference.
18+
19+
Is this good or bad? It's just different. It creates an unexpected syntax case&mdash;parentheses aren't _always_ optional&mdash;but in exchange it gives you the ability to pass and receive functions fluently by name, something that's a bit klunky in Ruby.
20+
21+

chapters/index.html

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)