Branch
Hash :
b631949a
Author :
Date :
2021-11-03T12:19:36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
(function () {
if (typeof Prism === 'undefined' || typeof document === 'undefined') {
return;
}
// https://developer.mozilla.org/en-US/docs/Web/API/Element/matches#Polyfill
if (!Element.prototype.matches) {
Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
}
var LOADING_MESSAGE = 'Loading…';
var FAILURE_MESSAGE = function (status, message) {
return '✖ Error ' + status + ' while fetching file: ' + message;
};
var FAILURE_EMPTY_MESSAGE = '✖ Error: File does not exist or is empty';
var EXTENSIONS = {
'js': 'javascript',
'py': 'python',
'rb': 'ruby',
'ps1': 'powershell',
'psm1': 'powershell',
'sh': 'bash',
'bat': 'batch',
'h': 'c',
'tex': 'latex'
};
var STATUS_ATTR = 'data-src-status';
var STATUS_LOADING = 'loading';
var STATUS_LOADED = 'loaded';
var STATUS_FAILED = 'failed';
var SELECTOR = 'pre[data-src]:not([' + STATUS_ATTR + '="' + STATUS_LOADED + '"])'
+ ':not([' + STATUS_ATTR + '="' + STATUS_LOADING + '"])';
/**
* Loads the given file.
*
* @param {string} src The URL or path of the source file to load.
* @param {(result: string) => void} success
* @param {(reason: string) => void} error
*/
function loadFile(src, success, error) {
var xhr = new XMLHttpRequest();
xhr.open('GET', src, true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status < 400 && xhr.responseText) {
success(xhr.responseText);
} else {
if (xhr.status >= 400) {
error(FAILURE_MESSAGE(xhr.status, xhr.statusText));
} else {
error(FAILURE_EMPTY_MESSAGE);
}
}
}
};
xhr.send(null);
}
/**
* Parses the given range.
*
* This returns a range with inclusive ends.
*
* @param {string | null | undefined} range
* @returns {[number, number | undefined] | undefined}
*/
function parseRange(range) {
var m = /^\s*(\d+)\s*(?:(,)\s*(?:(\d+)\s*)?)?$/.exec(range || '');
if (m) {
var start = Number(m[1]);
var comma = m[2];
var end = m[3];
if (!comma) {
return [start, start];
}
if (!end) {
return [start, undefined];
}
return [start, Number(end)];
}
return undefined;
}
Prism.hooks.add('before-highlightall', function (env) {
env.selector += ', ' + SELECTOR;
});
Prism.hooks.add('before-sanity-check', function (env) {
var pre = /** @type {HTMLPreElement} */ (env.element);
if (pre.matches(SELECTOR)) {
env.code = ''; // fast-path the whole thing and go to complete
pre.setAttribute(STATUS_ATTR, STATUS_LOADING); // mark as loading
// add code element with loading message
var code = pre.appendChild(document.createElement('CODE'));
code.textContent = LOADING_MESSAGE;
var src = pre.getAttribute('data-src');
var language = env.language;
if (language === 'none') {
// the language might be 'none' because there is no language set;
// in this case, we want to use the extension as the language
var extension = (/\.(\w+)$/.exec(src) || [, 'none'])[1];
language = EXTENSIONS[extension] || extension;
}
// set language classes
Prism.util.setLanguage(code, language);
Prism.util.setLanguage(pre, language);
// preload the language
var autoloader = Prism.plugins.autoloader;
if (autoloader) {
autoloader.loadLanguages(language);
}
// load file
loadFile(
src,
function (text) {
// mark as loaded
pre.setAttribute(STATUS_ATTR, STATUS_LOADED);
// handle data-range
var range = parseRange(pre.getAttribute('data-range'));
if (range) {
var lines = text.split(/\r\n?|\n/g);
// the range is one-based and inclusive on both ends
var start = range[0];
var end = range[1] == null ? lines.length : range[1];
if (start < 0) { start += lines.length; }
start = Math.max(0, Math.min(start - 1, lines.length));
if (end < 0) { end += lines.length; }
end = Math.max(0, Math.min(end, lines.length));
text = lines.slice(start, end).join('\n');
// add data-start for line numbers
if (!pre.hasAttribute('data-start')) {
pre.setAttribute('data-start', String(start + 1));
}
}
// highlight code
code.textContent = text;
Prism.highlightElement(code);
},
function (error) {
// mark as failed
pre.setAttribute(STATUS_ATTR, STATUS_FAILED);
code.textContent = error;
}
);
}
});
Prism.plugins.fileHighlight = {
/**
* Executes the File Highlight plugin for all matching `pre` elements under the given container.
*
* Note: Elements which are already loaded or currently loading will not be touched by this method.
*
* @param {ParentNode} [container=document]
*/
highlight: function highlight(container) {
var elements = (container || document).querySelectorAll(SELECTOR);
for (var i = 0, element; (element = elements[i++]);) {
Prism.highlightElement(element);
}
}
};
var logged = false;
/** @deprecated Use `Prism.plugins.fileHighlight.highlight` instead. */
Prism.fileHighlight = function () {
if (!logged) {
console.warn('Prism.fileHighlight is deprecated. Use `Prism.plugins.fileHighlight.highlight` instead.');
logged = true;
}
Prism.plugins.fileHighlight.highlight.apply(this, arguments);
};
}());