~didrocks/+junk/face-detection-15.04

« back to all changes in this revision

Viewing changes to facedetection/www/bower_components/prism/plugins/file-highlight/prism-file-highlight.js

  • Committer: Didier Roche
  • Date: 2016-05-10 23:09:11 UTC
  • Revision ID: didier.roche@canonical.com-20160510230911-c7xr490zrj3yrzxd
New version

Show diffs side-by-side

added added

removed removed

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