(opts)
| 66 | * Optional - defaults to the defaultProcessor (above). |
| 67 | */ |
| 68 | var createProcessor = function (opts) { |
| 69 | var url = opts.url, |
| 70 | init = opts.init || passthrough, |
| 71 | handler = opts.handler || defaultProcessor, |
| 72 | processorData = pick(opts, 'id', 'target', 'extensions'); |
| 73 | |
| 74 | opts.extensions = opts.extensions || []; |
| 75 | if (!opts.extensions.length) { |
| 76 | opts.extensions = [opts.id]; |
| 77 | } |
| 78 | |
| 79 | opts.extensions.forEach(function (ext) { |
| 80 | processorBy.extension[ext] = opts.id; |
| 81 | }); |
| 82 | |
| 83 | // This actually loads in the processor – script files & init code |
| 84 | var loadProcessor = function (ready) { |
| 85 | |
| 86 | var failed = false; |
| 87 | |
| 88 | // Overwritten when the script loads |
| 89 | var callback = function () { |
| 90 | if (window.console) { |
| 91 | window.console.warn('Processor is not ready yet - trying again'); |
| 92 | } |
| 93 | failed = true; |
| 94 | return ''; |
| 95 | }; |
| 96 | |
| 97 | // Script has loaded. |
| 98 | // Run any init code, and swap the callback. If we failed, try again. |
| 99 | var scriptCB = function () { |
| 100 | init(function () { |
| 101 | callback = handler; |
| 102 | if (failed) { |
| 103 | renderLivePreview(); |
| 104 | } |
| 105 | ready(); |
| 106 | }); |
| 107 | }; |
| 108 | |
| 109 | if (url) { |
| 110 | // Load the processor's script |
| 111 | getScript(url, scriptCB); |
| 112 | } else { |
| 113 | // No url, go straight on |
| 114 | init(function () { |
| 115 | callback = handler; |
| 116 | ready(); |
| 117 | }); |
| 118 | } |
| 119 | |
| 120 | // Create a proxy function that holds the handler in scope so that, when |
| 121 | // the callbacks are swapped, rendering still works. |
| 122 | var cache = { |
| 123 | source: '', |
| 124 | result: '' |
| 125 | }; |
no test coverage detected