code_sync 0.6.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (213) hide show
  1. data/.rvmrc +1 -0
  2. data/CNAME +1 -0
  3. data/Gemfile +24 -0
  4. data/Gemfile.lock +117 -0
  5. data/LICENSE.md +22 -0
  6. data/ROADMAP.md +20 -0
  7. data/TODO.md +14 -0
  8. data/bin/codesync +11 -0
  9. data/code_sync.gemspec +30 -0
  10. data/config/routes.rb +26 -0
  11. data/lib/assets/javascripts/canvas.coffee +12 -0
  12. data/lib/assets/javascripts/canvas/editors.coffee +48 -0
  13. data/lib/assets/javascripts/canvas/index.coffee +15 -0
  14. data/lib/assets/javascripts/canvas/layer_controller.coffee +47 -0
  15. data/lib/assets/javascripts/code_sync.coffee +3 -0
  16. data/lib/assets/javascripts/code_sync/backends/gist.coffee +78 -0
  17. data/lib/assets/javascripts/code_sync/client/index.coffee +104 -0
  18. data/lib/assets/javascripts/code_sync/client/util.coffee +61 -0
  19. data/lib/assets/javascripts/code_sync/codemirror.coffee +12 -0
  20. data/lib/assets/javascripts/code_sync/config.js.coffee.erb +38 -0
  21. data/lib/assets/javascripts/code_sync/console/index.coffee +54 -0
  22. data/lib/assets/javascripts/code_sync/console/templates/console.jst.skim +10 -0
  23. data/lib/assets/javascripts/code_sync/dependencies.coffee +6 -0
  24. data/lib/assets/javascripts/code_sync/editor/advanced.coffee +0 -0
  25. data/lib/assets/javascripts/code_sync/editor/datasources/document.coffee +212 -0
  26. data/lib/assets/javascripts/code_sync/editor/datasources/gist_loader.coffee +7 -0
  27. data/lib/assets/javascripts/code_sync/editor/datasources/modes.coffee +117 -0
  28. data/lib/assets/javascripts/code_sync/editor/datasources/project_assets.coffee +14 -0
  29. data/lib/assets/javascripts/code_sync/editor/index.coffee +415 -0
  30. data/lib/assets/javascripts/code_sync/editor/plugins/asset_selector.coffee +106 -0
  31. data/lib/assets/javascripts/code_sync/editor/plugins/color_picker.coffee +83 -0
  32. data/lib/assets/javascripts/code_sync/editor/plugins/document_manager.coffee +55 -0
  33. data/lib/assets/javascripts/code_sync/editor/plugins/document_tabs.coffee +178 -0
  34. data/lib/assets/javascripts/code_sync/editor/plugins/element_sync.coffee +111 -0
  35. data/lib/assets/javascripts/code_sync/editor/plugins/keymap_selector.coffee +44 -0
  36. data/lib/assets/javascripts/code_sync/editor/plugins/mode_selector.coffee +53 -0
  37. data/lib/assets/javascripts/code_sync/editor/plugins/name_input.coffee +44 -0
  38. data/lib/assets/javascripts/code_sync/editor/plugins/preferences.coffee +71 -0
  39. data/lib/assets/javascripts/code_sync/editor/templates/asset_editor.jst.skim +2 -0
  40. data/lib/assets/javascripts/code_sync/editor/templates/asset_selector.jst.skim +5 -0
  41. data/lib/assets/javascripts/code_sync/editor/templates/document_manager_tab.jst.skim +15 -0
  42. data/lib/assets/javascripts/code_sync/editor/templates/element_sync.jst.skim +19 -0
  43. data/lib/assets/javascripts/code_sync/editor/templates/preferences_panel.jst.skim +38 -0
  44. data/lib/assets/javascripts/code_sync/editor/views/asset_selector.coffee +106 -0
  45. data/lib/assets/javascripts/code_sync/editor/views/color_picker.coffee +76 -0
  46. data/lib/assets/javascripts/code_sync/editor/views/document_manager.coffee +176 -0
  47. data/lib/assets/javascripts/code_sync/editor/views/keymap_selector.coffee +37 -0
  48. data/lib/assets/javascripts/code_sync/editor/views/mode_selector.coffee +47 -0
  49. data/lib/assets/javascripts/code_sync/editor/views/name_input.coffee +44 -0
  50. data/lib/assets/javascripts/code_sync/editor/views/preferences.coffee +71 -0
  51. data/lib/assets/javascripts/code_sync/index.coffee +4 -0
  52. data/lib/assets/javascripts/code_sync/reloader.coffee +2 -0
  53. data/lib/assets/javascripts/code_sync_basic.coffee +1 -0
  54. data/lib/assets/javascripts/demos.coffee +48 -0
  55. data/lib/assets/javascripts/demos/default-content.coffee +72 -0
  56. data/lib/assets/javascripts/demos/layout_selector.coffee +19 -0
  57. data/lib/assets/javascripts/demos/tour.coffee +70 -0
  58. data/lib/assets/javascripts/demos/tour.jst.skim +29 -0
  59. data/lib/assets/javascripts/marketing.coffee +0 -0
  60. data/lib/assets/javascripts/vendor/.DS_Store +0 -0
  61. data/lib/assets/javascripts/vendor/backbone-events.js +160 -0
  62. data/lib/assets/javascripts/vendor/backbone-min.js +4 -0
  63. data/lib/assets/javascripts/vendor/codemirror-coffeescript.js +346 -0
  64. data/lib/assets/javascripts/vendor/codemirror-css.js +570 -0
  65. data/lib/assets/javascripts/vendor/codemirror-haml.js +153 -0
  66. data/lib/assets/javascripts/vendor/codemirror-htmlmixed.js +104 -0
  67. data/lib/assets/javascripts/vendor/codemirror-javascript.js +468 -0
  68. data/lib/assets/javascripts/vendor/codemirror-markdown.js +526 -0
  69. data/lib/assets/javascripts/vendor/codemirror-ruby.js +194 -0
  70. data/lib/assets/javascripts/vendor/codemirror-sass.js +330 -0
  71. data/lib/assets/javascripts/vendor/codemirror-skim.js +330 -0
  72. data/lib/assets/javascripts/vendor/codemirror-vim.js +3159 -0
  73. data/lib/assets/javascripts/vendor/codemirror-xml.js +328 -0
  74. data/lib/assets/javascripts/vendor/console.js +339 -0
  75. data/lib/assets/javascripts/vendor/gisted.js +27 -0
  76. data/lib/assets/javascripts/vendor/jquery-ui-resize-drag.min.js +6 -0
  77. data/lib/assets/javascripts/vendor/jquery.js +5 -0
  78. data/lib/assets/javascripts/vendor/keylauncher.js +4 -0
  79. data/lib/assets/javascripts/vendor/keymaster.min.js +4 -0
  80. data/lib/assets/javascripts/vendor/spectrum.js +1868 -0
  81. data/lib/assets/javascripts/vendor/underscore-min.js +1 -0
  82. data/lib/assets/javascripts/vendor/underscore.string.min.js +1 -0
  83. data/lib/assets/javascripts/vendor/vendored_codemirror.js +5558 -0
  84. data/lib/assets/javascripts/vendor/zepto.js +2 -0
  85. data/lib/assets/stylesheets/canvas.css.scss +101 -0
  86. data/lib/assets/stylesheets/code_sync.css.scss +4 -0
  87. data/lib/assets/stylesheets/code_sync/codemirror.css +7 -0
  88. data/lib/assets/stylesheets/code_sync/console.css +86 -0
  89. data/lib/assets/stylesheets/code_sync/editor/asset-name-input.css.scss +12 -0
  90. data/lib/assets/stylesheets/code_sync/editor/asset-selector.css.scss +58 -0
  91. data/lib/assets/stylesheets/code_sync/editor/codesync-color-picker.css.sass +5 -0
  92. data/lib/assets/stylesheets/code_sync/editor/document-tabs.css.scss +61 -0
  93. data/lib/assets/stylesheets/code_sync/editor/element-sync.css.scss +72 -0
  94. data/lib/assets/stylesheets/code_sync/editor/mode-selector.css.scss +0 -0
  95. data/lib/assets/stylesheets/code_sync/editor/preferences-panel.css.scss +26 -0
  96. data/lib/assets/stylesheets/code_sync/index.css.scss +141 -0
  97. data/lib/assets/stylesheets/demos.css.scss +96 -0
  98. data/lib/assets/stylesheets/marketing.css.sass +46 -0
  99. data/lib/assets/stylesheets/marketing/syntax.css.scss +1 -0
  100. data/lib/assets/stylesheets/vendor/animate.css +1 -0
  101. data/lib/assets/stylesheets/vendor/codemirror-ambiance.css +75 -0
  102. data/lib/assets/stylesheets/vendor/codemirror-lesserdark.css +44 -0
  103. data/lib/assets/stylesheets/vendor/codemirror-monokai.css +28 -0
  104. data/lib/assets/stylesheets/vendor/codemirror-xq-light.css +43 -0
  105. data/lib/assets/stylesheets/vendor/grid-layout.css +1406 -0
  106. data/lib/assets/stylesheets/vendor/spectrum.css +481 -0
  107. data/lib/assets/stylesheets/vendor/vendored_codemirror.css +246 -0
  108. data/lib/code_sync.rb +41 -0
  109. data/lib/code_sync/cli.rb +73 -0
  110. data/lib/code_sync/manager.rb +238 -0
  111. data/lib/code_sync/processors.rb +18 -0
  112. data/lib/code_sync/processors/basic.rb +9 -0
  113. data/lib/code_sync/processors/jst_processor.rb +17 -0
  114. data/lib/code_sync/pry_console.rb +132 -0
  115. data/lib/code_sync/rails.rb +7 -0
  116. data/lib/code_sync/rails/engine.rb +12 -0
  117. data/lib/code_sync/server.rb +225 -0
  118. data/lib/code_sync/sprockets_adapter.rb +145 -0
  119. data/lib/code_sync/temp_asset.rb +20 -0
  120. data/lib/code_sync/version.rb +3 -0
  121. data/lib/middleman_extension.rb +43 -0
  122. data/readme.md +26 -0
  123. data/site/.gitignore +14 -0
  124. data/site/Gemfile +13 -0
  125. data/site/Gemfile.lock +183 -0
  126. data/site/config.rb +26 -0
  127. data/site/source/canvas.html.slim +21 -0
  128. data/site/source/codepen-style-demo.html.slim +21 -0
  129. data/site/source/demo.html.slim +30 -0
  130. data/site/source/index.html.slim +128 -0
  131. data/site/source/layouts/layout.slim +18 -0
  132. data/site/source/samples/_client.html.md +13 -0
  133. data/site/source/samples/_editor.html.md +19 -0
  134. data/site/source/samples/_hooks.html.md +8 -0
  135. data/site/source/samples/_middleman.html.md +7 -0
  136. data/site/source/samples/_rails.html.md +8 -0
  137. data/site/source/samples/_standalone.html.md +36 -0
  138. data/spec/lib/code_sync/sprockets_adapter_spec.rb +44 -0
  139. data/spec/spec_helper.rb +21 -0
  140. data/spec/support/.DS_Store +0 -0
  141. data/spec/support/dummy_middleman/.gitignore +14 -0
  142. data/spec/support/dummy_middleman/Gemfile +5 -0
  143. data/spec/support/dummy_middleman/Gemfile.lock +100 -0
  144. data/spec/support/dummy_middleman/config.rb +77 -0
  145. data/spec/support/dummy_middleman/source/images/background.png +0 -0
  146. data/spec/support/dummy_middleman/source/images/middleman.png +0 -0
  147. data/spec/support/dummy_middleman/source/index.html.erb +10 -0
  148. data/spec/support/dummy_middleman/source/javascripts/all.js +1 -0
  149. data/spec/support/dummy_middleman/source/layouts/layout.erb +19 -0
  150. data/spec/support/dummy_middleman/source/stylesheets/all.css +55 -0
  151. data/spec/support/dummy_middleman/source/stylesheets/normalize.css +375 -0
  152. data/spec/support/dummy_rails/.gitignore +15 -0
  153. data/spec/support/dummy_rails/Gemfile +38 -0
  154. data/spec/support/dummy_rails/Gemfile.lock +112 -0
  155. data/spec/support/dummy_rails/README.rdoc +261 -0
  156. data/spec/support/dummy_rails/Rakefile +7 -0
  157. data/spec/support/dummy_rails/app/assets/images/rails.png +0 -0
  158. data/spec/support/dummy_rails/app/assets/javascripts/application.js +15 -0
  159. data/spec/support/dummy_rails/app/assets/stylesheets/application.css +13 -0
  160. data/spec/support/dummy_rails/app/controllers/application_controller.rb +3 -0
  161. data/spec/support/dummy_rails/app/helpers/application_helper.rb +2 -0
  162. data/spec/support/dummy_rails/app/mailers/.gitkeep +0 -0
  163. data/spec/support/dummy_rails/app/models/.gitkeep +0 -0
  164. data/spec/support/dummy_rails/app/views/layouts/application.html.erb +14 -0
  165. data/spec/support/dummy_rails/config.ru +4 -0
  166. data/spec/support/dummy_rails/config/application.rb +62 -0
  167. data/spec/support/dummy_rails/config/boot.rb +6 -0
  168. data/spec/support/dummy_rails/config/database.yml +25 -0
  169. data/spec/support/dummy_rails/config/environment.rb +5 -0
  170. data/spec/support/dummy_rails/config/environments/development.rb +37 -0
  171. data/spec/support/dummy_rails/config/environments/production.rb +67 -0
  172. data/spec/support/dummy_rails/config/environments/test.rb +37 -0
  173. data/spec/support/dummy_rails/config/initializers/backtrace_silencers.rb +7 -0
  174. data/spec/support/dummy_rails/config/initializers/inflections.rb +15 -0
  175. data/spec/support/dummy_rails/config/initializers/mime_types.rb +5 -0
  176. data/spec/support/dummy_rails/config/initializers/secret_token.rb +7 -0
  177. data/spec/support/dummy_rails/config/initializers/session_store.rb +8 -0
  178. data/spec/support/dummy_rails/config/initializers/wrap_parameters.rb +14 -0
  179. data/spec/support/dummy_rails/config/locales/en.yml +5 -0
  180. data/spec/support/dummy_rails/config/routes.rb +58 -0
  181. data/spec/support/dummy_rails/db/seeds.rb +7 -0
  182. data/spec/support/dummy_rails/doc/README_FOR_APP +2 -0
  183. data/spec/support/dummy_rails/lib/assets/.gitkeep +0 -0
  184. data/spec/support/dummy_rails/lib/tasks/.gitkeep +0 -0
  185. data/spec/support/dummy_rails/log/.gitkeep +0 -0
  186. data/spec/support/dummy_rails/public/404.html +26 -0
  187. data/spec/support/dummy_rails/public/422.html +26 -0
  188. data/spec/support/dummy_rails/public/500.html +25 -0
  189. data/spec/support/dummy_rails/public/favicon.ico +0 -0
  190. data/spec/support/dummy_rails/public/index.html +241 -0
  191. data/spec/support/dummy_rails/public/robots.txt +5 -0
  192. data/spec/support/dummy_rails/script/rails +6 -0
  193. data/spec/support/dummy_rails/test/fixtures/.gitkeep +0 -0
  194. data/spec/support/dummy_rails/test/functional/.gitkeep +0 -0
  195. data/spec/support/dummy_rails/test/integration/.gitkeep +0 -0
  196. data/spec/support/dummy_rails/test/performance/browsing_test.rb +12 -0
  197. data/spec/support/dummy_rails/test/test_helper.rb +13 -0
  198. data/spec/support/dummy_rails/test/unit/.gitkeep +0 -0
  199. data/spec/support/dummy_rails/vendor/assets/javascripts/.gitkeep +0 -0
  200. data/spec/support/dummy_rails/vendor/assets/stylesheets/.gitkeep +0 -0
  201. data/spec/support/dummy_rails/vendor/plugins/.gitkeep +0 -0
  202. data/spec/support/dummy_static/.DS_Store +0 -0
  203. data/spec/support/dummy_static/app/.DS_Store +0 -0
  204. data/spec/support/dummy_static/app/assets/.DS_Store +0 -0
  205. data/spec/support/dummy_static/app/assets/javascripts/manifest.coffee +4 -0
  206. data/spec/support/dummy_static/app/assets/javascripts/spec_application_javascript.coffee +4 -0
  207. data/spec/support/dummy_static/app/assets/stylesheets/spec_application_stylesheet.css.scss +5 -0
  208. data/spec/support/dummy_static/lib/assets/javascripts/spec_library_javascript.coffee +3 -0
  209. data/spec/support/dummy_static/lib/assets/stylesheets/spec_library_stylesheet.css.scss +5 -0
  210. data/spec/support/dummy_static/vendor/assets/javascripts/spec_vendor_javascript.js +5 -0
  211. data/spec/support/dummy_static/vendor/assets/stylesheets/spec_vendor_stylesheets.css +3 -0
  212. data/vendor/assets/stylesheets/code_sync.css +1 -0
  213. metadata +492 -0
