Blame view

bower_components/prism/plugins/file-highlight/prism-file-highlight.js 1.69 KB
73bcce88   luigser   COMPONENTS
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
  (function () {
  	if (!self.Prism || !self.document || !document.querySelector) {
  		return;
  	}
  
  	self.Prism.fileHighlight = function() {
  
  		var Extensions = {
  			'js': 'javascript',
  			'html': 'markup',
  			'svg': 'markup',
  			'xml': 'markup',
  			'py': 'python',
  			'rb': 'ruby',
  			'ps1': 'powershell',
  			'psm1': 'powershell'
  		};
  
  		if(Array.prototype.forEach) { // Check to prevent error in IE8
  			Array.prototype.slice.call(document.querySelectorAll('pre[data-src]')).forEach(function (pre) {
  				var src = pre.getAttribute('data-src');
  
  				var language, parent = pre;
  				var lang = /\blang(?:uage)?-(?!\*)(\w+)\b/i;
  				while (parent && !lang.test(parent.className)) {
  					parent = parent.parentNode;
  				}
  
  				if (parent) {
  					language = (pre.className.match(lang) || [, ''])[1];
  				}
  
  				if (!language) {
  					var extension = (src.match(/\.(\w+)$/) || [, ''])[1];
  					language = Extensions[extension] || extension;
  				}
  
  				var code = document.createElement('code');
  				code.className = 'language-' + language;
  
  				pre.textContent = '';
  
  				code.textContent = 'Loading…';
  
  				pre.appendChild(code);
  
  				var xhr = new XMLHttpRequest();
  
  				xhr.open('GET', src, true);
  
  				xhr.onreadystatechange = function () {
  					if (xhr.readyState == 4) {
  
  						if (xhr.status < 400 && xhr.responseText) {
  							code.textContent = xhr.responseText;
  
  							Prism.highlightElement(code);
  						}
  						else if (xhr.status >= 400) {
  							code.textContent = '✖ Error ' + xhr.status + ' while fetching file: ' + xhr.statusText;
  						}
  						else {
  							code.textContent = '✖ Error: File does not exist or is empty';
  						}
  					}
  				};
  
  				xhr.send(null);
  			});
  		}
  
  	};
  
  	self.Prism.fileHighlight();
  
  })();