OLD | NEW |
(Empty) | |
| 1 "use strict"; |
| 2 |
| 3 var fs = require("fs"); |
| 4 var vm = require("vm"); |
| 5 var components = require("./components"); |
| 6 var languagesCatalog = components.languages; |
| 7 |
| 8 |
| 9 module.exports = { |
| 10 |
| 11 /** |
| 12 * Creates a new Prism instance with the given language loaded |
| 13 * |
| 14 * @param {string|string[]} languages |
| 15 * @returns {Prism} |
| 16 */ |
| 17 createInstance: function (languages) { |
| 18 var context = { |
| 19 loadedLanguages: [], |
| 20 Prism: this.createEmptyPrism() |
| 21 }; |
| 22 |
| 23 context = this.loadLanguages(languages, context); |
| 24 |
| 25 return context.Prism; |
| 26 }, |
| 27 |
| 28 /** |
| 29 * Loads the given languages and appends the config to the given Prism o
bject |
| 30 * |
| 31 * @private |
| 32 * @param {string|string[]} languages |
| 33 * @param {{loadedLanguages: string[], Prism: Prism}} context |
| 34 * @returns {{loadedLanguages: string[], Prism: Prism}} |
| 35 */ |
| 36 loadLanguages: function (languages, context) { |
| 37 if (typeof languages === 'string') { |
| 38 languages = [languages]; |
| 39 } |
| 40 |
| 41 var self = this; |
| 42 |
| 43 languages.forEach(function (language) { |
| 44 context = self.loadLanguage(language, context); |
| 45 }); |
| 46 |
| 47 return context; |
| 48 }, |
| 49 |
| 50 /** |
| 51 * Loads the given language (including recursively loading the dependenc
ies) and |
| 52 * appends the config to the given Prism object |
| 53 * |
| 54 * @private |
| 55 * @param {string} language |
| 56 * @param {{loadedLanguages: string[], Prism: Prism}} context |
| 57 * @returns {{loadedLanguages: string[], Prism: Prism}} |
| 58 */ |
| 59 loadLanguage: function (language, context) { |
| 60 if (!languagesCatalog[language]) { |
| 61 throw new Error("Language '" + language + "' not found."
); |
| 62 } |
| 63 |
| 64 // the given language was already loaded |
| 65 if (-1 < context.loadedLanguages.indexOf(language)) { |
| 66 return context; |
| 67 } |
| 68 |
| 69 // if the language has a dependency -> load it first |
| 70 if (languagesCatalog[language].require) { |
| 71 context = this.loadLanguages(languagesCatalog[language].
require, context); |
| 72 } |
| 73 |
| 74 // load the language itself |
| 75 var languageSource = this.loadFileSource(language); |
| 76 context.Prism = this.runFileWithContext(languageSource, {Prism:
context.Prism}).Prism; |
| 77 context.loadedLanguages.push(language); |
| 78 |
| 79 return context; |
| 80 }, |
| 81 |
| 82 |
| 83 /** |
| 84 * Creates a new empty prism instance |
| 85 * |
| 86 * @private |
| 87 * @returns {Prism} |
| 88 */ |
| 89 createEmptyPrism: function () { |
| 90 var coreSource = this.loadFileSource("core"); |
| 91 var context = this.runFileWithContext(coreSource); |
| 92 return context.Prism; |
| 93 }, |
| 94 |
| 95 |
| 96 /** |
| 97 * Cached file sources, to prevent massive HDD work |
| 98 * |
| 99 * @private |
| 100 * @type {Object.<string, string>} |
| 101 */ |
| 102 fileSourceCache: {}, |
| 103 |
| 104 |
| 105 /** |
| 106 * Loads the given file source as string |
| 107 * |
| 108 * @private |
| 109 * @param {string} name |
| 110 * @returns {string} |
| 111 */ |
| 112 loadFileSource: function (name) { |
| 113 return this.fileSourceCache[name] = this.fileSourceCache[name] |
| fs.readFileSync(__dirname + "/../../components/prism-" + name + ".js", "utf8")
; |
| 114 }, |
| 115 |
| 116 |
| 117 /** |
| 118 * Runs a VM for a given file source with the given context |
| 119 * |
| 120 * @private |
| 121 * @param {string} fileSource |
| 122 * @param {*} [context] |
| 123 * |
| 124 * @returns {*} |
| 125 */ |
| 126 runFileWithContext: function (fileSource, context) { |
| 127 context = context || {}; |
| 128 vm.runInNewContext(fileSource, context); |
| 129 return context; |
| 130 } |
| 131 }; |
OLD | NEW |