@@ -0,0 +1,153 @@
1
+ (function() {
2
+ "use strict";
3
+
4
+ // full haml mode. This handled embeded ruby and html fragments too
5
+ CodeMirror.defineMode("haml", function(config) {
6
+ var htmlMode = CodeMirror.getMode(config, {name: "htmlmixed"});
7
+ var rubyMode = CodeMirror.getMode(config, "ruby");
8
+
9
+ function rubyInQuote(endQuote) {
10
+ return function(stream, state) {
11
+ var ch = stream.peek();
12
+ if (ch == endQuote && state.rubyState.tokenize.length == 1) {
13
+ // step out of ruby context as it seems to complete processing all the braces
14
+ stream.next();
15
+ state.tokenize = html;
16
+ return "closeAttributeTag";
17
+ } else {
18
+ return ruby(stream, state);
19
+ }
20
+ };
21
+ }
22
+
23
+ function ruby(stream, state) {
24
+ if (stream.match("-#")) {
25
+ stream.skipToEnd();
26
+ return "comment";
27
+ }
28
+ return rubyMode.token(stream, state.rubyState);
29
+ }
30
+
31
+ function html(stream, state) {
32
+ var ch = stream.peek();
33
+
34
+ // handle haml declarations. All declarations that cant be handled here
35
+ // will be passed to html mode
36
+ if (state.previousToken.style == "comment" ) {
37
+ if (state.indented > state.previousToken.indented) {
38
+ stream.skipToEnd();
39
+ return "commentLine";
40
+ }
41
+ }
42
+
43
+ if (state.startOfLine) {
44
+ if (ch == "!" && stream.match("!!")) {
45
+ stream.skipToEnd();
46
+ return "tag";
47
+ } else if (stream.match(/^%[\w:#\.]+=/)) {
48
+ state.tokenize = ruby;
49
+ return "hamlTag";
50
+ } else if (stream.match(/^%[\w:]+/)) {
51
+ return "hamlTag";
52
+ } else if (ch == "/" ) {
53
+ stream.skipToEnd();
54
+ return "comment";
55
+ }
56
+ }
57
+
58
+ if (state.startOfLine || state.previousToken.style == "hamlTag") {
59
+ if ( ch == "#" || ch == ".") {
60
+ stream.match(/[\w-#\.]*/);
61
+ return "hamlAttribute";
62
+ }
63
+ }
64
+
65
+ // donot handle --> as valid ruby, make it HTML close comment instead
66
+ if (state.startOfLine && !stream.match("-->", false) && (ch == "=" || ch == "-" )) {
67
+ state.tokenize = ruby;
68
+ return null;
69
+ }
70
+
71
+ if (state.previousToken.style == "hamlTag" ||
72
+ state.previousToken.style == "closeAttributeTag" ||
73
+ state.previousToken.style == "hamlAttribute") {
74
+ if (ch == "(") {
75
+ state.tokenize = rubyInQuote(")");
76
+ return null;
77
+ } else if (ch == "{") {
78
+ state.tokenize = rubyInQuote("}");
79
+ return null;
80
+ }
81
+ }
82
+
83
+ return htmlMode.token(stream, state.htmlState);
84
+ }
85
+
86
+ return {
87
+ // default to html mode
88
+ startState: function() {
89
+ var htmlState = htmlMode.startState();
90
+ var rubyState = rubyMode.startState();
91
+ return {
92
+ htmlState: htmlState,
93
+ rubyState: rubyState,
94
+ indented: 0,
95
+ previousToken: { style: null, indented: 0},
96
+ tokenize: html
97
+ };
98
+ },
99
+
100
+ copyState: function(state) {
101
+ return {
102
+ htmlState : CodeMirror.copyState(htmlMode, state.htmlState),
103
+ rubyState: CodeMirror.copyState(rubyMode, state.rubyState),
104
+ indented: state.indented,
105
+ previousToken: state.previousToken,
106
+ tokenize: state.tokenize
107
+ };
108
+ },
109
+
110
+ token: function(stream, state) {
111
+ if (stream.sol()) {
112
+ state.indented = stream.indentation();
113
+ state.startOfLine = true;
114
+ }
115
+ if (stream.eatSpace()) return null;
116
+ var style = state.tokenize(stream, state);
117
+ state.startOfLine = false;
118
+ // dont record comment line as we only want to measure comment line with
119
+ // the opening comment block
120
+ if (style && style != "commentLine") {
121
+ state.previousToken = { style: style, indented: state.indented };
122
+ }
123
+ // if current state is ruby and the previous token is not `,` reset the
124
+ // tokenize to html
125
+ if (stream.eol() && state.tokenize == ruby) {
126
+ stream.backUp(1);
127
+ var ch = stream.peek();
128
+ stream.next();
129
+ if (ch && ch != ",") {
130
+ state.tokenize = html;
131
+ }
132
+ }
133
+ // reprocess some of the specific style tag when finish setting previousToken
134
+ if (style == "hamlTag") {
135
+ style = "tag";
136
+ } else if (style == "commentLine") {
137
+ style = "comment";
138
+ } else if (style == "hamlAttribute") {
139
+ style = "attribute";
140
+ } else if (style == "closeAttributeTag") {
141
+ style = null;
142
+ }
143
+ return style;
144
+ },
145
+
146
+ indent: function(state) {
147
+ return state.indented;
148
+ }
149
+ };
150
+ }, "htmlmixed", "ruby");
151
+
152
+ CodeMirror.defineMIME("text/x-haml", "haml");
153
+ })();
@@ -0,0 +1,104 @@
1
+ CodeMirror.defineMode("htmlmixed", function(config, parserConfig) {
2
+ var htmlMode = CodeMirror.getMode(config, {name: "xml", htmlMode: true});
3
+ var cssMode = CodeMirror.getMode(config, "css");
4
+
5
+ var scriptTypes = [], scriptTypesConf = parserConfig && parserConfig.scriptTypes;
6
+ scriptTypes.push({matches: /^(?:text|application)\/(?:x-)?(?:java|ecma)script$|^$/i,
7
+ mode: CodeMirror.getMode(config, "javascript")});
8
+ if (scriptTypesConf) for (var i = 0; i < scriptTypesConf.length; ++i) {
9
+ var conf = scriptTypesConf[i];
10
+ scriptTypes.push({matches: conf.matches, mode: conf.mode && CodeMirror.getMode(config, conf.mode)});
11
+ }
12
+ scriptTypes.push({matches: /./,
13
+ mode: CodeMirror.getMode(config, "text/plain")});
14
+
15
+ function html(stream, state) {
16
+ var tagName = state.htmlState.tagName;
17
+ var style = htmlMode.token(stream, state.htmlState);
18
+ if (tagName == "script" && /\btag\b/.test(style) && stream.current() == ">") {
19
+ // Script block: mode to change to depends on type attribute
20
+ var scriptType = stream.string.slice(Math.max(0, stream.pos - 100), stream.pos).match(/\btype\s*=\s*("[^"]+"|'[^']+'|\S+)[^<]*$/i);
21
+ scriptType = scriptType ? scriptType[1] : "";
22
+ if (scriptType && /[\"\']/.test(scriptType.charAt(0))) scriptType = scriptType.slice(1, scriptType.length - 1);
23
+ for (var i = 0; i < scriptTypes.length; ++i) {
24
+ var tp = scriptTypes[i];
25
+ if (typeof tp.matches == "string" ? scriptType == tp.matches : tp.matches.test(scriptType)) {
26
+ if (tp.mode) {
27
+ state.token = script;
28
+ state.localMode = tp.mode;
29
+ state.localState = tp.mode.startState && tp.mode.startState(htmlMode.indent(state.htmlState, ""));
30
+ }
31
+ break;
32
+ }
33
+ }
34
+ } else if (tagName == "style" && /\btag\b/.test(style) && stream.current() == ">") {
35
+ state.token = css;
36
+ state.localMode = cssMode;
37
+ state.localState = cssMode.startState(htmlMode.indent(state.htmlState, ""));
38
+ }
39
+ return style;
40
+ }
41
+ function maybeBackup(stream, pat, style) {
42
+ var cur = stream.current();
43
+ var close = cur.search(pat), m;
44
+ if (close > -1) stream.backUp(cur.length - close);
45
+ else if (m = cur.match(/<\/?$/)) {
46
+ stream.backUp(cur.length);
47
+ if (!stream.match(pat, false)) stream.match(cur[0]);
48
+ }
49
+ return style;
50
+ }
51
+ function script(stream, state) {
52
+ if (stream.match(/^<\/\s*script\s*>/i, false)) {
53
+ state.token = html;
54
+ state.localState = state.localMode = null;
55
+ return html(stream, state);
56
+ }
57
+ return maybeBackup(stream, /<\/\s*script\s*>/,
58
+ state.localMode.token(stream, state.localState));
59
+ }
60
+ function css(stream, state) {
61
+ if (stream.match(/^<\/\s*style\s*>/i, false)) {
62
+ state.token = html;
63
+ state.localState = state.localMode = null;
64
+ return html(stream, state);
65
+ }
66
+ return maybeBackup(stream, /<\/\s*style\s*>/,
67
+ cssMode.token(stream, state.localState));
68
+ }
69
+
70
+ return {
71
+ startState: function() {
72
+ var state = htmlMode.startState();
73
+ return {token: html, localMode: null, localState: null, htmlState: state};
74
+ },
75
+
76
+ copyState: function(state) {
77
+ if (state.localState)
78
+ var local = CodeMirror.copyState(state.localMode, state.localState);
79
+ return {token: state.token, localMode: state.localMode, localState: local,
80
+ htmlState: CodeMirror.copyState(htmlMode, state.htmlState)};
81
+ },
82
+
83
+ token: function(stream, state) {
84
+ return state.token(stream, state);
85
+ },
86
+
87
+ indent: function(state, textAfter) {
88
+ if (!state.localMode || /^\s*<\//.test(textAfter))
89
+ return htmlMode.indent(state.htmlState, textAfter);
90
+ else if (state.localMode.indent)
91
+ return state.localMode.indent(state.localState, textAfter);
92
+ else
93
+ return CodeMirror.Pass;
94
+ },
95
+
96
+ electricChars: "/{}:",
97
+
98
+ innerMode: function(state) {
99
+ return {state: state.localState || state.htmlState, mode: state.localMode || htmlMode};
100
+ }
101
+ };
102
+ }, "xml", "javascript", "css");
103
+
104
+ CodeMirror.defineMIME("text/html", "htmlmixed");
@@ -0,0 +1,468 @@
1
+ // TODO actually recognize syntax of TypeScript constructs
2
+
3
+ CodeMirror.defineMode("javascript", function(config, parserConfig) {
4
+ var indentUnit = config.indentUnit;
5
+ var jsonMode = parserConfig.json;
6
+ var isTS = parserConfig.typescript;
7
+
8
+ // Tokenizer
9
+
10
+ var keywords = function(){
11
+ function kw(type) {return {type: type, style: "keyword"};}
12
+ var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");
13
+ var operator = kw("operator"), atom = {type: "atom", style: "atom"};
14
+
15
+ var jsKeywords = {
16
+ "if": kw("if"), "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B,
17
+ "return": C, "break": C, "continue": C, "new": C, "delete": C, "throw": C,
18
+ "var": kw("var"), "const": kw("var"), "let": kw("var"),
19
+ "function": kw("function"), "catch": kw("catch"),
20
+ "for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"),
21
+ "in": operator, "typeof": operator, "instanceof": operator,
22
+ "true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom,
23
+ "this": kw("this")
24
+ };
25
+
26
+ // Extend the 'normal' keywords with the TypeScript language extensions
27
+ if (isTS) {
28
+ var type = {type: "variable", style: "variable-3"};
29
+ var tsKeywords = {
30
+ // object-like things
31
+ "interface": kw("interface"),
32
+ "class": kw("class"),
33
+ "extends": kw("extends"),
34
+ "constructor": kw("constructor"),
35
+
36
+ // scope modifiers
37
+ "public": kw("public"),
38
+ "private": kw("private"),
39
+ "protected": kw("protected"),
40
+ "static": kw("static"),
41
+
42
+ "super": kw("super"),
43
+
44
+ // types
45
+ "string": type, "number": type, "bool": type, "any": type
46
+ };
47
+
48
+ for (var attr in tsKeywords) {
49
+ jsKeywords[attr] = tsKeywords[attr];
50
+ }
51
+ }
52
+
53
+ return jsKeywords;
54
+ }();
55
+
56
+ var isOperatorChar = /[+\-*&%=<>!?|~^]/;
57
+
58
+ function chain(stream, state, f) {
59
+ state.tokenize = f;
60
+ return f(stream, state);
61
+ }
62
+
63
+ function nextUntilUnescaped(stream, end) {
64
+ var escaped = false, next;
65
+ while ((next = stream.next()) != null) {
66
+ if (next == end && !escaped)
67
+ return false;
68
+ escaped = !escaped && next == "\\";
69
+ }
70
+ return escaped;
71
+ }
72
+
73
+ // Used as scratch variables to communicate multiple values without
74
+ // consing up tons of objects.
75
+ var type, content;
76
+ function ret(tp, style, cont) {
77
+ type = tp; content = cont;
78
+ return style;
79
+ }
80
+
81
+ function jsTokenBase(stream, state) {
82
+ var ch = stream.next();
83
+ if (ch == '"' || ch == "'")
84
+ return chain(stream, state, jsTokenString(ch));
85
+ else if (/[\[\]{}\(\),;\:\.]/.test(ch))
86
+ return ret(ch);
87
+ else if (ch == "0" && stream.eat(/x/i)) {
88
+ stream.eatWhile(/[\da-f]/i);
89
+ return ret("number", "number");
90
+ }
91
+ else if (/\d/.test(ch) || ch == "-" && stream.eat(/\d/)) {
92
+ stream.match(/^\d*(?:\.\d*)?(?:[eE][+\-]?\d+)?/);
93
+ return ret("number", "number");
94
+ }
95
+ else if (ch == "/") {
96
+ if (stream.eat("*")) {
97
+ return chain(stream, state, jsTokenComment);
98
+ }
99
+ else if (stream.eat("/")) {
100
+ stream.skipToEnd();
101
+ return ret("comment", "comment");
102
+ }
103
+ else if (state.lastType == "operator" || state.lastType == "keyword c" ||
104
+ /^[\[{}\(,;:]$/.test(state.lastType)) {
105
+ nextUntilUnescaped(stream, "/");
106
+ stream.eatWhile(/[gimy]/); // 'y' is "sticky" option in Mozilla
107
+ return ret("regexp", "string-2");
108
+ }
109
+ else {
110
+ stream.eatWhile(isOperatorChar);
111
+ return ret("operator", null, stream.current());
112
+ }
113
+ }
114
+ else if (ch == "#") {
115
+ stream.skipToEnd();
116
+ return ret("error", "error");
117
+ }
118
+ else if (isOperatorChar.test(ch)) {
119
+ stream.eatWhile(isOperatorChar);
120
+ return ret("operator", null, stream.current());
121
+ }
122
+ else {
123
+ stream.eatWhile(/[\w\$_]/);
124
+ var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word];
125
+ return (known && state.lastType != ".") ? ret(known.type, known.style, word) :
126
+ ret("variable", "variable", word);
127
+ }
128
+ }
129
+
130
+ function jsTokenString(quote) {
131
+ return function(stream, state) {
132
+ if (!nextUntilUnescaped(stream, quote))
133
+ state.tokenize = jsTokenBase;
134
+ return ret("string", "string");
135
+ };
136
+ }
137
+
138
+ function jsTokenComment(stream, state) {
139
+ var maybeEnd = false, ch;
140
+ while (ch = stream.next()) {
141
+ if (ch == "/" && maybeEnd) {
142
+ state.tokenize = jsTokenBase;
143
+ break;
144
+ }
145
+ maybeEnd = (ch == "*");
146
+ }
147
+ return ret("comment", "comment");
148
+ }
149
+
150
+ // Parser
151
+
152
+ var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true, "this": true};
153
+
154
+ function JSLexical(indented, column, type, align, prev, info) {
155
+ this.indented = indented;
156
+ this.column = column;
157
+ this.type = type;
158
+ this.prev = prev;
159
+ this.info = info;
160
+ if (align != null) this.align = align;
161
+ }
162
+
163
+ function inScope(state, varname) {
164
+ for (var v = state.localVars; v; v = v.next)
165
+ if (v.name == varname) return true;
166
+ }
167
+
168
+ function parseJS(state, style, type, content, stream) {
169
+ var cc = state.cc;
170
+ // Communicate our context to the combinators.
171
+ // (Less wasteful than consing up a hundred closures on every call.)
172
+ cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc;
173
+
174
+ if (!state.lexical.hasOwnProperty("align"))
175
+ state.lexical.align = true;
176
+
177
+ while(true) {
178
+ var combinator = cc.length ? cc.pop() : jsonMode ? expression : statement;
179
+ if (combinator(type, content)) {
180
+ while(cc.length && cc[cc.length - 1].lex)
181
+ cc.pop()();
182
+ if (cx.marked) return cx.marked;
183
+ if (type == "variable" && inScope(state, content)) return "variable-2";
184
+ return style;
185
+ }
186
+ }
187
+ }
188
+
189
+ // Combinator utils
190
+
191
+ var cx = {state: null, column: null, marked: null, cc: null};
192
+ function pass() {
193
+ for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]);
194
+ }
195
+ function cont() {
196
+ pass.apply(null, arguments);
197
+ return true;
198
+ }
199
+ function register(varname) {
200
+ function inList(list) {
201
+ for (var v = list; v; v = v.next)
202
+ if (v.name == varname) return true;
203
+ return false;
204
+ }
205
+ var state = cx.state;
206
+ if (state.context) {
207
+ cx.marked = "def";
208
+ if (inList(state.localVars)) return;
209
+ state.localVars = {name: varname, next: state.localVars};
210
+ } else {
211
+ if (inList(state.globalVars)) return;
212
+ state.globalVars = {name: varname, next: state.globalVars};
213
+ }
214
+ }
215
+
216
+ // Combinators
217
+
218
+ var defaultVars = {name: "this", next: {name: "arguments"}};
219
+ function pushcontext() {
220
+ cx.state.context = {prev: cx.state.context, vars: cx.state.localVars};
221
+ cx.state.localVars = defaultVars;
222
+ }
223
+ function popcontext() {
224
+ cx.state.localVars = cx.state.context.vars;
225
+ cx.state.context = cx.state.context.prev;
226
+ }
227
+ function pushlex(type, info) {
228
+ var result = function() {
229
+ var state = cx.state;
230
+ state.lexical = new JSLexical(state.indented, cx.stream.column(), type, null, state.lexical, info);
231
+ };
232
+ result.lex = true;
233
+ return result;
234
+ }
235
+ function poplex() {
236
+ var state = cx.state;
237
+ if (state.lexical.prev) {
238
+ if (state.lexical.type == ")")
239
+ state.indented = state.lexical.indented;
240
+ state.lexical = state.lexical.prev;
241
+ }
242
+ }
243
+ poplex.lex = true;
244
+
245
+ function expect(wanted) {
246
+ return function(type) {
247
+ if (type == wanted) return cont();
248
+ else if (wanted == ";") return pass();
249
+ else return cont(arguments.callee);
250
+ };
251
+ }
252
+
253
+ function statement(type) {
254
+ if (type == "var") return cont(pushlex("vardef"), vardef1, expect(";"), poplex);
255
+ if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex);
256
+ if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
257
+ if (type == "{") return cont(pushlex("}"), block, poplex);
258
+ if (type == ";") return cont();
259
+ if (type == "if") return cont(pushlex("form"), expression, statement, poplex, maybeelse(cx.state.indented));
260
+ if (type == "function") return cont(functiondef);
261
+ if (type == "for") return cont(pushlex("form"), expect("("), pushlex(")"), forspec1, expect(")"),
262
+ poplex, statement, poplex);
263
+ if (type == "variable") return cont(pushlex("stat"), maybelabel);
264
+ if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"), expect("{"),
265
+ block, poplex, poplex);
266
+ if (type == "case") return cont(expression, expect(":"));
267
+ if (type == "default") return cont(expect(":"));
268
+ if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),
269
+ statement, poplex, popcontext);
270
+ return pass(pushlex("stat"), expression, expect(";"), poplex);
271
+ }
272
+ function expression(type) {
273
+ return expressionInner(type, maybeoperatorComma);
274
+ }
275
+ function expressionNoComma(type) {
276
+ return expressionInner(type, maybeoperatorNoComma);
277
+ }
278
+ function expressionInner(type, maybeop) {
279
+ if (atomicTypes.hasOwnProperty(type)) return cont(maybeop);
280
+ if (type == "function") return cont(functiondef);
281
+ if (type == "keyword c") return cont(maybeexpression);
282
+ if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeop);
283
+ if (type == "operator") return cont(expression);
284
+ if (type == "[") return cont(pushlex("]"), commasep(expressionNoComma, "]"), poplex, maybeop);
285
+ if (type == "{") return cont(pushlex("}"), commasep(objprop, "}"), poplex, maybeop);
286
+ return cont();
287
+ }
288
+ function maybeexpression(type) {
289
+ if (type.match(/[;\}\)\],]/)) return pass();
290
+ return pass(expression);
291
+ }
292
+
293
+ function maybeoperatorComma(type, value) {
294
+ if (type == ",") return pass();
295
+ return maybeoperatorNoComma(type, value, maybeoperatorComma);
296
+ }
297
+ function maybeoperatorNoComma(type, value, me) {
298
+ if (!me) me = maybeoperatorNoComma;
299
+ if (type == "operator") {
300
+ if (/\+\+|--/.test(value)) return cont(me);
301
+ if (value == "?") return cont(expression, expect(":"), expression);
302
+ return cont(expression);
303
+ }
304
+ if (type == ";") return;
305
+ if (type == "(") return cont(pushlex(")", "call"), commasep(expressionNoComma, ")"), poplex, me);
306
+ if (type == ".") return cont(property, me);
307
+ if (type == "[") return cont(pushlex("]"), expression, expect("]"), poplex, me);
308
+ }
309
+ function maybelabel(type) {
310
+ if (type == ":") return cont(poplex, statement);
311
+ return pass(maybeoperatorComma, expect(";"), poplex);
312
+ }
313
+ function property(type) {
314
+ if (type == "variable") {cx.marked = "property"; return cont();}
315
+ }
316
+ function objprop(type, value) {
317
+ if (type == "variable") {
318
+ cx.marked = "property";
319
+ if (value == "get" || value == "set") return cont(getterSetter);
320
+ } else if (type == "number" || type == "string") {
321
+ cx.marked = type + " property";
322
+ }
323
+ if (atomicTypes.hasOwnProperty(type)) return cont(expect(":"), expressionNoComma);
324
+ }
325
+ function getterSetter(type) {
326
+ if (type == ":") return cont(expression);
327
+ if (type != "variable") return cont(expect(":"), expression);
328
+ cx.marked = "property";
329
+ return cont(functiondef);
330
+ }
331
+ function commasep(what, end) {
332
+ function proceed(type) {
333
+ if (type == ",") {
334
+ var lex = cx.state.lexical;
335
+ if (lex.info == "call") lex.pos = (lex.pos || 0) + 1;
336
+ return cont(what, proceed);
337
+ }
338
+ if (type == end) return cont();
339
+ return cont(expect(end));
340
+ }
341
+ return function(type) {
342
+ if (type == end) return cont();
343
+ else return pass(what, proceed);
344
+ };
345
+ }
346
+ function block(type) {
347
+ if (type == "}") return cont();
348
+ return pass(statement, block);
349
+ }
350
+ function maybetype(type) {
351
+ if (type == ":") return cont(typedef);
352
+ return pass();
353
+ }
354
+ function typedef(type) {
355
+ if (type == "variable"){cx.marked = "variable-3"; return cont();}
356
+ return pass();
357
+ }
358
+ function vardef1(type, value) {
359
+ if (type == "variable") {
360
+ register(value);
361
+ return isTS ? cont(maybetype, vardef2) : cont(vardef2);
362
+ }
363
+ return pass();
364
+ }
365
+ function vardef2(type, value) {
366
+ if (value == "=") return cont(expressionNoComma, vardef2);
367
+ if (type == ",") return cont(vardef1);
368
+ }
369
+ function maybeelse(indent) {
370
+ return function(type, value) {
371
+ if (type == "keyword b" && value == "else") {
372
+ cx.state.lexical = new JSLexical(indent, 0, "form", null, cx.state.lexical);
373
+ return cont(statement, poplex);
374
+ }
375
+ return pass();
376
+ };
377
+ }
378
+ function forspec1(type) {
379
+ if (type == "var") return cont(vardef1, expect(";"), forspec2);
380
+ if (type == ";") return cont(forspec2);
381
+ if (type == "variable") return cont(formaybein);
382
+ return pass(expression, expect(";"), forspec2);
383
+ }
384
+ function formaybein(_type, value) {
385
+ if (value == "in") return cont(expression);
386
+ return cont(maybeoperatorComma, forspec2);
387
+ }
388
+ function forspec2(type, value) {
389
+ if (type == ";") return cont(forspec3);
390
+ if (value == "in") return cont(expression);
391
+ return pass(expression, expect(";"), forspec3);
392
+ }
393
+ function forspec3(type) {
394
+ if (type != ")") cont(expression);
395
+ }
396
+ function functiondef(type, value) {
397
+ if (type == "variable") {register(value); return cont(functiondef);}
398
+ if (type == "(") return cont(pushlex(")"), pushcontext, commasep(funarg, ")"), poplex, statement, popcontext);
399
+ }
400
+ function funarg(type, value) {
401
+ if (type == "variable") {register(value); return isTS ? cont(maybetype) : cont();}
402
+ }
403
+
404
+ // Interface
405
+
406
+ return {
407
+ startState: function(basecolumn) {
408
+ return {
409
+ tokenize: jsTokenBase,
410
+ lastType: null,
411
+ cc: [],
412
+ lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
413
+ localVars: parserConfig.localVars,
414
+ globalVars: parserConfig.globalVars,
415
+ context: parserConfig.localVars && {vars: parserConfig.localVars},
416
+ indented: 0
417
+ };
418
+ },
419
+
420
+ token: function(stream, state) {
421
+ if (stream.sol()) {
422
+ if (!state.lexical.hasOwnProperty("align"))
423
+ state.lexical.align = false;
424
+ state.indented = stream.indentation();
425
+ }
426
+ if (state.tokenize != jsTokenComment && stream.eatSpace()) return null;
427
+ var style = state.tokenize(stream, state);
428
+ if (type == "comment") return style;
429
+ state.lastType = type == "operator" && (content == "++" || content == "--") ? "incdec" : type;
430
+ return parseJS(state, style, type, content, stream);
431
+ },
432
+
433
+ indent: function(state, textAfter) {
434
+ if (state.tokenize == jsTokenComment) return CodeMirror.Pass;
435
+ if (state.tokenize != jsTokenBase) return 0;
436
+ var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical;
437
+ if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev;
438
+ var type = lexical.type, closing = firstChar == type;
439
+ if (parserConfig.statementIndent != null) {
440
+ if (type == ")" && lexical.prev && lexical.prev.type == "stat") lexical = lexical.prev;
441
+ if (lexical.type == "stat") return lexical.indented + parserConfig.statementIndent;
442
+ }
443
+
444
+ if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? 4 : 0);
445
+ else if (type == "form" && firstChar == "{") return lexical.indented;
446
+ else if (type == "form") return lexical.indented + indentUnit;
447
+ else if (type == "stat")
448
+ return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? indentUnit : 0);
449
+ else if (lexical.info == "switch" && !closing)
450
+ return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);
451
+ else if (lexical.align) return lexical.column + (closing ? 0 : 1);
452
+ else return lexical.indented + (closing ? 0 : indentUnit);
453
+ },
454
+
455
+ electricChars: ":{}",
456
+
457
+ jsonMode: jsonMode
458
+ };
459
+ });
460
+
461
+ CodeMirror.defineMIME("text/javascript", "javascript");
462
+ CodeMirror.defineMIME("text/ecmascript", "javascript");
463
+ CodeMirror.defineMIME("application/javascript", "javascript");
464
+ CodeMirror.defineMIME("application/ecmascript", "javascript");
465
+ CodeMirror.defineMIME("application/json", {name: "javascript", json: true});
466
+ CodeMirror.defineMIME("application/x-json", {name: "javascript", json: true});
467
+ CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true });
468
+ CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true });