1 | (function () {
|
2 | if (typeof self === 'undefined' || !self.Prism || !self.document || !document.querySelector) {
|
3 | return;
|
4 | }
|
5 |
|
6 | |
7 |
|
8 |
|
9 | self.Prism.fileHighlight = function(container) {
|
10 | container = container || document;
|
11 |
|
12 | var Extensions = {
|
13 | 'js': 'javascript',
|
14 | 'py': 'python',
|
15 | 'rb': 'ruby',
|
16 | 'ps1': 'powershell',
|
17 | 'psm1': 'powershell',
|
18 | 'sh': 'bash',
|
19 | 'bat': 'batch',
|
20 | 'h': 'c',
|
21 | 'tex': 'latex'
|
22 | };
|
23 |
|
24 | Array.prototype.slice.call(container.querySelectorAll('pre[data-src]')).forEach(function (pre) {
|
25 |
|
26 | if (pre.hasAttribute('data-src-loaded')) {
|
27 | return;
|
28 | }
|
29 |
|
30 |
|
31 | var src = pre.getAttribute('data-src');
|
32 |
|
33 | var language, parent = pre;
|
34 | var lang = /\blang(?:uage)?-([\w-]+)\b/i;
|
35 | while (parent && !lang.test(parent.className)) {
|
36 | parent = parent.parentNode;
|
37 | }
|
38 |
|
39 | if (parent) {
|
40 | language = (pre.className.match(lang) || [, ''])[1];
|
41 | }
|
42 |
|
43 | if (!language) {
|
44 | var extension = (src.match(/\.(\w+)$/) || [, ''])[1];
|
45 | language = Extensions[extension] || extension;
|
46 | }
|
47 |
|
48 | var code = document.createElement('code');
|
49 | code.className = 'language-' + language;
|
50 |
|
51 | pre.textContent = '';
|
52 |
|
53 | code.textContent = 'Loading…';
|
54 |
|
55 | pre.appendChild(code);
|
56 |
|
57 | var xhr = new XMLHttpRequest();
|
58 |
|
59 | xhr.open('GET', src, true);
|
60 |
|
61 | xhr.onreadystatechange = function () {
|
62 | if (xhr.readyState == 4) {
|
63 |
|
64 | if (xhr.status < 400 && xhr.responseText) {
|
65 | code.textContent = xhr.responseText;
|
66 |
|
67 | Prism.highlightElement(code);
|
68 |
|
69 | pre.setAttribute('data-src-loaded', '');
|
70 | }
|
71 | else if (xhr.status >= 400) {
|
72 | code.textContent = '✖ Error ' + xhr.status + ' while fetching file: ' + xhr.statusText;
|
73 | }
|
74 | else {
|
75 | code.textContent = '✖ Error: File does not exist or is empty';
|
76 | }
|
77 | }
|
78 | };
|
79 |
|
80 | xhr.send(null);
|
81 | });
|
82 | };
|
83 |
|
84 | document.addEventListener('DOMContentLoaded', function () {
|
85 |
|
86 | self.Prism.fileHighlight();
|
87 | });
|
88 |
|
89 | })();
|