Merge branch 'master' into prism-pascal Conflicts: components.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
diff --git a/components.js b/components.js
index f6ebab3..84c2eba 100644
--- a/components.js
+++ b/components.js
@@ -207,6 +207,19 @@ var components = {
"title": "Handlebars",
"owner": "Golmote"
},
+ "matlab": {
+ "title": "MATLAB",
+ "owner": "Golmote"
+ },
+ "less": {
+ "title": "Less",
+ "require": "css",
+ "owner": "Golmote"
+ },
+ "r": {
+ "title": "R",
+ "owner": "Golmote"
+ },
"pascal": {
"title": "Pascal",
"owner": "Golmote"
diff --git a/components/prism-coffeescript.js b/components/prism-coffeescript.js
index 6f374ed..6cf2c7e 100644
--- a/components/prism-coffeescript.js
+++ b/components/prism-coffeescript.js
@@ -1,18 +1,83 @@
+(function(Prism) {
+
+// Ignore comments starting with { to privilege string interpolation highlighting
+var comment = /#(?!\{).+/g,
+ interpolation = {
+ pattern: /#\{[^}]+\}/g,
+ alias: 'variable'
+ };
+
Prism.languages.coffeescript = Prism.languages.extend('javascript', {
- 'comment': [
- /([#]{3}\s*\r?\n(.*\s*\r*\n*)\s*?\r?\n[#]{3})/g,
- /(\s|^)([#]{1}[^#^\r^\n]{2,}?(\r?\n|$))/g
+ 'comment': comment,
+ 'string': [
+
+ // Strings are multiline
+ /'(?:\\?[\s\S])*?'/g,
+
+ {
+ // Strings are multiline
+ pattern: /"(?:\\?[\s\S])*?"/g,
+ inside: {
+ 'interpolation': interpolation
+ }
+ }
],
- 'keyword': /\b(this|window|delete|class|extends|namespace|extend|ar|let|if|else|while|do|for|each|of|return|in|instanceof|new|with|typeof|try|catch|finally|null|undefined|break|continue)\b/g
+ 'keyword': /\b(and|break|by|catch|class|continue|debugger|delete|do|each|else|extend|extends|false|finally|for|if|in|instanceof|is|isnt|let|loop|namespace|new|no|not|null|of|off|on|or|own|return|super|switch|then|this|throw|true|try|typeof|undefined|unless|until|when|while|window|with|yes|yield)\b/g,
+ 'class-member': {
+ pattern: /@(?!\d)\w+/,
+ alias: 'variable'
+ }
});
-Prism.languages.insertBefore('coffeescript', 'keyword', {
- 'function': {
- pattern: /[a-z|A-z]+\s*[:|=]\s*(\([.|a-z\s|,|:|{|}|\"|\'|=]*\))?\s*->/gi,
+Prism.languages.insertBefore('coffeescript', 'comment', {
+ 'multiline-comment': {
+ pattern: /###[\s\S]+?###/g,
+ alias: 'comment'
+ },
+
+ // Block regexp can contain comments and interpolation
+ 'block-regex': {
+ pattern: /\/{3}[\s\S]*?\/{3}/,
+ alias: 'regex',
inside: {
- 'function-name': /[_?a-z-|A-Z-]+(\s*[:|=])| @[_?$?a-z-|A-Z-]+(\s*)| /g,
- 'operator': /[-+]{1,2}|!|=?<|=?>|={1,2}|(&){1,2}|\|?\||\?|\*|\//g
+ 'comment': comment,
+ 'interpolation': interpolation
+ }
+ }
+});
+
+Prism.languages.insertBefore('coffeescript', 'string', {
+ 'inline-javascript': {
+ pattern: /`(?:\\?[\s\S])*?`/g,
+ inside: {
+ 'delimiter': {
+ pattern: /^`|`$/g,
+ alias: 'punctuation'
+ },
+ rest: Prism.languages.javascript
}
},
- 'attr-name': /[_?a-z-|A-Z-]+(\s*:)| @[_?$?a-z-|A-Z-]+(\s*)| /g
+
+ // Block strings
+ 'multiline-string': [
+ {
+ pattern: /'''[\s\S]*?'''/,
+ alias: 'string'
+ },
+ {
+ pattern: /"""[\s\S]*?"""/,
+ alias: 'string',
+ inside: {
+ interpolation: interpolation
+ }
+ }
+ ]
+
});
+
+Prism.languages.insertBefore('coffeescript', 'keyword', {
+ // Object property
+ 'property': /(?!\d)\w+(?=\s*:(?!:))/g
+});
+
+}(Prism));
\ No newline at end of file
diff --git a/components/prism-coffeescript.min.js b/components/prism-coffeescript.min.js
index 3b7c550..6f871ff 100644
--- a/components/prism-coffeescript.min.js
+++ b/components/prism-coffeescript.min.js
@@ -1 +1 @@
-Prism.languages.coffeescript=Prism.languages.extend("javascript",{comment:[/([#]{3}\s*\r?\n(.*\s*\r*\n*)\s*?\r?\n[#]{3})/g,/(\s|^)([#]{1}[^#^\r^\n]{2,}?(\r?\n|$))/g],keyword:/\b(this|window|delete|class|extends|namespace|extend|ar|let|if|else|while|do|for|each|of|return|in|instanceof|new|with|typeof|try|catch|finally|null|undefined|break|continue)\b/g}),Prism.languages.insertBefore("coffeescript","keyword",{"function":{pattern:/[a-z|A-z]+\s*[:|=]\s*(\([.|a-z\s|,|:|{|}|\"|\'|=]*\))?\s*->/gi,inside:{"function-name":/[_?a-z-|A-Z-]+(\s*[:|=])| @[_?$?a-z-|A-Z-]+(\s*)| /g,operator:/[-+]{1,2}|!|=?<|=?>|={1,2}|(&){1,2}|\|?\||\?|\*|\//g}},"attr-name":/[_?a-z-|A-Z-]+(\s*:)| @[_?$?a-z-|A-Z-]+(\s*)| /g});
\ No newline at end of file
+!function(a){var b=/#(?!\{).+/g,c={pattern:/#\{[^}]+\}/g,alias:"variable"};a.languages.coffeescript=a.languages.extend("javascript",{comment:b,string:[/'(?:\\?[\s\S])*?'/g,{pattern:/"(?:\\?[\s\S])*?"/g,inside:{interpolation:c}}],keyword:/\b(and|break|by|catch|class|continue|debugger|delete|do|each|else|extend|extends|false|finally|for|if|in|instanceof|is|isnt|let|loop|namespace|new|no|not|null|of|off|on|or|own|return|super|switch|then|this|throw|true|try|typeof|undefined|unless|until|when|while|window|with|yes|yield)\b/g,"class-member":{pattern:/@(?!\d)\w+/,alias:"variable"}}),a.languages.insertBefore("coffeescript","comment",{"multiline-comment":{pattern:/###[\s\S]+?###/g,alias:"comment"},"block-regex":{pattern:/\/{3}[\s\S]*?\/{3}/,alias:"regex",inside:{comment:b,interpolation:c}}}),a.languages.insertBefore("coffeescript","string",{"inline-javascript":{pattern:/`(?:\\?[\s\S])*?`/g,inside:{delimiter:{pattern:/^`|`$/g,alias:"punctuation"},rest:a.languages.javascript}},"multiline-string":[{pattern:/'''[\s\S]*?'''/,alias:"string"},{pattern:/"""[\s\S]*?"""/,alias:"string",inside:{interpolation:c}}]}),a.languages.insertBefore("coffeescript","keyword",{property:/(?!\d)\w+(?=\s*:(?!:))/g})}(Prism);
\ No newline at end of file
diff --git a/components/prism-less.js b/components/prism-less.js
new file mode 100644
index 0000000..995f3be
--- /dev/null
+++ b/components/prism-less.js
@@ -0,0 +1,60 @@
+/* FIXME :
+ :extend() is not handled specifically : its highlighting is buggy.
+ Mixin usage must be inside a ruleset to be highlighted.
+ At-rules (e.g. import) containing interpolations are buggy.
+ Detached rulesets are highlighted as at-rules.
+ A comment before a mixin usage prevents the latter to be properly highlighted.
+ */
+
+Prism.languages.less = Prism.languages.extend('css', {
+ 'comment': [
+ /\/\*[\w\W]*?\*\//g,
+ {
+ pattern: /(^|[^\\])\/\/.+/g,
+ lookbehind: true
+ }
+ ],
+ 'atrule': {
+ pattern: /@[\w-]+?(?:\([^{}]+\)|[^(){};])*?(?=\s*\{)/gi,
+ inside: {
+ 'punctuation': /[:()]/g
+ }
+ },
+ // selectors and mixins are considered the same
+ 'selector': {
+ pattern: /(?:@\{[\w-]+\}|[^{};\s@])(?:@\{[\w-]+\}|\([^{}]*\)|[^{};@])*?(?=\s*\{)/g,
+ inside: {
+ // mixin parameters
+ 'variable': /@+[\w-]+/
+ }
+ },
+
+ 'property': /(\b|\B)(?:@\{[\w-]+\}|[\w-])+(?:\+_?)?(?=\s*:)/ig,
+ 'punctuation': /[{}();:,]/g,
+ 'operator': /[+\-*\/]/
+});
+
+// Invert function and punctuation positions
+Prism.languages.insertBefore('less', 'punctuation', {
+ 'function': Prism.languages.less.function
+});
+
+Prism.languages.insertBefore('less', 'property', {
+ 'variable': [
+ // Variable declaration (the colon must be consumed!)
+ {
+ pattern: /@[\w-]+\s*:/,
+ inside: {
+ "punctuation": /:/
+ }
+ },
+
+ // Variable usage
+ /@@?[\w-]+/
+ ],
+ 'mixin-usage': {
+ pattern: /([{;]\s*)[.#](?!\d)[\w-]+.*?(?=[(;])/,
+ lookbehind: true,
+ alias: 'function'
+ }
+});
\ No newline at end of file
diff --git a/components/prism-less.min.js b/components/prism-less.min.js
new file mode 100644
index 0000000..13d53f6
--- /dev/null
+++ b/components/prism-less.min.js
@@ -0,0 +1 @@
+Prism.languages.less=Prism.languages.extend("css",{comment:[/\/\*[\w\W]*?\*\//g,{pattern:/(^|[^\\])\/\/.+/g,lookbehind:!0}],atrule:{pattern:/@[\w-]+?(?:\([^{}]+\)|[^(){};])*?(?=\s*\{)/gi,inside:{punctuation:/[:()]/g}},selector:{pattern:/(?:@\{[\w-]+\}|[^{};\s@])(?:@\{[\w-]+\}|\([^{}]*\)|[^{};@])*?(?=\s*\{)/g,inside:{variable:/@+[\w-]+/}},property:/(\b|\B)(?:@\{[\w-]+\}|[\w-])+(?:\+_?)?(?=\s*:)/gi,punctuation:/[{}();:,]/g,operator:/[+\-*\/]/}),Prism.languages.insertBefore("less","punctuation",{"function":Prism.languages.less.function}),Prism.languages.insertBefore("less","property",{variable:[{pattern:/@[\w-]+\s*:/,inside:{punctuation:/:/}},/@@?[\w-]+/],"mixin-usage":{pattern:/([{;]\s*)[.#](?!\d)[\w-]+.*?(?=[(;])/,lookbehind:!0,alias:"function"}});
\ No newline at end of file
diff --git a/components/prism-matlab.js b/components/prism-matlab.js
new file mode 100644
index 0000000..0adcd72
--- /dev/null
+++ b/components/prism-matlab.js
@@ -0,0 +1,17 @@
+Prism.languages.matlab = {
+ // We put string before comment, because of printf() patterns that contain "%"
+ 'string': {
+ pattern: /(^|\W)'(?:''|[^'\n])*'/g,
+ lookbehind: true
+ },
+ 'comment': [
+ /%\{[\s\S]*?\}%/g,
+ /%.+/g
+ ],
+ // FIXME We could handle imaginary numbers as a whole
+ 'number': /\b-?(?:\d*\.?\d+(?:[eE][+-]?\d+)?(?:[ij])?|[ij])\b/g,
+ 'keyword': /\b(?:break|case|catch|continue|else|elseif|end|for|function|if|inf|NaN|otherwise|parfor|pause|pi|return|switch|try|while)\b/,
+ 'function': /(?!\d)\w+(?=\s*\()/g,
+ 'operator': /\.?[*^\/\\']|[+\-:@]|[<>=~]=?|&&?|\|\|?/g,
+ 'punctuation': /\.{3}|[.,;\[\](){}!]/g
+};
\ No newline at end of file
diff --git a/components/prism-matlab.min.js b/components/prism-matlab.min.js
new file mode 100644
index 0000000..3e4267f
--- /dev/null
+++ b/components/prism-matlab.min.js
@@ -0,0 +1 @@
+Prism.languages.matlab={string:{pattern:/(^|\W)'(?:''|[^'\n])*'/g,lookbehind:!0},comment:[/%\{[\s\S]*?\}%/g,/%.+/g],number:/\b-?(?:\d*\.?\d+(?:[eE][+-]?\d+)?(?:[ij])?|[ij])\b/g,keyword:/\b(?:break|case|catch|continue|else|elseif|end|for|function|if|inf|NaN|otherwise|parfor|pause|pi|return|switch|try|while)\b/,"function":/(?!\d)\w+(?=\s*\()/g,operator:/\.?[*^\/\\']|[+\-:@]|[<>=~]=?|&&?|\|\|?/g,punctuation:/\.{3}|[.,;\[\](){}!]/g};
\ No newline at end of file
diff --git a/components/prism-r.js b/components/prism-r.js
new file mode 100644
index 0000000..f6d9e20
--- /dev/null
+++ b/components/prism-r.js
@@ -0,0 +1,19 @@
+Prism.languages.r = {
+ 'comment': /#.+/,
+ 'string': /(['"])(?:\\?.)*?\1/,
+ 'percent-operator': {
+ // Includes user-defined operators
+ // and %%, %*%, %/%, %in%, %o%, %x%
+ pattern: /%[^%]*?%/,
+ alias: 'operator'
+ },
+ 'boolean': /\b(?:TRUE|FALSE)\b/,
+ 'ellipsis': /\.\.(?:\.|\d+)/,
+ 'number': [
+ /\b(?:NaN|Inf)\b/,
+ /\b(?:0x[\dA-Fa-f]+(?:\.\d*)?|\d*\.?\d+)(?:[EePp][+-]??\d+)?[iL]?\b/
+ ],
+ 'keyword': /\b(?:if|else|repeat|while|function|for|in|next|break|NULL|NA|NA_integer_|NA_real_|NA_complex_|NA_character_)\b/,
+ 'operator': /->>?|<?<-|[<>!=]=?|::?|&&?|\|\|?|[+\-*\/^$@~]/,
+ 'punctuation': /[(){}\[\],;]/
+};
\ No newline at end of file
diff --git a/components/prism-r.min.js b/components/prism-r.min.js
new file mode 100644
index 0000000..6784b40
--- /dev/null
+++ b/components/prism-r.min.js
@@ -0,0 +1 @@
+Prism.languages.r={comment:/#.+/,string:/(['"])(?:\\?.)*?\1/,"percent-operator":{pattern:/%[^%]*?%/,alias:"operator"},"boolean":/\b(?:TRUE|FALSE)\b/,ellipsis:/\.\.(?:\.|\d+)/,number:[/\b(?:NaN|Inf)\b/,/\b(?:0x[\dA-Fa-f]+(?:\.\d*)?|\d*\.?\d+)(?:[EePp][+-]??\d+)?[iL]?\b/],keyword:/\b(?:if|else|repeat|while|function|for|in|next|break|NULL|NA|NA_integer_|NA_real_|NA_complex_|NA_character_)\b/,operator:/->>?|<?<-|[<>!=]=?|::?|&&?|\|\|?|[+\-*\/^$@~]/,punctuation:/[(){}\[\],;]/};
\ No newline at end of file
diff --git a/components/prism-scheme-min.js b/components/prism-scheme-min.js
deleted file mode 100644
index d9ed781..0000000
--- a/components/prism-scheme-min.js
+++ /dev/null
@@ -1 +0,0 @@
-Prism.languages.scheme={"boolean":/#(t|f){1}/,comment:/;.*/,keyword:{pattern:/([(])(define(-syntax|-library|-values)?|(case-)?lambda|let(-values|(rec)?(\*)?)?|else|if|cond|begin|delay|delay-force|parameterize|guard|set!|(quasi-)?quote|syntax-rules)/,lookbehind:true},builtin:{pattern:/([(])(cons|car|cdr|null\?|pair\?|boolean\?|eof-object\?|char\?|procedure\?|number\?|port\?|string\?|vector\?|symbol\?|bytevector\?|list|call-with-current-continuation|call\/cc|append|abs|apply|eval)\b/,lookbehind:true},string:/(["])(?:(?=(\\?))\2.)*?\1|'[^('|\s)]+/,number:/(\s|\))[-+]?[0-9]*\.?[0-9]+((\s*)[-+]{1}(\s*)[0-9]*\.?[0-9]+i)?/,operator:/(\*|\+|\-|\%|\/|<=|=>|>=|<|=|>)/,"function":{pattern:/([(])[^(\s|\))]*\s/,lookbehind:true},punctuation:/[()]/}
diff --git a/components/prism-scheme.min.js b/components/prism-scheme.min.js
new file mode 100644
index 0000000..d9ed781
--- /dev/null
+++ b/components/prism-scheme.min.js
@@ -0,0 +1 @@
+Prism.languages.scheme={"boolean":/#(t|f){1}/,comment:/;.*/,keyword:{pattern:/([(])(define(-syntax|-library|-values)?|(case-)?lambda|let(-values|(rec)?(\*)?)?|else|if|cond|begin|delay|delay-force|parameterize|guard|set!|(quasi-)?quote|syntax-rules)/,lookbehind:true},builtin:{pattern:/([(])(cons|car|cdr|null\?|pair\?|boolean\?|eof-object\?|char\?|procedure\?|number\?|port\?|string\?|vector\?|symbol\?|bytevector\?|list|call-with-current-continuation|call\/cc|append|abs|apply|eval)\b/,lookbehind:true},string:/(["])(?:(?=(\\?))\2.)*?\1|'[^('|\s)]+/,number:/(\s|\))[-+]?[0-9]*\.?[0-9]+((\s*)[-+]{1}(\s*)[0-9]*\.?[0-9]+i)?/,operator:/(\*|\+|\-|\%|\/|<=|=>|>=|<|=|>)/,"function":{pattern:/([(])[^(\s|\))]*\s/,lookbehind:true},punctuation:/[()]/}
diff --git a/download.html b/download.html
index 8b366a8..29ea909 100644
--- a/download.html
+++ b/download.html
@@ -154,6 +154,7 @@ section.download {
<script src="utopia.js"></script>
<script src="components.js"></script>
<script src="code.js"></script>
+<script src="vendor/promise.js"></script>
<script src="download.js"></script>
</body>
diff --git a/download.js b/download.js
index d6f106d..44a7299 100644
--- a/download.js
+++ b/download.js
@@ -10,12 +10,24 @@ var minified = true;
var dependencies = {};
+var treeURL = 'https://api.github.com/repos/LeaVerou/prism/git/trees/gh-pages?recursive=1';
+var treePromise = new Promise(function(resolve) {
+ $u.xhr({
+ url: treeURL,
+ callback: function(xhr) {
+ if (xhr.status < 400) {
+ resolve(JSON.parse(xhr.responseText).tree);
+ }
+ }
+ });
+});
+
var qstr = window.location.search.match(/(?:languages|plugins)=[-+\w]+|themes=[-\w]+/g);
if (qstr) {
qstr.forEach(function(str) {
var kv = str.split('=', 2),
- category = kv[0],
- ids = kv[1].split('+');
+ category = kv[0],
+ ids = kv[1].split('+');
if (category !== 'meta' && category !== 'core' && components[category]) {
for (var id in components[category]) {
if (components[category][id].option) {
@@ -174,10 +186,20 @@ form.elements.compression[0].onclick =
form.elements.compression[1].onclick = function() {
minified = !!+this.value;
- fetchFiles();
+ getFilesSizes();
+}
+
+function getFileSize(filepath) {
+ return treePromise.then(function(tree) {
+ for(var i=0, l=tree.length; i<l; i++) {
+ if(tree[i].path === filepath) {
+ return tree[i].size;
+ }
+ }
+ });
}
-function fetchFiles() {
+function getFilesSizes() {
for (var category in components) {
var all = components[category];
@@ -192,26 +214,18 @@ function fetchFiles() {
files.forEach(function (filepath) {
var file = cache[filepath] = cache[filepath] || {};
- if (!file.contents) {
-
- (function(category, id, file, filepath, distro){
-
- $u.xhr({
- url: filepath,
- callback: function(xhr) {
- if (xhr.status < 400) {
-
- file.contents = xhr.responseText;
-
- file.size = +xhr.getResponseHeader('Content-Length') || file.contents.length;
-
- distro.size += file.size;
-
- update(category, id);
- }
+ if(!file.size) {
+
+ (function(category, id) {
+ getFileSize(filepath).then(function(size) {
+ if(size) {
+ file.size = size;
+ distro.size += file.size;
+
+ update(category, id);
}
});
- })(category, id, file, filepath, distro);
+ }(category, id));
}
else {
update(category, id);
@@ -221,7 +235,22 @@ function fetchFiles() {
}
}
-fetchFiles();
+getFilesSizes();
+
+function getFileContents(filepath) {
+ return new Promise(function(resolve, reject) {
+ $u.xhr({
+ url: filepath,
+ callback: function(xhr) {
+ if (xhr.status < 400) {
+ resolve(xhr.responseText);
+ } else {
+ reject();
+ }
+ }
+ });
+ });
+}
function prettySize(size) {
return Math.round(100 * size / 1024)/100 + 'KB';
@@ -242,10 +271,17 @@ function update(updatedCategory, updatedId){
distro.paths.forEach(function(path) {
if (cache[path]) {
+ var file = cache[path];
+
var type = path.match(/\.(\w+)$/)[1],
- size = cache[path].size || 0;
+ size = file.size || 0;
if (info.enabled) {
+
+ if (!file.contentsPromise) {
+ file.contentsPromise = getFileContents(path);
+ }
+
total[type] += size;
}
@@ -291,8 +327,9 @@ function delayedGenerateCode(){
}
timerId = setTimeout(generateCode, 500);
}
+
function generateCode(){
- var code = {js: '', css: ''};
+ var promises = [];
var redownload = {};
for (var category in components) {
@@ -313,27 +350,59 @@ function generateCode(){
if (cache[path]) {
var type = path.match(/\.(\w+)$/)[1];
- code[type] += cache[path].contents + (type === 'js'? ';' : '') + '\n';
+ promises.push({
+ contentsPromise: cache[path].contentsPromise,
+ path: path,
+ type: type
+ });
}
});
}
}
}
+
+ buildCode(promises).then(function(code) {
- var redownloadUrl = window.location.href.split("?")[0] + "?";
- for (var category in redownload) {
- redownloadUrl += category + "=" + redownload[category].join('+') + "&";
- }
- redownloadUrl = "/* " + redownloadUrl.replace(/&$/,"") + " */";
+ var redownloadUrl = window.location.href.split("?")[0] + "?";
+ for (var category in redownload) {
+ redownloadUrl += category + "=" + redownload[category].join('+') + "&";
+ }
+ redownloadUrl = "/* " + redownloadUrl.replace(/&$/,"") + " */";
- for (var type in code) {
- var codeElement = $('#download-' + type + ' code');
-
- codeElement.textContent = redownloadUrl + "\n" + code[type];
- Prism.highlightElement(codeElement, true);
-
- $('#download-' + type + ' .download-button').href = 'data:application/octet-stream;charset=utf-8,' + encodeURIComponent(redownloadUrl + "\n" + code[type]);
- }
+ for (var type in code) {
+ var codeElement = $('#download-' + type + ' code');
+
+ codeElement.textContent = redownloadUrl + "\n" + code[type];
+ Prism.highlightElement(codeElement, true);
+
+ $('#download-' + type + ' .download-button').href = 'data:application/octet-stream;charset=utf-8,' + encodeURIComponent(redownloadUrl + "\n" + code[type]);
+ }
+ });
+}
+
+function buildCode(promises) {
+ var i = 0,
+ l = promises.length;
+ var code = {js: '', css: ''};
+ var f = function(resolve) {
+ if(i < l) {
+ var p = promises[i];
+ p.contentsPromise.then(function(contents) {
+ code[p.type] += contents + (p.type === 'js'? ';' : '') + '\n';
+ i++;
+ f(resolve);
+ });
+ p.contentsPromise['catch'](function() {
+ code[p.type] += '/* Error downloading file '+p.path+' */' + '\n';
+ i++;
+ f(resolve);
+ });
+ } else {
+ resolve(code);
+ }
+ };
+
+ return new Promise(f);
}
})();
\ No newline at end of file