use prism-tomorrow.css

This commit is contained in:
CyC2018
2018-12-19 14:09:39 +08:00
parent 0f00bcacaf
commit e9e604e6a7
1747 changed files with 100462 additions and 0 deletions

View File

@ -0,0 +1,131 @@
"use strict";
var fs = require("fs");
var vm = require("vm");
var components = require("../../components");
var languagesCatalog = components.languages;
module.exports = {
/**
* Creates a new Prism instance with the given language loaded
*
* @param {string|string[]} languages
* @returns {Prism}
*/
createInstance: function (languages) {
var context = {
loadedLanguages: [],
Prism: this.createEmptyPrism()
};
context = this.loadLanguages(languages, context);
return context.Prism;
},
/**
* Loads the given languages and appends the config to the given Prism object
*
* @private
* @param {string|string[]} languages
* @param {{loadedLanguages: string[], Prism: Prism}} context
* @returns {{loadedLanguages: string[], Prism: Prism}}
*/
loadLanguages: function (languages, context) {
if (typeof languages === 'string') {
languages = [languages];
}
var self = this;
languages.forEach(function (language) {
context = self.loadLanguage(language, context);
});
return context;
},
/**
* Loads the given language (including recursively loading the dependencies) and
* appends the config to the given Prism object
*
* @private
* @param {string} language
* @param {{loadedLanguages: string[], Prism: Prism}} context
* @returns {{loadedLanguages: string[], Prism: Prism}}
*/
loadLanguage: function (language, context) {
if (!languagesCatalog[language]) {
throw new Error("Language '" + language + "' not found.");
}
// the given language was already loaded
if (-1 < context.loadedLanguages.indexOf(language)) {
return context;
}
// if the language has a dependency -> load it first
if (languagesCatalog[language].require) {
context = this.loadLanguages(languagesCatalog[language].require, context);
}
// load the language itself
var languageSource = this.loadFileSource(language);
context.Prism = this.runFileWithContext(languageSource, {Prism: context.Prism}).Prism;
context.loadedLanguages.push(language);
return context;
},
/**
* Creates a new empty prism instance
*
* @private
* @returns {Prism}
*/
createEmptyPrism: function () {
var coreSource = this.loadFileSource("core");
var context = this.runFileWithContext(coreSource);
return context.Prism;
},
/**
* Cached file sources, to prevent massive HDD work
*
* @private
* @type {Object.<string, string>}
*/
fileSourceCache: {},
/**
* Loads the given file source as string
*
* @private
* @param {string} name
* @returns {string}
*/
loadFileSource: function (name) {
return this.fileSourceCache[name] = this.fileSourceCache[name] || fs.readFileSync(__dirname + "/../../components/prism-" + name + ".js", "utf8");
},
/**
* Runs a VM for a given file source with the given context
*
* @private
* @param {string} fileSource
* @param {*} [context]
*
* @returns {*}
*/
runFileWithContext: function (fileSource, context) {
context = context || {};
vm.runInNewContext(fileSource, context);
return context;
}
};

View File

