123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- (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);
- };
- }());
|