liquid_cms 0.2.1.1 → 0.2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (130) hide show
  1. data/CHANGELOG.rdoc +7 -0
  2. data/README.rdoc +15 -1
  3. data/TODO.rdoc +0 -2
  4. data/app/controllers/cms/assets_controller.rb +20 -6
  5. data/app/controllers/cms/components_controller.rb +11 -3
  6. data/app/controllers/cms/pages_controller.rb +15 -4
  7. data/app/helpers/cms/common_helper.rb +31 -31
  8. data/app/liquid/cms_paperclip_extension.rb +75 -3
  9. data/app/models/cms/asset.rb +1 -1
  10. data/app/models/cms/component.rb +3 -3
  11. data/app/views/cms/assets/_form.html.erb +1 -1
  12. data/app/views/cms/assets/update.js.rjs +1 -0
  13. data/app/views/cms/components/update.js.rjs +1 -0
  14. data/app/views/cms/pages/_form.html.erb +1 -1
  15. data/app/views/cms/pages/update.js.rjs +1 -0
  16. data/app/views/layouts/cms.html.erb +4 -3
  17. data/generators/liquid_cms/liquid_cms_generator.rb +1 -0
  18. data/generators/liquid_cms/templates/config/locales/cms/en.yml +4 -0
  19. data/generators/liquid_cms/templates/migration_rev2.rb +13 -0
  20. data/generators/liquid_cms/templates/public/cms/codemirror/LICENSE +16 -20
  21. data/generators/liquid_cms/templates/public/cms/codemirror/lib/codemirror.css +68 -0
  22. data/generators/liquid_cms/templates/public/cms/codemirror/lib/codemirror.js +2197 -0
  23. data/generators/liquid_cms/templates/public/cms/codemirror/lib/overlay.js +51 -0
  24. data/generators/liquid_cms/templates/public/cms/codemirror/lib/runmode.js +27 -0
  25. data/generators/liquid_cms/templates/public/cms/codemirror/mode/clike/clike.js +247 -0
  26. data/generators/liquid_cms/templates/public/cms/codemirror/mode/clike/index.html +102 -0
  27. data/generators/liquid_cms/templates/public/cms/codemirror/mode/clojure/clojure.js +207 -0
  28. data/generators/liquid_cms/templates/public/cms/codemirror/mode/clojure/index.html +85 -0
  29. data/generators/liquid_cms/templates/public/cms/codemirror/mode/coffeescript/LICENSE +22 -0
  30. data/generators/liquid_cms/templates/public/cms/codemirror/mode/coffeescript/coffeescript.js +325 -0
  31. data/generators/liquid_cms/templates/public/cms/codemirror/mode/coffeescript/index.html +722 -0
  32. data/generators/liquid_cms/templates/public/cms/codemirror/mode/css/css.js +124 -0
  33. data/generators/liquid_cms/templates/public/cms/codemirror/mode/css/index.html +56 -0
  34. data/generators/liquid_cms/templates/public/cms/codemirror/mode/diff/diff.css +3 -0
  35. data/generators/liquid_cms/templates/public/cms/codemirror/mode/diff/diff.js +13 -0
  36. data/generators/liquid_cms/templates/public/cms/codemirror/mode/diff/index.html +99 -0
  37. data/generators/liquid_cms/templates/public/cms/codemirror/mode/haskell/haskell.js +242 -0
  38. data/generators/liquid_cms/templates/public/cms/codemirror/mode/haskell/index.html +60 -0
  39. data/generators/liquid_cms/templates/public/cms/codemirror/mode/htmlmixed/htmlmixed.js +79 -0
  40. data/generators/liquid_cms/templates/public/cms/codemirror/mode/htmlmixed/index.html +52 -0
  41. data/generators/liquid_cms/templates/public/cms/codemirror/mode/javascript/index.html +78 -0
  42. data/generators/liquid_cms/templates/public/cms/codemirror/mode/javascript/javascript.js +352 -0
  43. data/generators/liquid_cms/templates/public/cms/codemirror/mode/jinja2/index.html +38 -0
  44. data/generators/liquid_cms/templates/public/cms/codemirror/mode/jinja2/jinja2.js +42 -0
  45. data/generators/liquid_cms/templates/public/cms/codemirror/mode/lua/index.html +72 -0
  46. data/generators/liquid_cms/templates/public/cms/codemirror/mode/lua/lua.js +140 -0
  47. data/generators/liquid_cms/templates/public/cms/codemirror/mode/markdown/index.html +340 -0
  48. data/generators/liquid_cms/templates/public/cms/codemirror/mode/markdown/markdown.css +10 -0
  49. data/generators/liquid_cms/templates/public/cms/codemirror/mode/markdown/markdown.js +230 -0
  50. data/generators/liquid_cms/templates/public/cms/codemirror/mode/ntriples/index.html +33 -0
  51. data/generators/liquid_cms/templates/public/cms/codemirror/mode/ntriples/ntriples.js +172 -0
  52. data/generators/liquid_cms/templates/public/cms/codemirror/mode/pascal/LICENSE +7 -0
  53. data/generators/liquid_cms/templates/public/cms/codemirror/mode/pascal/index.html +49 -0
  54. data/generators/liquid_cms/templates/public/cms/codemirror/mode/pascal/pascal.js +138 -0
  55. data/generators/liquid_cms/templates/public/cms/codemirror/mode/php/index.html +49 -0
  56. data/generators/liquid_cms/templates/public/cms/codemirror/mode/php/php.js +116 -0
  57. data/generators/liquid_cms/templates/public/cms/codemirror/mode/plsql/index.html +63 -0
  58. data/generators/liquid_cms/templates/public/cms/codemirror/mode/plsql/plsql.js +217 -0
  59. data/generators/liquid_cms/templates/public/cms/codemirror/mode/python/LICENSE.txt +21 -0
  60. data/generators/liquid_cms/templates/public/cms/codemirror/mode/python/index.html +123 -0
  61. data/generators/liquid_cms/templates/public/cms/codemirror/mode/python/python.js +320 -0
  62. data/generators/liquid_cms/templates/public/cms/codemirror/mode/r/LICENSE +24 -0
  63. data/generators/liquid_cms/templates/public/cms/codemirror/mode/r/index.html +74 -0
  64. data/generators/liquid_cms/templates/public/cms/codemirror/mode/r/r.js +141 -0
  65. data/generators/liquid_cms/templates/public/cms/codemirror/mode/rst/index.html +526 -0
  66. data/generators/liquid_cms/templates/public/cms/codemirror/mode/rst/rst.css +75 -0
  67. data/generators/liquid_cms/templates/public/cms/codemirror/mode/rst/rst.js +333 -0
  68. data/generators/liquid_cms/templates/public/cms/codemirror/mode/ruby/LICENSE +24 -0
  69. data/generators/liquid_cms/templates/public/cms/codemirror/mode/ruby/index.html +172 -0
  70. data/generators/liquid_cms/templates/public/cms/codemirror/mode/ruby/ruby.js +195 -0
  71. data/generators/liquid_cms/templates/public/cms/codemirror/mode/scheme/index.html +65 -0
  72. data/generators/liquid_cms/templates/public/cms/codemirror/mode/scheme/scheme.js +202 -0
  73. data/generators/liquid_cms/templates/public/cms/codemirror/mode/smalltalk/index.html +56 -0
  74. data/generators/liquid_cms/templates/public/cms/codemirror/mode/smalltalk/smalltalk.js +122 -0
  75. data/generators/liquid_cms/templates/public/cms/codemirror/mode/sparql/index.html +41 -0
  76. data/generators/liquid_cms/templates/public/cms/codemirror/mode/sparql/sparql.js +143 -0
  77. data/generators/liquid_cms/templates/public/cms/codemirror/mode/stex/index.html +96 -0
  78. data/generators/liquid_cms/templates/public/cms/codemirror/mode/stex/stex.js +167 -0
  79. data/generators/liquid_cms/templates/public/cms/codemirror/mode/velocity/index.html +103 -0
  80. data/generators/liquid_cms/templates/public/cms/codemirror/mode/velocity/velocity.js +146 -0
  81. data/generators/liquid_cms/templates/public/cms/codemirror/mode/xml/index.html +42 -0
  82. data/generators/liquid_cms/templates/public/cms/codemirror/mode/xml/xml.js +231 -0
  83. data/generators/liquid_cms/templates/public/cms/codemirror/mode/xmlpure/index.html +60 -0
  84. data/generators/liquid_cms/templates/public/cms/codemirror/mode/xmlpure/xmlpure.js +481 -0
  85. data/generators/liquid_cms/templates/public/cms/codemirror/mode/yaml/index.html +68 -0
  86. data/generators/liquid_cms/templates/public/cms/codemirror/mode/yaml/yaml.js +95 -0
  87. data/generators/liquid_cms/templates/public/cms/codemirror/theme/cobalt.css +17 -0
  88. data/generators/liquid_cms/templates/public/cms/codemirror/theme/default.css +19 -0
  89. data/generators/liquid_cms/templates/public/cms/codemirror/theme/eclipse.css +24 -0
  90. data/generators/liquid_cms/templates/public/cms/codemirror/theme/elegant.css +9 -0
  91. data/generators/liquid_cms/templates/public/cms/codemirror/theme/neat.css +8 -0
  92. data/generators/liquid_cms/templates/public/cms/codemirror/theme/night.css +20 -0
  93. data/generators/liquid_cms/templates/public/cms/javascripts/cms.js +1 -1
  94. data/generators/liquid_cms/templates/public/cms/javascripts/codemirror_custom.js +96 -0
  95. data/generators/liquid_cms/templates/public/cms/stylesheets/codemirror_changes.css +26 -0
  96. data/generators/liquid_cms/templates/public/cms/stylesheets/liquid.css +7 -0
  97. data/generators/liquid_cms/templates/public/cms/stylesheets/simple_form.css +0 -8
  98. data/lib/liquid_cms/version.rb +1 -1
  99. data/liquid_cms.gemspec +0 -1
  100. data/test/functional/assets_controller_test.rb +35 -20
  101. data/test/functional/components_controller_test.rb +15 -5
  102. data/test/functional/pages_controller_test.rb +19 -6
  103. data/test/rails_app/config/environment.rb +1 -1
  104. data/test/rails_app/config/locales/cms/en.yml +19 -4
  105. data/test/rails_app/db/migrate/20110511161859_create_liquid_cms_upgrade_rev2.rb +13 -0
  106. data/test/unit/asset_test.rb +1 -1
  107. metadata +100 -58
  108. data/generators/liquid_cms/templates/public/cms/codemirror/css/csscolors.css +0 -55
  109. data/generators/liquid_cms/templates/public/cms/codemirror/css/docs.css +0 -158
  110. data/generators/liquid_cms/templates/public/cms/codemirror/css/font.js +0 -15
  111. data/generators/liquid_cms/templates/public/cms/codemirror/css/jscolors.css +0 -59
  112. data/generators/liquid_cms/templates/public/cms/codemirror/css/sparqlcolors.css +0 -43
  113. data/generators/liquid_cms/templates/public/cms/codemirror/css/xmlcolors.css +0 -55
  114. data/generators/liquid_cms/templates/public/cms/codemirror/js/codemirror.js +0 -582
  115. data/generators/liquid_cms/templates/public/cms/codemirror/js/editor.js +0 -1671
  116. data/generators/liquid_cms/templates/public/cms/codemirror/js/highlight.js +0 -68
  117. data/generators/liquid_cms/templates/public/cms/codemirror/js/mirrorframe.js +0 -81
  118. data/generators/liquid_cms/templates/public/cms/codemirror/js/parsecss.js +0 -161
  119. data/generators/liquid_cms/templates/public/cms/codemirror/js/parsedummy.js +0 -32
  120. data/generators/liquid_cms/templates/public/cms/codemirror/js/parsehtmlmixed.js +0 -93
  121. data/generators/liquid_cms/templates/public/cms/codemirror/js/parsejavascript.js +0 -359
  122. data/generators/liquid_cms/templates/public/cms/codemirror/js/parsesparql.js +0 -162
  123. data/generators/liquid_cms/templates/public/cms/codemirror/js/parsexml.js +0 -291
  124. data/generators/liquid_cms/templates/public/cms/codemirror/js/select.js +0 -699
  125. data/generators/liquid_cms/templates/public/cms/codemirror/js/stringstream.js +0 -159
  126. data/generators/liquid_cms/templates/public/cms/codemirror/js/tokenize.js +0 -57
  127. data/generators/liquid_cms/templates/public/cms/codemirror/js/tokenizejavascript.js +0 -174
  128. data/generators/liquid_cms/templates/public/cms/codemirror/js/undo.js +0 -413
  129. data/generators/liquid_cms/templates/public/cms/codemirror/js/unittests.js +0 -44
  130. data/generators/liquid_cms/templates/public/cms/codemirror/js/util.js +0 -133