@ -0,0 +1,196 @@
"use strict";
var fs = require("fs");
var assert = require("chai").assert;
var PrismLoader = require("./prism-loader");
var TokenStreamTransformer = require("./token-stream-transformer");
/**
* Handles parsing of a test case file.
*
*
* A test case file consists of at least two parts, separated by a line of dashes.
* This separation line must start at the beginning of the line and consist of at least three dashes.
*
* The test case file can either consist of two parts:
*
* {source code}
* ----
* {expected token stream}
*
*
* or of three parts:
*
* {source code}
* ----
* {expected token stream}
* ----
* {text comment explaining the test case}
*
* If the file contains more than three parts, the remaining parts are just ignored.
* If the file however does not contain at least two parts (so no expected token stream),
* the test case will later be marked as failed.
*
*
* @type {{runTestCase: Function, transformCompiledTokenStream: Function, parseTestCaseFile: Function}}
*/
module.exports = {
/**
* Runs the given test case file and asserts the result
*
* The passed language identifier can either be a language like "css" or a composed language
* identifier like "css+markup". Composed identifiers can be used for testing language inclusion.
*
* When testing language inclusion, the first given language is the main language which will be passed
* to Prism for highlighting ("css+markup" will result in a call to Prism to highlight with the "css" grammar).
* But it will be ensured, that the additional passed languages will be loaded too.
*
* The languages will be loaded in the order they were provided.
*
* @param {string} languageIdentifier
* @param {string} filePath
*/
runTestCase: function (languageIdentifier, filePath) {
var testCase = this.parseTestCaseFile(filePath);
var usedLanguages = this.parseLanguageNames(languageIdentifier);
if (null === testCase) {
throw new Error("Test case file has invalid format (or the provided token stream is invalid JSON), please read the docs.");
}
var Prism = PrismLoader.createInstance(usedLanguages.languages);
// the first language is the main language to highlight
var mainLanguageGrammar = Prism.languages[usedLanguages.mainLanguage];
var env = {
code: testCase.testSource,
grammar: mainLanguageGrammar,
language: usedLanguages.mainLanguage
};
Prism.hooks.run('before-tokenize', env);
env.tokens = Prism.tokenize(env.code, env.grammar);
Prism.hooks.run('after-tokenize', env);
var compiledTokenStream = env.tokens;
var simplifiedTokenStream = TokenStreamTransformer.simplify(compiledTokenStream);
var tzd = JSON.stringify( simplifiedTokenStream ); var exp = JSON.stringify( testCase.expectedTokenStream );
var i = 0; var j = 0; var diff = "";
while ( j < tzd.length ){ if (exp[i] != tzd[j] || i == exp.length) diff += tzd[j]; else i++; j++; }
// var message = "\nToken Stream: \n" + JSON.stringify( simplifiedTokenStream, null, " " ) +
var message = "\nToken Stream: \n" + tzd +
"\n-----------------------------------------\n" +
"Expected Token Stream: \n" + exp +
"\n-----------------------------------------\n" + diff;
var result = assert.deepEqual(simplifiedTokenStream, testCase.expectedTokenStream, testCase.comment + message);
},
/**
* Parses the language names and finds the main language.
*
* It is either the last language or the language followed by a exclamation mark “!”.
* There should only be one language with an exclamation mark.
*
* @param {string} languageIdentifier
*
* @returns {{languages: string[], mainLanguage: string}}
*/
parseLanguageNames: function (languageIdentifier) {
var languages = languageIdentifier.split("+");
var mainLanguage = null;
languages = languages.map(
function (language) {
var pos = language.indexOf("!");
if (-1 < pos) {
if (mainLanguage) {
throw "There are multiple main languages defined.";
}
mainLanguage = language.replace("!", "");
return mainLanguage;
}
return language;
}
);
if (!mainLanguage) {
mainLanguage = languages[languages.length-1];
}
return {
languages: languages,
mainLanguage: mainLanguage
};
},
/**
* Parses the test case from the given test case file
*
* @private
* @param {string} filePath
* @returns {{testSource: string, expectedTokenStream: Array.<Array.<string>>, comment:string?}|null}
*/
parseTestCaseFile: function (filePath) {
var testCaseSource = fs.readFileSync(filePath, "utf8");
var testCaseParts = testCaseSource.split(/^-{10,}\w*$/m);
try {
var testCase = {
testSource: testCaseParts[0].trim(),
expectedTokenStream: JSON.parse(testCaseParts[1]),
comment: null
};
// if there are three parts, the third one is the comment
// explaining the test case
if (testCaseParts[2]) {
testCase.comment = testCaseParts[2].trim();
}
return testCase;
}
catch (e) {
// the JSON can't be parsed (e.g. it could be empty)
return null;
}
},
/**
* Runs the given pieces of codes and asserts their result.
*
* Code is provided as the key and expected result as the value.
*
* @param {string} languageIdentifier
* @param {object} codes
*/
runTestsWithHooks: function (languageIdentifier, codes) {
var usedLanguages = this.parseLanguageNames(languageIdentifier);
var Prism = PrismLoader.createInstance(usedLanguages.languages);
// the first language is the main language to highlight
for (var code in codes) {
if (codes.hasOwnProperty(code)) {
var env = {
element: {},
language: usedLanguages.mainLanguage,
grammar: Prism.languages[usedLanguages.mainLanguage],
code: code
};
Prism.hooks.run('before-highlight', env);
env.highlightedCode = Prism.highlight(env.code, env.grammar, env.language);
Prism.hooks.run('before-insert', env);
env.element.innerHTML = env.highlightedCode;
Prism.hooks.run('after-highlight', env);
Prism.hooks.run('complete', env);
assert.equal(env.highlightedCode, codes[code]);
}
}
}
};

