liquid_cms 0.3.1.0 → 0.3.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +10 -0
- data/Gemfile +0 -3
- data/README.rdoc +16 -2
- data/TODO.rdoc +0 -2
- data/app/controllers/cms/assets_controller.rb +20 -6
- data/app/controllers/cms/components_controller.rb +11 -3
- data/app/controllers/cms/pages_controller.rb +15 -4
- data/app/helpers/cms/common_helper.rb +31 -32
- data/app/liquid/cms_paperclip_extension.rb +75 -3
- data/app/liquid/tags/{data_tag.rb → cms/data_tag.rb} +0 -0
- data/app/models/cms/asset.rb +1 -1
- data/app/models/cms/component.rb +3 -3
- data/app/views/cms/assets/_form.html.erb +1 -2
- data/app/views/cms/assets/update.js.rjs +1 -0
- data/app/views/cms/components/update.js.rjs +1 -0
- data/app/views/cms/pages/_form.html.erb +3 -3
- data/app/views/cms/pages/update.js.rjs +1 -0
- data/app/views/layouts/cms.html.erb +4 -3
- data/config/initializers/cms/simple_form.rb +70 -12
- data/config/initializers/cms/simple_form_updates.rb +9 -3
- data/config/locales/cms/en.yml +6 -0
- data/lib/generators/liquid_cms/install_generator.rb +5 -1
- data/lib/generators/liquid_cms/templates/migration_rev2.rb +13 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/LICENSE +16 -20
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/lib/codemirror.css +68 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/lib/codemirror.js +2197 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/lib/overlay.js +51 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/lib/runmode.js +27 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/clike/clike.js +247 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/clike/index.html +102 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/clojure/clojure.js +207 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/clojure/index.html +85 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/coffeescript/LICENSE +22 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/coffeescript/coffeescript.js +325 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/coffeescript/index.html +722 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/css/css.js +124 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/css/index.html +56 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/diff/diff.css +3 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/diff/diff.js +13 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/diff/index.html +99 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/haskell/haskell.js +242 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/haskell/index.html +60 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/htmlmixed/htmlmixed.js +79 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/htmlmixed/index.html +52 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/javascript/index.html +78 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/javascript/javascript.js +352 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/jinja2/index.html +38 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/jinja2/jinja2.js +42 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/lua/index.html +72 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/lua/lua.js +140 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/markdown/index.html +340 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/markdown/markdown.css +10 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/markdown/markdown.js +230 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/ntriples/index.html +33 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/ntriples/ntriples.js +172 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/pascal/LICENSE +7 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/pascal/index.html +49 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/pascal/pascal.js +138 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/php/index.html +49 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/php/php.js +116 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/plsql/index.html +63 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/plsql/plsql.js +217 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/python/LICENSE.txt +21 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/python/index.html +123 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/python/python.js +320 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/r/LICENSE +24 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/r/index.html +74 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/r/r.js +141 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/rst/index.html +526 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/rst/rst.css +75 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/rst/rst.js +333 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/ruby/LICENSE +24 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/ruby/index.html +172 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/ruby/ruby.js +195 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/scheme/index.html +65 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/scheme/scheme.js +202 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/smalltalk/index.html +56 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/smalltalk/smalltalk.js +122 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/sparql/index.html +41 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/sparql/sparql.js +143 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/stex/index.html +96 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/stex/stex.js +167 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/velocity/index.html +103 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/velocity/velocity.js +146 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/xml/index.html +42 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/xml/xml.js +231 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/xmlpure/index.html +60 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/xmlpure/xmlpure.js +481 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/yaml/index.html +68 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/yaml/yaml.js +95 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/theme/cobalt.css +17 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/theme/default.css +19 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/theme/eclipse.css +24 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/theme/elegant.css +9 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/theme/neat.css +8 -0
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/theme/night.css +20 -0
- data/lib/generators/liquid_cms/templates/public/cms/javascripts/cms.js +1 -1
- data/lib/generators/liquid_cms/templates/public/cms/javascripts/codemirror_custom.js +96 -0
- data/lib/generators/liquid_cms/templates/public/cms/stylesheets/codemirror_changes.css +26 -0
- data/lib/generators/liquid_cms/templates/public/cms/stylesheets/liquid.css +7 -0
- data/lib/generators/liquid_cms/templates/public/cms/stylesheets/simple_form.css +0 -8
- data/lib/liquid_cms/version.rb +1 -1
- data/liquid_cms.gemspec +1 -1
- data/test/functional/assets_controller_test.rb +35 -20
- data/test/functional/components_controller_test.rb +15 -5
- data/test/functional/pages_controller_test.rb +19 -6
- data/test/rails_app/Gemfile +1 -2
- data/test/rails_app/config/locales/cms/en.yml +26 -4
- data/test/rails_app/db/migrate/20110511161859_create_liquid_cms_upgrade_rev2.rb +13 -0
- data/test/unit/asset_test.rb +1 -1
- data/test/unit/component_test.rb +1 -1
- metadata +89 -32
- data/generators/liquid_cms/templates/migration_rev1.rb +0 -38
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/css/csscolors.css +0 -55
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/css/docs.css +0 -158
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/css/font.js +0 -15
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/css/jscolors.css +0 -59
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/css/sparqlcolors.css +0 -43
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/css/xmlcolors.css +0 -55
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/js/codemirror.js +0 -582
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/js/editor.js +0 -1671
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/js/highlight.js +0 -68
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/js/mirrorframe.js +0 -81
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/js/parsecss.js +0 -161
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/js/parsedummy.js +0 -32
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/js/parsehtmlmixed.js +0 -93
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/js/parsejavascript.js +0 -359
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/js/parsesparql.js +0 -162
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/js/parsexml.js +0 -291
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/js/select.js +0 -699
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/js/stringstream.js +0 -159
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/js/tokenize.js +0 -57
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/js/tokenizejavascript.js +0 -174
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/js/undo.js +0 -413
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/js/unittests.js +0 -44
- data/lib/generators/liquid_cms/templates/public/cms/codemirror/js/util.js +0 -133
@@ -0,0 +1,85 @@
|
|
1
|
+
<!doctype html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>CodeMirror 2: Clojure mode</title>
|
5
|
+
<link rel="stylesheet" href="../../lib/codemirror.css">
|
6
|
+
<script src="../../lib/codemirror.js"></script>
|
7
|
+
<script src="clojure.js"></script>
|
8
|
+
<link rel="stylesheet" href="../../theme/default.css">
|
9
|
+
<style>.CodeMirror {background: #f8f8f8;}</style>
|
10
|
+
<link rel="stylesheet" href="../../css/docs.css">
|
11
|
+
</head>
|
12
|
+
<body>
|
13
|
+
<h1>CodeMirror 2: Clojure mode</h1>
|
14
|
+
<form><textarea id="code" name="code">
|
15
|
+
; Conway's Game of Life, from http://rosettacode.org/wiki/Conway%27s_Game_of_Life#Clojure
|
16
|
+
(defstruct grid :w :h :cells)
|
17
|
+
|
18
|
+
(defn get-cell
|
19
|
+
"Returns the value at x,y. The grid is treated as a torus, such that both x and
|
20
|
+
y coordinates will wrap around if greater than width and height respectively."
|
21
|
+
[grid x y]
|
22
|
+
(let [x (mod x (:w grid))
|
23
|
+
y (mod y (:h grid))]
|
24
|
+
(-> grid :cells (nth y) (nth x))))
|
25
|
+
|
26
|
+
(defn neighbors
|
27
|
+
"Returns a lazy sequence of all neighbors of the specified cell."
|
28
|
+
[grid x y]
|
29
|
+
(for [j [(dec y) y (inc y)]
|
30
|
+
i [(dec x) x (inc x)]
|
31
|
+
:when (not (and (= i x) (= j y)))]
|
32
|
+
(get-cell grid i j)))
|
33
|
+
|
34
|
+
(defn evolve-cell
|
35
|
+
"Returns the new state of the specifed cell."
|
36
|
+
[grid x y]
|
37
|
+
(let [c (get-cell grid x y)
|
38
|
+
n (reduce + (neighbors grid x y))]
|
39
|
+
(if (or (and (zero? c) (= 3 n))
|
40
|
+
(and (= 1 c) (or (= 2 n) (= 3 n))))
|
41
|
+
1 0)))
|
42
|
+
|
43
|
+
(defn evolve-grid
|
44
|
+
"Returns a new grid whose cells have all been evolved."
|
45
|
+
[grid]
|
46
|
+
(assoc grid :cells
|
47
|
+
(vec (for [y (range (:h grid))]
|
48
|
+
(vec (for [x (range (:w grid))]
|
49
|
+
(evolve-cell grid x y)))))))
|
50
|
+
|
51
|
+
(defn generations [grid]
|
52
|
+
"Returns a lazy sequence of the grid, and all subsequent generations."
|
53
|
+
(iterate evolve-grid grid))
|
54
|
+
|
55
|
+
(defn make-grid [w h & row-patterns]
|
56
|
+
(let [cells (vec (for [rp row-patterns]
|
57
|
+
(vec (mapcat #(take %1 (repeat %2)) rp (cycle [0 1])))))]
|
58
|
+
(if (and (= h (count cells))
|
59
|
+
(every? #(= w (count %)) cells))
|
60
|
+
(struct grid w h cells)
|
61
|
+
(throw (IllegalArgumentException. "Resulting cells do not match expected width/height.")))))
|
62
|
+
|
63
|
+
(defn display-row [row]
|
64
|
+
(do (dorun (map print (map #(if (zero? %) " . " "[X]") row))) (println)))
|
65
|
+
|
66
|
+
(defn display-grid [grid]
|
67
|
+
(dorun (map display-row (:cells grid))))
|
68
|
+
|
69
|
+
(defn display-grids [grids]
|
70
|
+
(dorun
|
71
|
+
(interleave
|
72
|
+
(repeatedly println)
|
73
|
+
(map display-grid grids))))
|
74
|
+
|
75
|
+
(def blinker (make-grid 5 5 [5] [5] [1 3 1] [5] [5]))
|
76
|
+
(display-grids (take 3 (generations blinker)))
|
77
|
+
</textarea></form>
|
78
|
+
<script>
|
79
|
+
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {});
|
80
|
+
</script>
|
81
|
+
|
82
|
+
<p><strong>MIME types defined:</strong> <code>text/x-clojure</code>.</p>
|
83
|
+
|
84
|
+
</body>
|
85
|
+
</html>
|
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2011 Jeff Pickhardt
|
4
|
+
Modified from the Python CodeMirror mode, Copyright (c) 2010 Timothy Farrell
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in
|
14
|
+
all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
+
THE SOFTWARE.
|
data/lib/generators/liquid_cms/templates/public/cms/codemirror/mode/coffeescript/coffeescript.js
ADDED
@@ -0,0 +1,325 @@
|
|
1
|
+
/**
|
2
|
+
* Link to the project's GitHub page:
|
3
|
+
* https://github.com/pickhardt/coffeescript-codemirror-mode
|
4
|
+
*/
|
5
|
+
CodeMirror.defineMode('coffeescript', function(conf) {
|
6
|
+
var ERRORCLASS = 'error';
|
7
|
+
|
8
|
+
function wordRegexp(words) {
|
9
|
+
return new RegExp("^((" + words.join(")|(") + "))\\b");
|
10
|
+
}
|
11
|
+
|
12
|
+
var singleOperators = new RegExp("^[\\+\\-\\*/%&|\\^~<>!\?]");
|
13
|
+
var singleDelimiters = new RegExp('^[\\(\\)\\[\\]\\{\\}@,:`=;\\.]');
|
14
|
+
var doubleOperators = new RegExp("^((\->)|(\=>)|(\\+\\+)|(\\+\\=)|(\\-\\-)|(\\-\\=)|(\\*\\*)|(\\*\\=)|(\\/\\/)|(\\/\\=)|(==)|(!=)|(<=)|(>=)|(<>)|(<<)|(>>)|(//))");
|
15
|
+
var doubleDelimiters = new RegExp("^((\\.\\.)|(\\+=)|(\\-=)|(\\*=)|(%=)|(/=)|(&=)|(\\|=)|(\\^=))");
|
16
|
+
var tripleDelimiters = new RegExp("^((\\.\\.\\.)|(//=)|(>>=)|(<<=)|(\\*\\*=))");
|
17
|
+
var identifiers = new RegExp("^[_A-Za-z][_A-Za-z0-9]*");
|
18
|
+
|
19
|
+
var wordOperators = wordRegexp(['and', 'or', 'not',
|
20
|
+
'is', 'isnt', 'in',
|
21
|
+
'instanceof', 'typeof']);
|
22
|
+
var indentKeywords = ['for', 'while', 'loop', 'if', 'unless', 'else',
|
23
|
+
'switch', 'try', 'catch', 'finally', 'class'];
|
24
|
+
var commonKeywords = ['break', 'by', 'continue', 'debugger', 'delete',
|
25
|
+
'do', 'in', 'of', 'new', 'return', 'then',
|
26
|
+
'this', 'throw', 'when', 'until'];
|
27
|
+
|
28
|
+
var keywords = wordRegexp(indentKeywords.concat(commonKeywords));
|
29
|
+
|
30
|
+
indentKeywords = wordRegexp(indentKeywords);
|
31
|
+
|
32
|
+
|
33
|
+
var stringPrefixes = new RegExp("^('{3}|\"{3}|['\"])");
|
34
|
+
var regexPrefixes = new RegExp("^(/{3}|/)");
|
35
|
+
var commonConstants = ['Infinity', 'NaN', 'undefined', 'null', 'true', 'false', 'on', 'off', 'yes', 'no'];
|
36
|
+
var constants = wordRegexp(commonConstants);
|
37
|
+
|
38
|
+
// Tokenizers
|
39
|
+
function tokenBase(stream, state) {
|
40
|
+
// Handle scope changes
|
41
|
+
if (stream.sol()) {
|
42
|
+
var scopeOffset = state.scopes[0].offset;
|
43
|
+
if (stream.eatSpace()) {
|
44
|
+
var lineOffset = stream.indentation();
|
45
|
+
if (lineOffset > scopeOffset) {
|
46
|
+
return 'indent';
|
47
|
+
} else if (lineOffset < scopeOffset) {
|
48
|
+
return 'dedent';
|
49
|
+
}
|
50
|
+
return null;
|
51
|
+
} else {
|
52
|
+
if (scopeOffset > 0) {
|
53
|
+
dedent(stream, state);
|
54
|
+
}
|
55
|
+
}
|
56
|
+
}
|
57
|
+
if (stream.eatSpace()) {
|
58
|
+
return null;
|
59
|
+
}
|
60
|
+
|
61
|
+
var ch = stream.peek();
|
62
|
+
|
63
|
+
// Handle comments
|
64
|
+
if (ch === '#') {
|
65
|
+
stream.skipToEnd();
|
66
|
+
return 'comment';
|
67
|
+
}
|
68
|
+
|
69
|
+
// Handle number literals
|
70
|
+
if (stream.match(/^-?[0-9\.]/, false)) {
|
71
|
+
var floatLiteral = false;
|
72
|
+
// Floats
|
73
|
+
if (stream.match(/^-?\d*\.\d+(e[\+\-]?\d+)?/i)) {
|
74
|
+
floatLiteral = true;
|
75
|
+
}
|
76
|
+
if (stream.match(/^-?\d+\.\d*/)) {
|
77
|
+
floatLiteral = true;
|
78
|
+
}
|
79
|
+
if (stream.match(/^-?\.\d+/)) {
|
80
|
+
floatLiteral = true;
|
81
|
+
}
|
82
|
+
if (floatLiteral) {
|
83
|
+
return 'number';
|
84
|
+
}
|
85
|
+
// Integers
|
86
|
+
var intLiteral = false;
|
87
|
+
// Hex
|
88
|
+
if (stream.match(/^-?0x[0-9a-f]+/i)) {
|
89
|
+
intLiteral = true;
|
90
|
+
}
|
91
|
+
// Decimal
|
92
|
+
if (stream.match(/^-?[1-9]\d*(e[\+\-]?\d+)?/)) {
|
93
|
+
intLiteral = true;
|
94
|
+
}
|
95
|
+
// Zero by itself with no other piece of number.
|
96
|
+
if (stream.match(/^-?0(?![\dx])/i)) {
|
97
|
+
intLiteral = true;
|
98
|
+
}
|
99
|
+
if (intLiteral) {
|
100
|
+
return 'number';
|
101
|
+
}
|
102
|
+
}
|
103
|
+
|
104
|
+
// Handle strings
|
105
|
+
if (stream.match(stringPrefixes)) {
|
106
|
+
state.tokenize = tokenFactory(stream.current(), 'string');
|
107
|
+
return state.tokenize(stream, state);
|
108
|
+
}
|
109
|
+
// Handle regex literals
|
110
|
+
if (stream.match(regexPrefixes)) {
|
111
|
+
if (stream.current() != '/' || stream.match(/^.*\//, false)) { // prevent highlight of division
|
112
|
+
state.tokenize = tokenFactory(stream.current(), 'string-2');
|
113
|
+
return state.tokenize(stream, state);
|
114
|
+
} else {
|
115
|
+
stream.backUp(1);
|
116
|
+
}
|
117
|
+
}
|
118
|
+
|
119
|
+
// Handle operators and delimiters
|
120
|
+
if (stream.match(tripleDelimiters) || stream.match(doubleDelimiters)) {
|
121
|
+
return 'punctuation';
|
122
|
+
}
|
123
|
+
if (stream.match(doubleOperators)
|
124
|
+
|| stream.match(singleOperators)
|
125
|
+
|| stream.match(wordOperators)) {
|
126
|
+
return 'operator';
|
127
|
+
}
|
128
|
+
if (stream.match(singleDelimiters)) {
|
129
|
+
return 'punctuation';
|
130
|
+
}
|
131
|
+
|
132
|
+
if (stream.match(constants)) {
|
133
|
+
return 'atom';
|
134
|
+
}
|
135
|
+
|
136
|
+
if (stream.match(keywords)) {
|
137
|
+
return 'keyword';
|
138
|
+
}
|
139
|
+
|
140
|
+
if (stream.match(identifiers)) {
|
141
|
+
return 'variable';
|
142
|
+
}
|
143
|
+
|
144
|
+
// Handle non-detected items
|
145
|
+
stream.next();
|
146
|
+
return ERRORCLASS;
|
147
|
+
}
|
148
|
+
|
149
|
+
function tokenFactory(delimiter, outclass) {
|
150
|
+
var delim_re = new RegExp(delimiter);
|
151
|
+
var singleline = delimiter.length == 1;
|
152
|
+
|
153
|
+
return function tokenString(stream, state) {
|
154
|
+
while (!stream.eol()) {
|
155
|
+
stream.eatWhile(/[^'"\/\\]/);
|
156
|
+
if (stream.eat('\\')) {
|
157
|
+
stream.next();
|
158
|
+
if (singleline && stream.eol()) {
|
159
|
+
return outclass;
|
160
|
+
}
|
161
|
+
} else if (stream.match(delim_re)) {
|
162
|
+
state.tokenize = tokenBase;
|
163
|
+
return outclass;
|
164
|
+
} else {
|
165
|
+
stream.eat(/['"\/]/);
|
166
|
+
}
|
167
|
+
}
|
168
|
+
if (singleline) {
|
169
|
+
if (conf.mode.singleLineStringErrors) {
|
170
|
+
outclass = ERRORCLASS
|
171
|
+
} else {
|
172
|
+
state.tokenize = tokenBase;
|
173
|
+
}
|
174
|
+
}
|
175
|
+
return outclass;
|
176
|
+
};
|
177
|
+
}
|
178
|
+
|
179
|
+
function indent(stream, state, type) {
|
180
|
+
type = type || 'coffee';
|
181
|
+
var indentUnit = 0;
|
182
|
+
if (type === 'coffee') {
|
183
|
+
for (var i = 0; i < state.scopes.length; i++) {
|
184
|
+
if (state.scopes[i].type === 'coffee') {
|
185
|
+
indentUnit = state.scopes[i].offset + conf.indentUnit;
|
186
|
+
break;
|
187
|
+
}
|
188
|
+
}
|
189
|
+
} else {
|
190
|
+
indentUnit = stream.column() + stream.current().length;
|
191
|
+
}
|
192
|
+
state.scopes.unshift({
|
193
|
+
offset: indentUnit,
|
194
|
+
type: type
|
195
|
+
});
|
196
|
+
}
|
197
|
+
|
198
|
+
function dedent(stream, state) {
|
199
|
+
if (state.scopes.length == 1) return;
|
200
|
+
if (state.scopes[0].type === 'coffee') {
|
201
|
+
var _indent = stream.indentation();
|
202
|
+
var _indent_index = -1;
|
203
|
+
for (var i = 0; i < state.scopes.length; ++i) {
|
204
|
+
if (_indent === state.scopes[i].offset) {
|
205
|
+
_indent_index = i;
|
206
|
+
break;
|
207
|
+
}
|
208
|
+
}
|
209
|
+
if (_indent_index === -1) {
|
210
|
+
return true;
|
211
|
+
}
|
212
|
+
while (state.scopes[0].offset !== _indent) {
|
213
|
+
state.scopes.shift();
|
214
|
+
}
|
215
|
+
return false
|
216
|
+
} else {
|
217
|
+
state.scopes.shift();
|
218
|
+
return false;
|
219
|
+
}
|
220
|
+
}
|
221
|
+
|
222
|
+
function tokenLexer(stream, state) {
|
223
|
+
var style = state.tokenize(stream, state);
|
224
|
+
var current = stream.current();
|
225
|
+
|
226
|
+
// Handle '.' connected identifiers
|
227
|
+
if (current === '.') {
|
228
|
+
style = state.tokenize(stream, state);
|
229
|
+
current = stream.current();
|
230
|
+
if (style === 'variable') {
|
231
|
+
return 'variable';
|
232
|
+
} else {
|
233
|
+
return ERRORCLASS;
|
234
|
+
}
|
235
|
+
}
|
236
|
+
|
237
|
+
// Handle properties
|
238
|
+
if (current === '@') {
|
239
|
+
style = state.tokenize(stream, state);
|
240
|
+
current = stream.current();
|
241
|
+
if (style === 'variable') {
|
242
|
+
return 'variable-2';
|
243
|
+
} else {
|
244
|
+
return ERRORCLASS;
|
245
|
+
}
|
246
|
+
}
|
247
|
+
|
248
|
+
// Handle scope changes.
|
249
|
+
if (current === 'return') {
|
250
|
+
state.dedent += 1;
|
251
|
+
}
|
252
|
+
if (((current === '->' || current === '=>') &&
|
253
|
+
!state.lambda &&
|
254
|
+
state.scopes[0].type == 'coffee' &&
|
255
|
+
stream.peek() === '')
|
256
|
+
|| style === 'indent') {
|
257
|
+
indent(stream, state);
|
258
|
+
}
|
259
|
+
var delimiter_index = '[({'.indexOf(current);
|
260
|
+
if (delimiter_index !== -1) {
|
261
|
+
indent(stream, state, '])}'.slice(delimiter_index, delimiter_index+1));
|
262
|
+
}
|
263
|
+
if (indentKeywords.exec(current)){
|
264
|
+
indent(stream, state);
|
265
|
+
}
|
266
|
+
if (current == 'then'){
|
267
|
+
dedent(stream, state);
|
268
|
+
}
|
269
|
+
|
270
|
+
|
271
|
+
if (style === 'dedent') {
|
272
|
+
if (dedent(stream, state)) {
|
273
|
+
return ERRORCLASS;
|
274
|
+
}
|
275
|
+
}
|
276
|
+
delimiter_index = '])}'.indexOf(current);
|
277
|
+
if (delimiter_index !== -1) {
|
278
|
+
if (dedent(stream, state)) {
|
279
|
+
return ERRORCLASS;
|
280
|
+
}
|
281
|
+
}
|
282
|
+
if (state.dedent > 0 && stream.eol() && state.scopes[0].type == 'coffee') {
|
283
|
+
if (state.scopes.length > 1) state.scopes.shift();
|
284
|
+
state.dedent -= 1;
|
285
|
+
}
|
286
|
+
|
287
|
+
return style;
|
288
|
+
}
|
289
|
+
|
290
|
+
var external = {
|
291
|
+
startState: function(basecolumn) {
|
292
|
+
return {
|
293
|
+
tokenize: tokenBase,
|
294
|
+
scopes: [{offset:basecolumn || 0, type:'coffee'}],
|
295
|
+
lastToken: null,
|
296
|
+
lambda: false,
|
297
|
+
dedent: 0
|
298
|
+
};
|
299
|
+
},
|
300
|
+
|
301
|
+
token: function(stream, state) {
|
302
|
+
var style = tokenLexer(stream, state);
|
303
|
+
|
304
|
+
state.lastToken = {style:style, content: stream.current()};
|
305
|
+
|
306
|
+
if (stream.eol() && stream.lambda) {
|
307
|
+
state.lambda = false;
|
308
|
+
}
|
309
|
+
|
310
|
+
return style;
|
311
|
+
},
|
312
|
+
|
313
|
+
indent: function(state, textAfter) {
|
314
|
+
if (state.tokenize != tokenBase) {
|
315
|
+
return 0;
|
316
|
+
}
|
317
|
+
|
318
|
+
return state.scopes[0].offset;
|
319
|
+
}
|
320
|
+
|
321
|
+
};
|
322
|
+
return external;
|
323
|
+
});
|
324
|
+
|
325
|
+
CodeMirror.defineMIME('text/x-coffeescript', 'coffeescript');
|
@@ -0,0 +1,722 @@
|
|
1
|
+
<!doctype html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>CodeMirror 2: CoffeeScript mode</title>
|
5
|
+
<link rel="stylesheet" href="../../lib/codemirror.css">
|
6
|
+
<script src="../../lib/codemirror.js"></script>
|
7
|
+
<script src="coffeescript.js"></script>
|
8
|
+
<link rel="stylesheet" href="../../theme/default.css">
|
9
|
+
<style>.CodeMirror {border-top: 1px solid silver; border-bottom: 1px solid silver;}</style>
|
10
|
+
<link rel="stylesheet" href="../../css/docs.css">
|
11
|
+
</head>
|
12
|
+
<body>
|
13
|
+
<h1>CodeMirror 2: CoffeeScript mode</h1>
|
14
|
+
<form><textarea id="code" name="code">
|
15
|
+
# CoffeeScript mode for CodeMirror
|
16
|
+
# Copyright (c) 2011 Jeff Pickhardt, released under
|
17
|
+
# the MIT License.
|
18
|
+
#
|
19
|
+
# Modified from the Python CodeMirror mode, which also is
|
20
|
+
# under the MIT License Copyright (c) 2010 Timothy Farrell.
|
21
|
+
#
|
22
|
+
# The following script, Underscore.coffee, is used to
|
23
|
+
# demonstrate CoffeeScript mode for CodeMirror.
|
24
|
+
#
|
25
|
+
# To download CoffeeScript mode for CodeMirror, go to:
|
26
|
+
# https://github.com/pickhardt/coffeescript-codemirror-mode
|
27
|
+
|
28
|
+
# **Underscore.coffee
|
29
|
+
# (c) 2011 Jeremy Ashkenas, DocumentCloud Inc.**
|
30
|
+
# Underscore is freely distributable under the terms of the
|
31
|
+
# [MIT license](http://en.wikipedia.org/wiki/MIT_License).
|
32
|
+
# Portions of Underscore are inspired by or borrowed from
|
33
|
+
# [Prototype.js](http://prototypejs.org/api), Oliver Steele's
|
34
|
+
# [Functional](http://osteele.com), and John Resig's
|
35
|
+
# [Micro-Templating](http://ejohn.org).
|
36
|
+
# For all details and documentation:
|
37
|
+
# http://documentcloud.github.com/underscore/
|
38
|
+
|
39
|
+
|
40
|
+
# Baseline setup
|
41
|
+
# --------------
|
42
|
+
|
43
|
+
# Establish the root object, `window` in the browser, or `global` on the server.
|
44
|
+
root = this
|
45
|
+
|
46
|
+
|
47
|
+
# Save the previous value of the `_` variable.
|
48
|
+
previousUnderscore = root._
|
49
|
+
|
50
|
+
|
51
|
+
# Establish the object that gets thrown to break out of a loop iteration.
|
52
|
+
# `StopIteration` is SOP on Mozilla.
|
53
|
+
breaker = if typeof(StopIteration) is 'undefined' then '__break__' else StopIteration
|
54
|
+
|
55
|
+
|
56
|
+
# Helper function to escape **RegExp** contents, because JS doesn't have one.
|
57
|
+
escapeRegExp = (string) -> string.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1')
|
58
|
+
|
59
|
+
|
60
|
+
# Save bytes in the minified (but not gzipped) version:
|
61
|
+
ArrayProto = Array.prototype
|
62
|
+
ObjProto = Object.prototype
|
63
|
+
|
64
|
+
|
65
|
+
# Create quick reference variables for speed access to core prototypes.
|
66
|
+
slice = ArrayProto.slice
|
67
|
+
unshift = ArrayProto.unshift
|
68
|
+
toString = ObjProto.toString
|
69
|
+
hasOwnProperty = ObjProto.hasOwnProperty
|
70
|
+
propertyIsEnumerable = ObjProto.propertyIsEnumerable
|
71
|
+
|
72
|
+
|
73
|
+
# All **ECMA5** native implementations we hope to use are declared here.
|
74
|
+
nativeForEach = ArrayProto.forEach
|
75
|
+
nativeMap = ArrayProto.map
|
76
|
+
nativeReduce = ArrayProto.reduce
|
77
|
+
nativeReduceRight = ArrayProto.reduceRight
|
78
|
+
nativeFilter = ArrayProto.filter
|
79
|
+
nativeEvery = ArrayProto.every
|
80
|
+
nativeSome = ArrayProto.some
|
81
|
+
nativeIndexOf = ArrayProto.indexOf
|
82
|
+
nativeLastIndexOf = ArrayProto.lastIndexOf
|
83
|
+
nativeIsArray = Array.isArray
|
84
|
+
nativeKeys = Object.keys
|
85
|
+
|
86
|
+
|
87
|
+
# Create a safe reference to the Underscore object for use below.
|
88
|
+
_ = (obj) -> new wrapper(obj)
|
89
|
+
|
90
|
+
|
91
|
+
# Export the Underscore object for **CommonJS**.
|
92
|
+
if typeof(exports) != 'undefined' then exports._ = _
|
93
|
+
|
94
|
+
|
95
|
+
# Export Underscore to global scope.
|
96
|
+
root._ = _
|
97
|
+
|
98
|
+
|
99
|
+
# Current version.
|
100
|
+
_.VERSION = '1.1.0'
|
101
|
+
|
102
|
+
|
103
|
+
# Collection Functions
|
104
|
+
# --------------------
|
105
|
+
|
106
|
+
# The cornerstone, an **each** implementation.
|
107
|
+
# Handles objects implementing **forEach**, arrays, and raw objects.
|
108
|
+
_.each = (obj, iterator, context) ->
|
109
|
+
try
|
110
|
+
if nativeForEach and obj.forEach is nativeForEach
|
111
|
+
obj.forEach iterator, context
|
112
|
+
else if _.isNumber obj.length
|
113
|
+
iterator.call context, obj[i], i, obj for i in [0...obj.length]
|
114
|
+
else
|
115
|
+
iterator.call context, val, key, obj for own key, val of obj
|
116
|
+
catch e
|
117
|
+
throw e if e isnt breaker
|
118
|
+
obj
|
119
|
+
|
120
|
+
|
121
|
+
# Return the results of applying the iterator to each element. Use JavaScript
|
122
|
+
# 1.6's version of **map**, if possible.
|
123
|
+
_.map = (obj, iterator, context) ->
|
124
|
+
return obj.map(iterator, context) if nativeMap and obj.map is nativeMap
|
125
|
+
results = []
|
126
|
+
_.each obj, (value, index, list) ->
|
127
|
+
results.push iterator.call context, value, index, list
|
128
|
+
results
|
129
|
+
|
130
|
+
|
131
|
+
# **Reduce** builds up a single result from a list of values. Also known as
|
132
|
+
# **inject**, or **foldl**. Uses JavaScript 1.8's version of **reduce**, if possible.
|
133
|
+
_.reduce = (obj, iterator, memo, context) ->
|
134
|
+
if nativeReduce and obj.reduce is nativeReduce
|
135
|
+
iterator = _.bind iterator, context if context
|
136
|
+
return obj.reduce iterator, memo
|
137
|
+
_.each obj, (value, index, list) ->
|
138
|
+
memo = iterator.call context, memo, value, index, list
|
139
|
+
memo
|
140
|
+
|
141
|
+
|
142
|
+
# The right-associative version of **reduce**, also known as **foldr**. Uses
|
143
|
+
# JavaScript 1.8's version of **reduceRight**, if available.
|
144
|
+
_.reduceRight = (obj, iterator, memo, context) ->
|
145
|
+
if nativeReduceRight and obj.reduceRight is nativeReduceRight
|
146
|
+
iterator = _.bind iterator, context if context
|
147
|
+
return obj.reduceRight iterator, memo
|
148
|
+
reversed = _.clone(_.toArray(obj)).reverse()
|
149
|
+
_.reduce reversed, iterator, memo, context
|
150
|
+
|
151
|
+
|
152
|
+
# Return the first value which passes a truth test.
|
153
|
+
_.detect = (obj, iterator, context) ->
|
154
|
+
result = null
|
155
|
+
_.each obj, (value, index, list) ->
|
156
|
+
if iterator.call context, value, index, list
|
157
|
+
result = value
|
158
|
+
_.breakLoop()
|
159
|
+
result
|
160
|
+
|
161
|
+
|
162
|
+
# Return all the elements that pass a truth test. Use JavaScript 1.6's
|
163
|
+
# **filter**, if it exists.
|
164
|
+
_.filter = (obj, iterator, context) ->
|
165
|
+
return obj.filter iterator, context if nativeFilter and obj.filter is nativeFilter
|
166
|
+
results = []
|
167
|
+
_.each obj, (value, index, list) ->
|
168
|
+
results.push value if iterator.call context, value, index, list
|
169
|
+
results
|
170
|
+
|
171
|
+
|
172
|
+
# Return all the elements for which a truth test fails.
|
173
|
+
_.reject = (obj, iterator, context) ->
|
174
|
+
results = []
|
175
|
+
_.each obj, (value, index, list) ->
|
176
|
+
results.push value if not iterator.call context, value, index, list
|
177
|
+
results
|
178
|
+
|
179
|
+
|
180
|
+
# Determine whether all of the elements match a truth test. Delegate to
|
181
|
+
# JavaScript 1.6's **every**, if it is present.
|
182
|
+
_.every = (obj, iterator, context) ->
|
183
|
+
iterator ||= _.identity
|
184
|
+
return obj.every iterator, context if nativeEvery and obj.every is nativeEvery
|
185
|
+
result = true
|
186
|
+
_.each obj, (value, index, list) ->
|
187
|
+
_.breakLoop() unless (result = result and iterator.call(context, value, index, list))
|
188
|
+
result
|
189
|
+
|
190
|
+
|
191
|
+
# Determine if at least one element in the object matches a truth test. Use
|
192
|
+
# JavaScript 1.6's **some**, if it exists.
|
193
|
+
_.some = (obj, iterator, context) ->
|
194
|
+
iterator ||= _.identity
|
195
|
+
return obj.some iterator, context if nativeSome and obj.some is nativeSome
|
196
|
+
result = false
|
197
|
+
_.each obj, (value, index, list) ->
|
198
|
+
_.breakLoop() if (result = iterator.call(context, value, index, list))
|
199
|
+
result
|
200
|
+
|
201
|
+
|
202
|
+
# Determine if a given value is included in the array or object,
|
203
|
+
# based on `===`.
|
204
|
+
_.include = (obj, target) ->
|
205
|
+
return _.indexOf(obj, target) isnt -1 if nativeIndexOf and obj.indexOf is nativeIndexOf
|
206
|
+
return true for own key, val of obj when val is target
|
207
|
+
false
|
208
|
+
|
209
|
+
|
210
|
+
# Invoke a method with arguments on every item in a collection.
|
211
|
+
_.invoke = (obj, method) ->
|
212
|
+
args = _.rest arguments, 2
|
213
|
+
(if method then val[method] else val).apply(val, args) for val in obj
|
214
|
+
|
215
|
+
|
216
|
+
# Convenience version of a common use case of **map**: fetching a property.
|
217
|
+
_.pluck = (obj, key) ->
|
218
|
+
_.map(obj, (val) -> val[key])
|
219
|
+
|
220
|
+
|
221
|
+
# Return the maximum item or (item-based computation).
|
222
|
+
_.max = (obj, iterator, context) ->
|
223
|
+
return Math.max.apply(Math, obj) if not iterator and _.isArray(obj)
|
224
|
+
result = computed: -Infinity
|
225
|
+
_.each obj, (value, index, list) ->
|
226
|
+
computed = if iterator then iterator.call(context, value, index, list) else value
|
227
|
+
computed >= result.computed and (result = {value: value, computed: computed})
|
228
|
+
result.value
|
229
|
+
|
230
|
+
|
231
|
+
# Return the minimum element (or element-based computation).
|
232
|
+
_.min = (obj, iterator, context) ->
|
233
|
+
return Math.min.apply(Math, obj) if not iterator and _.isArray(obj)
|
234
|
+
result = computed: Infinity
|
235
|
+
_.each obj, (value, index, list) ->
|
236
|
+
computed = if iterator then iterator.call(context, value, index, list) else value
|
237
|
+
computed < result.computed and (result = {value: value, computed: computed})
|
238
|
+
result.value
|
239
|
+
|
240
|
+
|
241
|
+
# Sort the object's values by a criterion produced by an iterator.
|
242
|
+
_.sortBy = (obj, iterator, context) ->
|
243
|
+
_.pluck(((_.map obj, (value, index, list) ->
|
244
|
+
{value: value, criteria: iterator.call(context, value, index, list)}
|
245
|
+
).sort((left, right) ->
|
246
|
+
a = left.criteria; b = right.criteria
|
247
|
+
if a < b then -1 else if a > b then 1 else 0
|
248
|
+
)), 'value')
|
249
|
+
|
250
|
+
|
251
|
+
# Use a comparator function to figure out at what index an object should
|
252
|
+
# be inserted so as to maintain order. Uses binary search.
|
253
|
+
_.sortedIndex = (array, obj, iterator) ->
|
254
|
+
iterator ||= _.identity
|
255
|
+
low = 0
|
256
|
+
high = array.length
|
257
|
+
while low < high
|
258
|
+
mid = (low + high) >> 1
|
259
|
+
if iterator(array[mid]) < iterator(obj) then low = mid + 1 else high = mid
|
260
|
+
low
|
261
|
+
|
262
|
+
|
263
|
+
# Convert anything iterable into a real, live array.
|
264
|
+
_.toArray = (iterable) ->
|
265
|
+
return [] if (!iterable)
|
266
|
+
return iterable.toArray() if (iterable.toArray)
|
267
|
+
return iterable if (_.isArray(iterable))
|
268
|
+
return slice.call(iterable) if (_.isArguments(iterable))
|
269
|
+
_.values(iterable)
|
270
|
+
|
271
|
+
|
272
|
+
# Return the number of elements in an object.
|
273
|
+
_.size = (obj) -> _.toArray(obj).length
|
274
|
+
|
275
|
+
|
276
|
+
# Array Functions
|
277
|
+
# ---------------
|
278
|
+
|
279
|
+
# Get the first element of an array. Passing `n` will return the first N
|
280
|
+
# values in the array. Aliased as **head**. The `guard` check allows it to work
|
281
|
+
# with **map**.
|
282
|
+
_.first = (array, n, guard) ->
|
283
|
+
if n and not guard then slice.call(array, 0, n) else array[0]
|
284
|
+
|
285
|
+
|
286
|
+
# Returns everything but the first entry of the array. Aliased as **tail**.
|
287
|
+
# Especially useful on the arguments object. Passing an `index` will return
|
288
|
+
# the rest of the values in the array from that index onward. The `guard`
|
289
|
+
# check allows it to work with **map**.
|
290
|
+
_.rest = (array, index, guard) ->
|
291
|
+
slice.call(array, if _.isUndefined(index) or guard then 1 else index)
|
292
|
+
|
293
|
+
|
294
|
+
# Get the last element of an array.
|
295
|
+
_.last = (array) -> array[array.length - 1]
|
296
|
+
|
297
|
+
|
298
|
+
# Trim out all falsy values from an array.
|
299
|
+
_.compact = (array) -> item for item in array when item
|
300
|
+
|
301
|
+
|
302
|
+
# Return a completely flattened version of an array.
|
303
|
+
_.flatten = (array) ->
|
304
|
+
_.reduce array, (memo, value) ->
|
305
|
+
return memo.concat(_.flatten(value)) if _.isArray value
|
306
|
+
memo.push value
|
307
|
+
memo
|
308
|
+
, []
|
309
|
+
|
310
|
+
|
311
|
+
# Return a version of the array that does not contain the specified value(s).
|
312
|
+
_.without = (array) ->
|
313
|
+
values = _.rest arguments
|
314
|
+
val for val in _.toArray(array) when not _.include values, val
|
315
|
+
|
316
|
+
|
317
|
+
# Produce a duplicate-free version of the array. If the array has already
|
318
|
+
# been sorted, you have the option of using a faster algorithm.
|
319
|
+
_.uniq = (array, isSorted) ->
|
320
|
+
memo = []
|
321
|
+
for el, i in _.toArray array
|
322
|
+
memo.push el if i is 0 || (if isSorted is true then _.last(memo) isnt el else not _.include(memo, el))
|
323
|
+
memo
|
324
|
+
|
325
|
+
|
326
|
+
# Produce an array that contains every item shared between all the
|
327
|
+
# passed-in arrays.
|
328
|
+
_.intersect = (array) ->
|
329
|
+
rest = _.rest arguments
|
330
|
+
_.select _.uniq(array), (item) ->
|
331
|
+
_.all rest, (other) ->
|
332
|
+
_.indexOf(other, item) >= 0
|
333
|
+
|
334
|
+
|
335
|
+
# Zip together multiple lists into a single array -- elements that share
|
336
|
+
# an index go together.
|
337
|
+
_.zip = ->
|
338
|
+
length = _.max _.pluck arguments, 'length'
|
339
|
+
results = new Array length
|
340
|
+
for i in [0...length]
|
341
|
+
results[i] = _.pluck arguments, String i
|
342
|
+
results
|
343
|
+
|
344
|
+
|
345
|
+
# If the browser doesn't supply us with **indexOf** (I'm looking at you, MSIE),
|
346
|
+
# we need this function. Return the position of the first occurrence of an
|
347
|
+
# item in an array, or -1 if the item is not included in the array.
|
348
|
+
_.indexOf = (array, item) ->
|
349
|
+
return array.indexOf item if nativeIndexOf and array.indexOf is nativeIndexOf
|
350
|
+
i = 0; l = array.length
|
351
|
+
while l - i
|
352
|
+
if array[i] is item then return i else i++
|
353
|
+
-1
|
354
|
+
|
355
|
+
|
356
|
+
# Provide JavaScript 1.6's **lastIndexOf**, delegating to the native function,
|
357
|
+
# if possible.
|
358
|
+
_.lastIndexOf = (array, item) ->
|
359
|
+
return array.lastIndexOf(item) if nativeLastIndexOf and array.lastIndexOf is nativeLastIndexOf
|
360
|
+
i = array.length
|
361
|
+
while i
|
362
|
+
if array[i] is item then return i else i--
|
363
|
+
-1
|
364
|
+
|
365
|
+
|
366
|
+
# Generate an integer Array containing an arithmetic progression. A port of
|
367
|
+
# [the native Python **range** function](http://docs.python.org/library/functions.html#range).
|
368
|
+
_.range = (start, stop, step) ->
|
369
|
+
a = arguments
|
370
|
+
solo = a.length <= 1
|
371
|
+
i = start = if solo then 0 else a[0]
|
372
|
+
stop = if solo then a[0] else a[1]
|
373
|
+
step = a[2] or 1
|
374
|
+
len = Math.ceil((stop - start) / step)
|
375
|
+
return [] if len <= 0
|
376
|
+
range = new Array len
|
377
|
+
idx = 0
|
378
|
+
loop
|
379
|
+
return range if (if step > 0 then i - stop else stop - i) >= 0
|
380
|
+
range[idx] = i
|
381
|
+
idx++
|
382
|
+
i+= step
|
383
|
+
|
384
|
+
|
385
|
+
# Function Functions
|
386
|
+
# ------------------
|
387
|
+
|
388
|
+
# Create a function bound to a given object (assigning `this`, and arguments,
|
389
|
+
# optionally). Binding with arguments is also known as **curry**.
|
390
|
+
_.bind = (func, obj) ->
|
391
|
+
args = _.rest arguments, 2
|
392
|
+
-> func.apply obj or root, args.concat arguments
|
393
|
+
|
394
|
+
|
395
|
+
# Bind all of an object's methods to that object. Useful for ensuring that
|
396
|
+
# all callbacks defined on an object belong to it.
|
397
|
+
_.bindAll = (obj) ->
|
398
|
+
funcs = if arguments.length > 1 then _.rest(arguments) else _.functions(obj)
|
399
|
+
_.each funcs, (f) -> obj[f] = _.bind obj[f], obj
|
400
|
+
obj
|
401
|
+
|
402
|
+
|
403
|
+
# Delays a function for the given number of milliseconds, and then calls
|
404
|
+
# it with the arguments supplied.
|
405
|
+
_.delay = (func, wait) ->
|
406
|
+
args = _.rest arguments, 2
|
407
|
+
setTimeout((-> func.apply(func, args)), wait)
|
408
|
+
|
409
|
+
|
410
|
+
# Memoize an expensive function by storing its results.
|
411
|
+
_.memoize = (func, hasher) ->
|
412
|
+
memo = {}
|
413
|
+
hasher or= _.identity
|
414
|
+
->
|
415
|
+
key = hasher.apply this, arguments
|
416
|
+
return memo[key] if key of memo
|
417
|
+
memo[key] = func.apply this, arguments
|
418
|
+
|
419
|
+
|
420
|
+
# Defers a function, scheduling it to run after the current call stack has
|
421
|
+
# cleared.
|
422
|
+
_.defer = (func) ->
|
423
|
+
_.delay.apply _, [func, 1].concat _.rest arguments
|
424
|
+
|
425
|
+
|
426
|
+
# Returns the first function passed as an argument to the second,
|
427
|
+
# allowing you to adjust arguments, run code before and after, and
|
428
|
+
# conditionally execute the original function.
|
429
|
+
_.wrap = (func, wrapper) ->
|
430
|
+
-> wrapper.apply wrapper, [func].concat arguments
|
431
|
+
|
432
|
+
|
433
|
+
# Returns a function that is the composition of a list of functions, each
|
434
|
+
# consuming the return value of the function that follows.
|
435
|
+
_.compose = ->
|
436
|
+
funcs = arguments
|
437
|
+
->
|
438
|
+
args = arguments
|
439
|
+
for i in [funcs.length - 1..0] by -1
|
440
|
+
args = [funcs[i].apply(this, args)]
|
441
|
+
args[0]
|
442
|
+
|
443
|
+
|
444
|
+
# Object Functions
|
445
|
+
# ----------------
|
446
|
+
|
447
|
+
# Retrieve the names of an object's properties.
|
448
|
+
_.keys = nativeKeys or (obj) ->
|
449
|
+
return _.range 0, obj.length if _.isArray(obj)
|
450
|
+
key for key, val of obj
|
451
|
+
|
452
|
+
|
453
|
+
# Retrieve the values of an object's properties.
|
454
|
+
_.values = (obj) ->
|
455
|
+
_.map obj, _.identity
|
456
|
+
|
457
|
+
|
458
|
+
# Return a sorted list of the function names available in Underscore.
|
459
|
+
_.functions = (obj) ->
|
460
|
+
_.filter(_.keys(obj), (key) -> _.isFunction(obj[key])).sort()
|
461
|
+
|
462
|
+
|
463
|
+
# Extend a given object with all of the properties in a source object.
|
464
|
+
_.extend = (obj) ->
|
465
|
+
for source in _.rest(arguments)
|
466
|
+
obj[key] = val for key, val of source
|
467
|
+
obj
|
468
|
+
|
469
|
+
|
470
|
+
# Create a (shallow-cloned) duplicate of an object.
|
471
|
+
_.clone = (obj) ->
|
472
|
+
return obj.slice 0 if _.isArray obj
|
473
|
+
_.extend {}, obj
|
474
|
+
|
475
|
+
|
476
|
+
# Invokes interceptor with the obj, and then returns obj.
|
477
|
+
# The primary purpose of this method is to "tap into" a method chain,
|
478
|
+
# in order to perform operations on intermediate results within
|
479
|
+
the chain.
|
480
|
+
_.tap = (obj, interceptor) ->
|
481
|
+
interceptor obj
|
482
|
+
obj
|
483
|
+
|
484
|
+
|
485
|
+
# Perform a deep comparison to check if two objects are equal.
|
486
|
+
_.isEqual = (a, b) ->
|
487
|
+
# Check object identity.
|
488
|
+
return true if a is b
|
489
|
+
# Different types?
|
490
|
+
atype = typeof(a); btype = typeof(b)
|
491
|
+
return false if atype isnt btype
|
492
|
+
# Basic equality test (watch out for coercions).
|
493
|
+
return true if `a == b`
|
494
|
+
# One is falsy and the other truthy.
|
495
|
+
return false if (!a and b) or (a and !b)
|
496
|
+
# One of them implements an `isEqual()`?
|
497
|
+
return a.isEqual(b) if a.isEqual
|
498
|
+
# Check dates' integer values.
|
499
|
+
return a.getTime() is b.getTime() if _.isDate(a) and _.isDate(b)
|
500
|
+
# Both are NaN?
|
501
|
+
return false if _.isNaN(a) and _.isNaN(b)
|
502
|
+
# Compare regular expressions.
|
503
|
+
if _.isRegExp(a) and _.isRegExp(b)
|
504
|
+
return a.source is b.source and
|
505
|
+
a.global is b.global and
|
506
|
+
a.ignoreCase is b.ignoreCase and
|
507
|
+
a.multiline is b.multiline
|
508
|
+
# If a is not an object by this point, we can't handle it.
|
509
|
+
return false if atype isnt 'object'
|
510
|
+
# Check for different array lengths before comparing contents.
|
511
|
+
return false if a.length and (a.length isnt b.length)
|
512
|
+
# Nothing else worked, deep compare the contents.
|
513
|
+
aKeys = _.keys(a); bKeys = _.keys(b)
|
514
|
+
# Different object sizes?
|
515
|
+
return false if aKeys.length isnt bKeys.length
|
516
|
+
# Recursive comparison of contents.
|
517
|
+
return false for key, val of a when !(key of b) or !_.isEqual(val, b[key])
|
518
|
+
true
|
519
|
+
|
520
|
+
|
521
|
+
# Is a given array or object empty?
|
522
|
+
_.isEmpty = (obj) ->
|
523
|
+
return obj.length is 0 if _.isArray(obj) or _.isString(obj)
|
524
|
+
return false for own key of obj
|
525
|
+
true
|
526
|
+
|
527
|
+
|
528
|
+
# Is a given value a DOM element?
|
529
|
+
_.isElement = (obj) -> obj and obj.nodeType is 1
|
530
|
+
|
531
|
+
|
532
|
+
# Is a given value an array?
|
533
|
+
_.isArray = nativeIsArray or (obj) -> !!(obj and obj.concat and obj.unshift and not obj.callee)
|
534
|
+
|
535
|
+
|
536
|
+
# Is a given variable an arguments object?
|
537
|
+
_.isArguments = (obj) -> obj and obj.callee
|
538
|
+
|
539
|
+
|
540
|
+
# Is the given value a function?
|
541
|
+
_.isFunction = (obj) -> !!(obj and obj.constructor and obj.call and obj.apply)
|
542
|
+
|
543
|
+
|
544
|
+
# Is the given value a string?
|
545
|
+
_.isString = (obj) -> !!(obj is '' or (obj and obj.charCodeAt and obj.substr))
|
546
|
+
|
547
|
+
|
548
|
+
# Is a given value a number?
|
549
|
+
_.isNumber = (obj) -> (obj is +obj) or toString.call(obj) is '[object Number]'
|
550
|
+
|
551
|
+
|
552
|
+
# Is a given value a boolean?
|
553
|
+
_.isBoolean = (obj) -> obj is true or obj is false
|
554
|
+
|
555
|
+
|
556
|
+
# Is a given value a Date?
|
557
|
+
_.isDate = (obj) -> !!(obj and obj.getTimezoneOffset and obj.setUTCFullYear)
|
558
|
+
|
559
|
+
|
560
|
+
# Is the given value a regular expression?
|
561
|
+
_.isRegExp = (obj) -> !!(obj and obj.exec and (obj.ignoreCase or obj.ignoreCase is false))
|
562
|
+
|
563
|
+
|
564
|
+
# Is the given value NaN -- this one is interesting. `NaN != NaN`, and
|
565
|
+
# `isNaN(undefined) == true`, so we make sure it's a number first.
|
566
|
+
_.isNaN = (obj) -> _.isNumber(obj) and window.isNaN(obj)
|
567
|
+
|
568
|
+
|
569
|
+
# Is a given value equal to null?
|
570
|
+
_.isNull = (obj) -> obj is null
|
571
|
+
|
572
|
+
|
573
|
+
# Is a given variable undefined?
|
574
|
+
_.isUndefined = (obj) -> typeof obj is 'undefined'
|
575
|
+
|
576
|
+
|
577
|
+
# Utility Functions
|
578
|
+
# -----------------
|
579
|
+
|
580
|
+
# Run Underscore.js in noConflict mode, returning the `_` variable to its
|
581
|
+
# previous owner. Returns a reference to the Underscore object.
|
582
|
+
_.noConflict = ->
|
583
|
+
root._ = previousUnderscore
|
584
|
+
this
|
585
|
+
|
586
|
+
|
587
|
+
# Keep the identity function around for default iterators.
|
588
|
+
_.identity = (value) -> value
|
589
|
+
|
590
|
+
|
591
|
+
# Run a function `n` times.
|
592
|
+
_.times = (n, iterator, context) ->
|
593
|
+
iterator.call context, i for i in [0...n]
|
594
|
+
|
595
|
+
|
596
|
+
# Break out of the middle of an iteration.
|
597
|
+
_.breakLoop = -> throw breaker
|
598
|
+
|
599
|
+
|
600
|
+
# Add your own custom functions to the Underscore object, ensuring that
|
601
|
+
# they're correctly added to the OOP wrapper as well.
|
602
|
+
_.mixin = (obj) ->
|
603
|
+
for name in _.functions(obj)
|
604
|
+
addToWrapper name, _[name] = obj[name]
|
605
|
+
|
606
|
+
|
607
|
+
# Generate a unique integer id (unique within the entire client session).
|
608
|
+
# Useful for temporary DOM ids.
|
609
|
+
idCounter = 0
|
610
|
+
_.uniqueId = (prefix) ->
|
611
|
+
(prefix or '') + idCounter++
|
612
|
+
|
613
|
+
|
614
|
+
# By default, Underscore uses **ERB**-style template delimiters, change the
|
615
|
+
# following template settings to use alternative delimiters.
|
616
|
+
_.templateSettings = {
|
617
|
+
start: '<%'
|
618
|
+
end: '%>'
|
619
|
+
interpolate: /<%=(.+?)%>/g
|
620
|
+
}
|
621
|
+
|
622
|
+
|
623
|
+
# JavaScript templating a-la **ERB**, pilfered from John Resig's
|
624
|
+
# *Secrets of the JavaScript Ninja*, page 83.
|
625
|
+
# Single-quote fix from Rick Strahl.
|
626
|
+
# With alterations for arbitrary delimiters, and to preserve whitespace.
|
627
|
+
_.template = (str, data) ->
|
628
|
+
c = _.templateSettings
|
629
|
+
endMatch = new RegExp("'(?=[^"+c.end.substr(0, 1)+"]*"+escapeRegExp(c.end)+")","g")
|
630
|
+
fn = new Function 'obj',
|
631
|
+
'var p=[],print=function(){p.push.apply(p,arguments);};' +
|
632
|
+
'with(obj||{}){p.push(\'' +
|
633
|
+
str.replace(/\r/g, '\\r')
|
634
|
+
.replace(/\n/g, '\\n')
|
635
|
+
.replace(/\t/g, '\\t')
|
636
|
+
.replace(endMatch,"���")
|
637
|
+
.split("'").join("\\'")
|
638
|
+
.split("���").join("'")
|
639
|
+
.replace(c.interpolate, "',$1,'")
|
640
|
+
.split(c.start).join("');")
|
641
|
+
.split(c.end).join("p.push('") +
|
642
|
+
"');}return p.join('');"
|
643
|
+
if data then fn(data) else fn
|
644
|
+
|
645
|
+
|
646
|
+
# Aliases
|
647
|
+
# -------
|
648
|
+
|
649
|
+
_.forEach = _.each
|
650
|
+
_.foldl = _.inject = _.reduce
|
651
|
+
_.foldr = _.reduceRight
|
652
|
+
_.select = _.filter
|
653
|
+
_.all = _.every
|
654
|
+
_.any = _.some
|
655
|
+
_.contains = _.include
|
656
|
+
_.head = _.first
|
657
|
+
_.tail = _.rest
|
658
|
+
_.methods = _.functions
|
659
|
+
|
660
|
+
|
661
|
+
# Setup the OOP Wrapper
|
662
|
+
# ---------------------
|
663
|
+
|
664
|
+
# If Underscore is called as a function, it returns a wrapped object that
|
665
|
+
# can be used OO-style. This wrapper holds altered versions of all the
|
666
|
+
# underscore functions. Wrapped objects may be chained.
|
667
|
+
wrapper = (obj) ->
|
668
|
+
this._wrapped = obj
|
669
|
+
this
|
670
|
+
|
671
|
+
|
672
|
+
# Helper function to continue chaining intermediate results.
|
673
|
+
result = (obj, chain) ->
|
674
|
+
if chain then _(obj).chain() else obj
|
675
|
+
|
676
|
+
|
677
|
+
# A method to easily add functions to the OOP wrapper.
|
678
|
+
addToWrapper = (name, func) ->
|
679
|
+
wrapper.prototype[name] = ->
|
680
|
+
args = _.toArray arguments
|
681
|
+
unshift.call args, this._wrapped
|
682
|
+
result func.apply(_, args), this._chain
|
683
|
+
|
684
|
+
|
685
|
+
# Add all ofthe Underscore functions to the wrapper object.
|
686
|
+
_.mixin _
|
687
|
+
|
688
|
+
|
689
|
+
# Add all mutator Array functions to the wrapper.
|
690
|
+
_.each ['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], (name) ->
|
691
|
+
method = Array.prototype[name]
|
692
|
+
wrapper.prototype[name] = ->
|
693
|
+
method.apply(this._wrapped, arguments)
|
694
|
+
result(this._wrapped, this._chain)
|
695
|
+
|
696
|
+
|
697
|
+
# Add all accessor Array functions to the wrapper.
|
698
|
+
_.each ['concat', 'join', 'slice'], (name) ->
|
699
|
+
method = Array.prototype[name]
|
700
|
+
wrapper.prototype[name] = ->
|
701
|
+
result(method.apply(this._wrapped, arguments), this._chain)
|
702
|
+
|
703
|
+
|
704
|
+
# Start chaining a wrapped Underscore object.
|
705
|
+
wrapper::chain = ->
|
706
|
+
this._chain = true
|
707
|
+
this
|
708
|
+
|
709
|
+
|
710
|
+
# Extracts the result from a wrapped and chained object.
|
711
|
+
wrapper::value = -> this._wrapped
|
712
|
+
</textarea></form>
|
713
|
+
<script>
|
714
|
+
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {});
|
715
|
+
</script>
|
716
|
+
|
717
|
+
<p><strong>MIME types defined:</strong> <code>text/x-coffeescript</code>.</p>
|
718
|
+
|
719
|
+
<p>The CoffeeScript mode was written by Jeff Pickhardt (<a href="LICENSE">license</a>).</p>
|
720
|
+
|
721
|
+
</body>
|
722
|
+
</html>
|