Permalink
Browse files

Add a way to download all grammars as a Zip directly from the Autoloa…

…der plugin page
  • Loading branch information...
1 parent b4f3423 commit 4a3cdfee0ed84a313bb2cbcfed819a1ac8b42a95 @Golmote Golmote committed Jun 18, 2016
Showing with 104 additions and 1 deletion.
  1. +87 −1 plugins/autoloader/index.html
  2. +2 −0 vendor/FileSaver.min.js
  3. +15 −0 vendor/jszip.min.js
@@ -8,6 +8,20 @@
<base href="../.." />
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="themes/prism.css" data-noprefix />
+<style>
+ .download-grammars {
+ font: inherit;
+ border: 0;
+ padding: 0;
+ margin: 0;
+ background: none;
+ text-decoration: underline;
+ cursor: pointer;
+ }
+ .download-grammars.loading:after {
+ content: ' [... Generating]';
+ }
+</style>
<script src="prefixfree.min.js"></script>
<script>var _gaq = [['_setAccount', 'UA-33746269-1'], ['_trackPageview']];</script>
@@ -27,9 +41,13 @@
<p>
The plugin will automatically handle missing grammars and load them for you.
+ To do this, you need to provide it with a directory of all the grammars you want.
+ </p>
+ <p>
+ You can download all the available grammars by clicking on the following link: <button class="download-grammars" type="button">download all grammars</button>.<br />
+ Alternatively, you can also clone the GitHub repo and take the <code>components</code> folder from there.
</p>
<p>
- The easiest way to download the grammars is to clone the repo and take the <code>components</code> folder from there.
You can then download Prism core and any plugins from the <a href="download.html">Download</a> page, without checking any languages (or just check the languages you want to load by default, e.g. if you're using a language a lot, then you probably want to save the extra HTTP request).
</p>
<p>
@@ -107,6 +125,74 @@
<script src="components.js"></script>
<script src="code.js"></script>
+<script src="components.js"></script>
+<script src="vendor/promise.js"></script>
+<script src="vendor/jszip.min.js"></script>
+<script src="vendor/FileSaver.min.js"></script>
+<script>
+ function getZip(files) {
+ return new Promise(function (resolve, reject) {
+ var zip = new JSZip();
+ var l = files.length;
+ var i = 0;
+ var process = function () {
+ if (i < l) {
+ addFile(zip, files[i][0], files[i][1]).then(function () {
+ i++;
+ process();
+ });
+ } else {
+ resolve(zip);
+ }
+ };
+ process();
+ });
+ }
+ function addFile(zip, filename, filepath) {
+ return getFileContents(filepath).then(function (contents) {
+ zip.file(filename, contents);
+ });
+ }
+ function getFileContents(filepath) {
+ return new Promise(function (resolve, reject) {
+ $u.xhr({
+ url: filepath,
+ callback: function (xhr) {
+ if (xhr.status < 400 && xhr.responseText) {
+ resolve(xhr.responseText);
+ } else {
+ // Never rejected, ignore errors
+ resolve();
+ }
+ }
+ });
+ });
+ }
+
+ $('.download-grammars').addEventListener('click', function () {
+ var btn = this;
+ btn.classList.add('loading');
+
+ var files = [];
+ for (var id in components.languages) {
+ if (id === 'meta') {
+ continue;
+ }
+ var basepath = components.languages.meta.path.replace(/\{id}/g, id);
+ var basename = basepath.substring(basepath.lastIndexOf('/') + 1);
+ files.push([basename + '.js', basepath + '.js']);
+ files.push([basename + '.min.js', basepath + '.min.js']);
+ }
+
+ getZip(files).then(function (zip) {
+ btn.classList.remove('loading');
+ return zip.generateAsync({type: 'blob'});
+ }).then(function (blob) {
+ saveAs(blob, 'prism-components.zip');
+ });
+ });
+
+</script>
</body>
</html>
@@ -0,0 +1,2 @@
+/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
+var saveAs=saveAs||function(e){"use strict";if(typeof e==="undefined"||typeof navigator!=="undefined"&&/MSIE [1-9]\./.test(navigator.userAgent)){return}var t=e.document,n=function(){return e.URL||e.webkitURL||e},r=t.createElementNS("http://www.w3.org/1999/xhtml","a"),o="download"in r,i=function(e){var t=new MouseEvent("click");e.dispatchEvent(t)},a=/constructor/i.test(e.HTMLElement),f=/CriOS\/[\d]+/.test(navigator.userAgent),u=function(t){(e.setImmediate||e.setTimeout)(function(){throw t},0)},d="application/octet-stream",s=1e3*40,c=function(e){var t=function(){if(typeof e==="string"){n().revokeObjectURL(e)}else{e.remove()}};setTimeout(t,s)},l=function(e,t,n){t=[].concat(t);var r=t.length;while(r--){var o=e["on"+t[r]];if(typeof o==="function"){try{o.call(e,n||e)}catch(i){u(i)}}}},p=function(e){if(/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(e.type)){return new Blob([String.fromCharCode(65279),e],{type:e.type})}return e},v=function(t,u,s){if(!s){t=p(t)}var v=this,w=t.type,m=w===d,y,h=function(){l(v,"writestart progress write writeend".split(" "))},S=function(){if((f||m&&a)&&e.FileReader){var r=new FileReader;r.onloadend=function(){var t=f?r.result:r.result.replace(/^data:[^;]*;/,"data:attachment/file;");var n=e.open(t,"_blank");if(!n)e.location.href=t;t=undefined;v.readyState=v.DONE;h()};r.readAsDataURL(t);v.readyState=v.INIT;return}if(!y){y=n().createObjectURL(t)}if(m){e.location.href=y}else{var o=e.open(y,"_blank");if(!o){e.location.href=y}}v.readyState=v.DONE;h();c(y)};v.readyState=v.INIT;if(o){y=n().createObjectURL(t);setTimeout(function(){r.href=y;r.download=u;i(r);h();c(y);v.readyState=v.DONE});return}S()},w=v.prototype,m=function(e,t,n){return new v(e,t||e.name||"download",n)};if(typeof navigator!=="undefined"&&navigator.msSaveOrOpenBlob){return function(e,t,n){t=t||e.name||"download";if(!n){e=p(e)}return navigator.msSaveOrOpenBlob(e,t)}}w.abort=function(){};w.readyState=w.INIT=0;w.WRITING=1;w.DONE=2;w.error=w.onwritestart=w.onprogress=w.onwrite=w.onabort=w.onerror=w.onwriteend=null;return m}(typeof self!=="undefined"&&self||typeof window!=="undefined"&&window||this.content);if(typeof module!=="undefined"&&module.exports){module.exports.saveAs=saveAs}else if(typeof define!=="undefined"&&define!==null&&define.amd!==null){define([],function(){return saveAs})}
Oops, something went wrong.

0 comments on commit 4a3cdfe

Please sign in to comment.