@@ -0,0 +1,333 @@
1
+ CodeMirror.defineMode('rst', function(config, options) {
2
+ function setState(state, fn, ctx) {
3
+ state.fn = fn;
4
+ setCtx(state, ctx);
5
+ }
6
+
7
+ function setCtx(state, ctx) {
8
+ state.ctx = ctx || {};
9
+ }
10
+
11
+ function setNormal(state, ch) {
12
+ if (ch && (typeof ch !== 'string')) {
13
+ var str = ch.current();
14
+ ch = str[str.length-1];
15
+ }
16
+
17
+ setState(state, normal, {back: ch});
18
+ }
19
+
20
+ function hasMode(mode) {
21
+ if (mode) {
22
+ var modes = CodeMirror.listModes();
23
+
24
+ for (var i in modes) {
25
+ if (modes[i] == mode) {
26
+ return true;
27
+ }
28
+ }
29
+ }
30
+
31
+ return false;
32
+ }
33
+
34
+ function getMode(mode) {
35
+ if (hasMode(mode)) {
36
+ return CodeMirror.getMode(config, mode);
37
+ } else {
38
+ return null;
39
+ }
40
+ }
41
+
42
+ var verbatimMode = getMode(options.verbatim);
43
+ var pythonMode = getMode('python');
44
+
45
+ var reSection = /^[!"#$%&'()*+,-./:;<=>?@[\\\]^_`{|}~]/;
46
+ var reDirective = /^\s*\w([-:.\w]*\w)?::(\s|$)/;
47
+ var reHyperlink = /^\s*_[\w-]+:(\s|$)/;
48
+ var reFootnote = /^\s*\[(\d+|#)\](\s|$)/;
49
+ var reCitation = /^\s*\[[A-Za-z][\w-]*\](\s|$)/;
50
+ var reFootnoteRef = /^\[(\d+|#)\]_/;
51
+ var reCitationRef = /^\[[A-Za-z][\w-]*\]_/;
52
+ var reDirectiveMarker = /^\.\.(\s|$)/;
53
+ var reVerbatimMarker = /^::\s*$/;
54
+ var rePreInline = /^[-\s"([{</:]/;
55
+ var rePostInline = /^[-\s`'")\]}>/:.,;!?\\_]/;
56
+ var reEnumeratedList = /^\s*((\d+|[A-Za-z#])[.)]|\((\d+|[A-Z-a-z#])\))\s/;
57
+ var reBulletedList = /^\s*[-\+\*]\s/;
58
+ var reExamples = /^\s+(>>>|In \[\d+\]:)\s/;
59
+
60
+ function normal(stream, state) {
61
+ var ch, sol, i;
62
+
63
+ if (stream.eat(/\\/)) {
64
+ ch = stream.next();
65
+ setNormal(state, ch);
66
+ return null;
67
+ }
68
+
69
+ sol = stream.sol();
70
+
71
+ if (sol && (ch = stream.eat(reSection))) {
72
+ for (i = 0; stream.eat(ch); i++);
73
+
74
+ if (i >= 3 && stream.match(/^\s*$/)) {
75
+ setNormal(state, null);
76
+ return 'section';
77
+ } else {
78
+ stream.backUp(i + 1);
79
+ }
80
+ }
81
+
82
+ if (sol && stream.match(reDirectiveMarker)) {
83
+ if (!stream.eol()) {
84
+ setState(state, directive);
85
+ }
86
+
87
+ return 'directive-marker';
88
+ }
89
+
90
+ if (stream.match(reVerbatimMarker)) {
91
+ if (!verbatimMode) {
92
+ setState(state, verbatim);
93
+ } else {
94
+ var mode = verbatimMode;
95
+
96
+ setState(state, verbatim, {
97
+ mode: mode,
98
+ local: mode.startState()
99
+ });
100
+ }
101
+
102
+ return 'verbatim-marker';
103
+ }
104
+
105
+ if (sol && stream.match(reExamples, false)) {
106
+ if (!pythonMode) {
107
+ setState(state, verbatim);
108
+ return 'verbatim-marker';
109
+ } else {
110
+ var mode = pythonMode;
111
+
112
+ setState(state, verbatim, {
113
+ mode: mode,
114
+ local: mode.startState()
115
+ });
116
+
117
+ return null;
118
+ }
119
+ }
120
+
121
+ if (sol && (stream.match(reEnumeratedList) ||
122
+ stream.match(reBulletedList))) {
123
+ setNormal(state, stream);
124
+ return 'list';
125
+ }
126
+
127
+ function testBackward(re) {
128
+ return sol || !state.ctx.back || re.test(state.ctx.back);
129
+ }
130
+
131
+ function testForward(re) {
132
+ return stream.eol() || stream.match(re, false);
133
+ }
134
+
135
+ function testInline(re) {
136
+ return stream.match(re) && testBackward(/\W/) && testForward(/\W/);
137
+ }
138
+
139
+ if (testInline(reFootnoteRef)) {
140
+ setNormal(state, stream);
141
+ return 'footnote';
142
+ }
143
+
144
+ if (testInline(reCitationRef)) {
145
+ setNormal(state, stream);
146
+ return 'citation';
147
+ }
148
+
149
+ ch = stream.next();
150
+
151
+ if (testBackward(rePreInline)) {
152
+ if ((ch === ':' || ch === '|') && stream.eat(/\S/)) {
153
+ var token;
154
+
155
+ if (ch === ':') {
156
+ token = 'role';
157
+ } else {
158
+ token = 'replacement';
159
+ }
160
+
161
+ setState(state, inline, {
162
+ ch: ch,
163
+ wide: false,
164
+ prev: null,
165
+ token: token
166
+ });
167
+
168
+ return token;
169
+ }
170
+
171
+ if (ch === '*' || ch === '`') {
172
+ var orig = ch,
173
+ wide = false;
174
+
175
+ ch = stream.next();
176
+
177
+ if (ch == orig) {
178
+ wide = true;
179
+ ch = stream.next();
180
+ }
181
+
182
+ if (ch && !/\s/.test(ch)) {
183
+ var token;
184
+
185
+ if (orig === '*') {
186
+ token = wide ? 'strong' : 'emphasis';
187
+ } else {
188
+ token = wide ? 'inline' : 'interpreted';
189
+ }
190
+
191
+ setState(state, inline, {
192
+ ch: orig, // inline() has to know what to search for
193
+ wide: wide, // are we looking for `ch` or `chch`
194
+ prev: null, // terminator must not be preceeded with whitespace
195
+ token: token // I don't want to recompute this all the time
196
+ });
197
+
198
+ return token;
199
+ }
200
+ }
201
+ }
202
+
203
+ setNormal(state, ch);
204
+ return null;
205
+ }
206
+
207
+ function inline(stream, state) {
208
+ var ch = stream.next(),
209
+ token = state.ctx.token;
210
+
211
+ function finish(ch) {
212
+ state.ctx.prev = ch;
213
+ return token;
214
+ }
215
+
216
+ if (ch != state.ctx.ch) {
217
+ return finish(ch);
218
+ }
219
+
220
+ if (/\s/.test(state.ctx.prev)) {
221
+ return finish(ch);
222
+ }
223
+
224
+ if (state.ctx.wide) {
225
+ ch = stream.next();
226
+
227
+ if (ch != state.ctx.ch) {
228
+ return finish(ch);
229
+ }
230
+ }
231
+
232
+ if (!stream.eol() && !rePostInline.test(stream.peek())) {
233
+ if (state.ctx.wide) {
234
+ stream.backUp(1);
235
+ }
236
+
237
+ return finish(ch);
238
+ }
239
+
240
+ setState(state, normal);
241
+ setNormal(state, ch);
242
+
243
+ return token;
244
+ }
245
+
246
+ function directive(stream, state) {
247
+ var token = null;
248
+
249
+ if (stream.match(reDirective)) {
250
+ token = 'directive';
251
+ } else if (stream.match(reHyperlink)) {
252
+ token = 'hyperlink';
253
+ } else if (stream.match(reFootnote)) {
254
+ token = 'footnote';
255
+ } else if (stream.match(reCitation)) {
256
+ token = 'citation';
257
+ } else {
258
+ stream.eatSpace();
259
+
260
+ if (stream.eol()) {
261
+ setNormal(state, stream);
262
+ return null;
263
+ } else {
264
+ stream.skipToEnd();
265
+ setState(state, comment);
266
+ return 'comment';
267
+ }
268
+ }
269
+
270
+ setState(state, body, {start: true});
271
+ return token;
272
+ }
273
+
274
+ function body(stream, state) {
275
+ var token = 'body';
276
+
277
+ if (!state.ctx.start || stream.sol()) {
278
+ return block(stream, state, token);
279
+ }
280
+
281
+ stream.skipToEnd();
282
+ setCtx(state);
283
+
284
+ return token;
285
+ }
286
+
287
+ function comment(stream, state) {
288
+ return block(stream, state, 'comment');
289
+ }
290
+
291
+ function verbatim(stream, state) {
292
+ if (!verbatimMode) {
293
+ return block(stream, state, 'verbatim');
294
+ } else {
295
+ if (stream.sol()) {
296
+ if (!stream.eatSpace()) {
297
+ setNormal(state, stream);
298
+ }
299
+
300
+ return null;
301
+ }
302
+
303
+ return verbatimMode.token(stream, state.ctx.local);
304
+ }
305
+ }
306
+
307
+ function block(stream, state, token) {
308
+ if (stream.eol() || stream.eatSpace()) {
309
+ stream.skipToEnd();
310
+ return token;
311
+ } else {
312
+ setNormal(state, stream);
313
+ return null;
314
+ }
315
+ }
316
+
317
+ return {
318
+ startState: function() {
319
+ return {fn: normal, ctx: {}};
320
+ },
321
+
322
+ copyState: function(state) {
323
+ return {fn: state.fn, ctx: state.ctx};
324
+ },
325
+
326
+ token: function(stream, state) {
327
+ var token = state.fn(stream, state);
328
+ return token;
329
+ }
330
+ };
331
+ });
332
+
333
+ CodeMirror.defineMIME("text/x-rst", "rst");
@@ -0,0 +1,24 @@
1
+ Copyright (c) 2011, Ubalo, Inc.
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+ * Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ * Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+ * Neither the name of the Ubalo, Inc. nor the names of its
12
+ contributors may be used to endorse or promote products derived
13
+ from this software without specific prior written permission.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ DISCLAIMED. IN NO EVENT SHALL UBALO, INC BE LIABLE FOR ANY
19
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,172 @@
1
+ <!doctype html>
2
+ <html>
3
+ <head>
4
+ <title>CodeMirror 2: Ruby mode</title>
5
+ <link rel="stylesheet" href="../../lib/codemirror.css">
6
+ <script src="../../lib/codemirror.js"></script>
7
+ <script src="ruby.js"></script>
8
+ <link rel="stylesheet" href="../../theme/default.css">
9
+ <style>
10
+ .CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}
11
+ .cm-s-default span.cm-arrow { color: red; }
12
+ </style>
13
+ <link rel="stylesheet" href="../../css/docs.css">
14
+ </head>
15
+ <body>
16
+ <h1>CodeMirror 2: Ruby mode</h1>
17
+ <form><textarea id="code" name="code">
18
+ # Code from http://sandbox.mc.edu/~bennet/ruby/code/poly_rb.html
19
+ #
20
+ # This program evaluates polynomials. It first asks for the coefficients
21
+ # of a polynomial, which must be entered on one line, highest-order first.
22
+ # It then requests values of x and will compute the value of the poly for
23
+ # each x. It will repeatly ask for x values, unless you the user enters
24
+ # a blank line. It that case, it will ask for another polynomial. If the
25
+ # user types quit for either input, the program immediately exits.
26
+ #
27
+
28
+ #
29
+ # Function to evaluate a polynomial at x. The polynomial is given
30
+ # as a list of coefficients, from the greatest to the least.
31
+ def polyval(x, coef)
32
+ sum = 0
33
+ coef = coef.clone # Don't want to destroy the original
34
+ while true
35
+ sum += coef.shift # Add and remove the next coef
36
+ break if coef.empty? # If no more, done entirely.
37
+ sum *= x # This happens the right number of times.
38
+ end
39
+ return sum
40
+ end
41
+
42
+ #
43
+ # Function to read a line containing a list of integers and return
44
+ # them as an array of integers. If the string conversion fails, it
45
+ # throws TypeError. If the input line is the word 'quit', then it
46
+ # converts it to an end-of-file exception
47
+ def readints(prompt)
48
+ # Read a line
49
+ print prompt
50
+ line = readline.chomp
51
+ raise EOFError.new if line == 'quit' # You can also use a real EOF.
52
+
53
+ # Go through each item on the line, converting each one and adding it
54
+ # to retval.
55
+ retval = [ ]
56
+ for str in line.split(/\s+/)
57
+ if str =~ /^\-?\d+$/
58
+ retval.push(str.to_i)
59
+ else
60
+ raise TypeError.new
61
+ end
62
+ end
63
+
64
+ return retval
65
+ end
66
+
67
+ #
68
+ # Take a coeff and an exponent and return the string representation, ignoring
69
+ # the sign of the coefficient.
70
+ def term_to_str(coef, exp)
71
+ ret = ""
72
+
73
+ # Show coeff, unless it's 1 or at the right
74
+ coef = coef.abs
75
+ ret = coef.to_s unless coef == 1 && exp > 0
76
+ ret += "x" if exp > 0 # x if exponent not 0
77
+ ret += "^" + exp.to_s if exp > 1 # ^exponent, if > 1.
78
+
79
+ return ret
80
+ end
81
+
82
+ #
83
+ # Create a string of the polynomial in sort-of-readable form.
84
+ def polystr(p)
85
+ # Get the exponent of first coefficient, plus 1.
86
+ exp = p.length
87
+
88
+ # Assign exponents to each term, making pairs of coeff and exponent,
89
+ # Then get rid of the zero terms.
90
+ p = (p.map { |c| exp -= 1; [ c, exp ] }).select { |p| p[0] != 0 }
91
+
92
+ # If there's nothing left, it's a zero
93
+ return "0" if p.empty?
94
+
95
+ # *** Now p is a non-empty list of [ coef, exponent ] pairs. ***
96
+
97
+ # Convert the first term, preceded by a "-" if it's negative.
98
+ result = (if p[0][0] < 0 then "-" else "" end) + term_to_str(*p[0])
99
+
100
+ # Convert the rest of the terms, in each case adding the appropriate
101
+ # + or - separating them.
102
+ for term in p[1...p.length]
103
+ # Add the separator then the rep. of the term.
104
+ result += (if term[0] < 0 then " - " else " + " end) +
105
+ term_to_str(*term)
106
+ end
107
+
108
+ return result
109
+ end
110
+
111
+ #
112
+ # Run until some kind of endfile.
113
+ begin
114
+ # Repeat until an exception or quit gets us out.
115
+ while true
116
+ # Read a poly until it works. An EOF will except out of the
117
+ # program.
118
+ print "\n"
119
+ begin
120
+ poly = readints("Enter a polynomial coefficients: ")
121
+ rescue TypeError
122
+ print "Try again.\n"
123
+ retry
124
+ end
125
+ break if poly.empty?
126
+
127
+ # Read and evaluate x values until the user types a blank line.
128
+ # Again, an EOF will except out of the pgm.
129
+ while true
130
+ # Request an integer.
131
+ print "Enter x value or blank line: "
132
+ x = readline.chomp
133
+ break if x == ''
134
+ raise EOFError.new if x == 'quit'
135
+
136
+ # If it looks bad, let's try again.
137
+ if x !~ /^\-?\d+$/
138
+ print "That doesn't look like an integer. Please try again.\n"
139
+ next
140
+ end
141
+
142
+ # Convert to an integer and print the result.
143
+ x = x.to_i
144
+ print "p(x) = ", polystr(poly), "\n"
145
+ print "p(", x, ") = ", polyval(x, poly), "\n"
146
+ end
147
+ end
148
+ rescue EOFError
149
+ print "\n=== EOF ===\n"
150
+ rescue Interrupt, SignalException
151
+ print "\n=== Interrupted ===\n"
152
+ else
153
+ print "--- Bye ---\n"
154
+ end
155
+ </textarea></form>
156
+ <script>
157
+ var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
158
+ mode: "text/x-ruby",
159
+ tabMode: "indent",
160
+ matchBrackets: true,
161
+ indentUnit: 4
162
+ });
163
+ </script>
164
+
165
+ <p><strong>MIME types defined:</strong> <code>text/x-ruby</code>.</p>
166
+
167
+ <p>Development of the CodeMirror Ruby mode was kindly sponsored
168
+ by <a href="http://ubalo.com/">Ubalo</a>, who hold
169
+ the <a href="LICENSE">license</a>.</p>
170
+
171
+ </body>
172
+ </html>