[d24f17c] | 1 | (function () {
|
---|
| 2 |
|
---|
| 3 | if (typeof Prism === 'undefined') {
|
---|
| 4 | return;
|
---|
| 5 | }
|
---|
| 6 |
|
---|
| 7 |
|
---|
| 8 | var LANGUAGE_REGEX = /^diff-([\w-]+)/i;
|
---|
| 9 | var HTML_TAG = /<\/?(?!\d)[^\s>\/=$<%]+(?:\s(?:\s*[^\s>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?\s*\/?>/g;
|
---|
| 10 | //this will match a line plus the line break while ignoring the line breaks HTML tags may contain.
|
---|
| 11 | var HTML_LINE = RegExp(/(?:__|[^\r\n<])*(?:\r\n?|\n|(?:__|[^\r\n<])(?![^\r\n]))/.source.replace(/__/g, function () { return HTML_TAG.source; }), 'gi');
|
---|
| 12 |
|
---|
| 13 | var warningLogged = false;
|
---|
| 14 |
|
---|
| 15 | Prism.hooks.add('before-sanity-check', function (env) {
|
---|
| 16 | var lang = env.language;
|
---|
| 17 | if (LANGUAGE_REGEX.test(lang) && !env.grammar) {
|
---|
| 18 | env.grammar = Prism.languages[lang] = Prism.languages.diff;
|
---|
| 19 | }
|
---|
| 20 | });
|
---|
| 21 | Prism.hooks.add('before-tokenize', function (env) {
|
---|
| 22 | if (!warningLogged && !Prism.languages.diff && !Prism.plugins.autoloader) {
|
---|
| 23 | warningLogged = true;
|
---|
| 24 | console.warn("Prism's Diff Highlight plugin requires the Diff language definition (prism-diff.js)." +
|
---|
| 25 | "Make sure the language definition is loaded or use Prism's Autoloader plugin.");
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | var lang = env.language;
|
---|
| 29 | if (LANGUAGE_REGEX.test(lang) && !Prism.languages[lang]) {
|
---|
| 30 | Prism.languages[lang] = Prism.languages.diff;
|
---|
| 31 | }
|
---|
| 32 | });
|
---|
| 33 |
|
---|
| 34 | Prism.hooks.add('wrap', function (env) {
|
---|
| 35 | var diffLanguage; var diffGrammar;
|
---|
| 36 |
|
---|
| 37 | if (env.language !== 'diff') {
|
---|
| 38 | var langMatch = LANGUAGE_REGEX.exec(env.language);
|
---|
| 39 | if (!langMatch) {
|
---|
| 40 | return; // not a language specific diff
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | diffLanguage = langMatch[1];
|
---|
| 44 | diffGrammar = Prism.languages[diffLanguage];
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | var PREFIXES = Prism.languages.diff && Prism.languages.diff.PREFIXES;
|
---|
| 48 |
|
---|
| 49 | // one of the diff tokens without any nested tokens
|
---|
| 50 | if (PREFIXES && env.type in PREFIXES) {
|
---|
| 51 | /** @type {string} */
|
---|
| 52 | var content = env.content.replace(HTML_TAG, ''); // remove all HTML tags
|
---|
| 53 |
|
---|
| 54 | /** @type {string} */
|
---|
| 55 | var decoded = content.replace(/</g, '<').replace(/&/g, '&');
|
---|
| 56 |
|
---|
| 57 | // remove any one-character prefix
|
---|
| 58 | var code = decoded.replace(/(^|[\r\n])./g, '$1');
|
---|
| 59 |
|
---|
| 60 | // highlight, if possible
|
---|
| 61 | var highlighted;
|
---|
| 62 | if (diffGrammar) {
|
---|
| 63 | highlighted = Prism.highlight(code, diffGrammar, diffLanguage);
|
---|
| 64 | } else {
|
---|
| 65 | highlighted = Prism.util.encode(code);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | // get the HTML source of the prefix token
|
---|
| 69 | var prefixToken = new Prism.Token('prefix', PREFIXES[env.type], [/\w+/.exec(env.type)[0]]);
|
---|
| 70 | var prefix = Prism.Token.stringify(prefixToken, env.language);
|
---|
| 71 |
|
---|
| 72 | // add prefix
|
---|
| 73 | var lines = []; var m;
|
---|
| 74 | HTML_LINE.lastIndex = 0;
|
---|
| 75 | while ((m = HTML_LINE.exec(highlighted))) {
|
---|
| 76 | lines.push(prefix + m[0]);
|
---|
| 77 | }
|
---|
| 78 | if (/(?:^|[\r\n]).$/.test(decoded)) {
|
---|
| 79 | // because both "+a\n+" and "+a\n" will map to "a\n" after the line prefixes are removed
|
---|
| 80 | lines.push(prefix);
|
---|
| 81 | }
|
---|
| 82 | env.content = lines.join('');
|
---|
| 83 |
|
---|
| 84 | if (diffGrammar) {
|
---|
| 85 | env.classes.push('language-' + diffLanguage);
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | });
|
---|
| 89 |
|
---|
| 90 | }());
|
---|