View File

@ -0,0 +1,115 @@
"use strict";
var fs = require("fs");
var path = require("path");
module.exports = {
/**
* Loads the list of all available tests
*
* @param {string} rootDir
* @returns {Object.<string, string[]>}
*/
loadAllTests: function (rootDir) {
var testSuite = {};
var self = this;
this.getAllDirectories(rootDir).forEach(
function (language) {
testSuite[language] = self.getAllFiles(path.join(rootDir, language));
}
);
return testSuite;
},
/**
* Loads the list of available tests that match the given languages
*
* @param {string} rootDir
* @param {string|string[]} languages
* @returns {Object.<string, string[]>}
*/
loadSomeTests: function (rootDir, languages) {
var testSuite = {};
var self = this;
this.getSomeDirectories(rootDir, languages).forEach(
function (language) {
testSuite[language] = self.getAllFiles(path.join(rootDir, language));
}
);
return testSuite;
},
/**
* Returns a list of all (sub)directories (just the directory names, not full paths)
* in the given src directory
*
* @param {string} src
* @returns {Array.<string>}
*/
getAllDirectories: function (src) {
return fs.readdirSync(src).filter(
function (file) {
return fs.statSync(path.join(src, file)).isDirectory();
}
);
},
/**
* Returns a list of all (sub)directories (just the directory names, not full paths)
* in the given src directory, matching the given languages
*
* @param {string} src
* @param {string|string[]} languages
* @returns {Array.<string>}
*/
getSomeDirectories: function (src, languages) {
var self = this;
return fs.readdirSync(src).filter(
function (file) {
return fs.statSync(path.join(src, file)).isDirectory() && self.directoryMatches(file, languages);
}
);
},
/**
* Returns whether a directory matches one of the given languages.
* @param {string} directory
* @param {string|string[]} languages
*/
directoryMatches: function (directory, languages) {
if (!Array.isArray(languages)) {
languages = [languages];
}
var dirLanguages = directory.split(/!?\+!?/);
return dirLanguages.some(function (lang) {
return languages.indexOf(lang) >= 0;
});
},
/**
* Returns a list of all full file paths to all files in the given src directory
*
* @private
* @param {string} src
* @returns {Array.<string>}
*/
getAllFiles: function (src) {
return fs.readdirSync(src).filter(
function (fileName) {
return fs.statSync(path.join(src, fileName)).isFile();
}
).map(
function (fileName) {
return path.join(src, fileName);
}
);
}
};

View File

@ -0,0 +1,32 @@
"use strict";
module.exports = {
/**
* Simplifies the token stream to ease the matching with the expected token stream.
*
* * Strings are kept as-is
* * In arrays each value is transformed individually
* * Values that are empty (empty arrays or strings only containing whitespace)
*
*
* @param {Array} tokenStream
* @returns {Array.<string[]|Array>}
*/
simplify: function (tokenStream) {
if (Array.isArray(tokenStream)) {
return tokenStream
.map(this.simplify.bind(this))
.filter(function (value) {
return !(Array.isArray(value) && !value.length) && !(typeof value === "string" && !value.trim().length);
}
);
}
else if (typeof tokenStream === "object") {
return [tokenStream.type, this.simplify(tokenStream.content)];
}
else {
return tokenStream;
}
}
};