~didrocks/+junk/face-detection-15.04

« back to all changes in this revision

Viewing changes to facedetection/www/bower_components/prism/plugins/command-line/prism-command-line.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
 
 
3
if (typeof self === 'undefined' || !self.Prism || !self.document) {
 
4
        return;
 
5
}
 
6
 
 
7
Prism.hooks.add('complete', function (env) {
 
8
        if (!env.code) {
 
9
                return;
 
10
        }
 
11
 
 
12
        // Works only for <code> wrapped inside <pre> (not inline).
 
13
        var pre = env.element.parentNode;
 
14
        var clsReg = /\s*\bcommand-line\b\s*/;
 
15
        if (
 
16
                !pre || !/pre/i.test(pre.nodeName) ||
 
17
                        // Abort only if neither the <pre> nor the <code> have the class
 
18
                (!clsReg.test(pre.className) && !clsReg.test(env.element.className))
 
19
        ) {
 
20
                return;
 
21
        }
 
22
 
 
23
        if (env.element.querySelector('.command-line-prompt')) {
 
24
                // Abort if prompt already exists.
 
25
                return;
 
26
        }
 
27
 
 
28
        if (clsReg.test(env.element.className)) {
 
29
                // Remove the class "command-line" from the <code>
 
30
                env.element.className = env.element.className.replace(clsReg, '');
 
31
        }
 
32
        if (!clsReg.test(pre.className)) {
 
33
                // Add the class "command-line" to the <pre>
 
34
                pre.className += ' command-line';
 
35
        }
 
36
 
 
37
        // Create the "rows" that will become the command-line prompts. -- cwells
 
38
        var lines = new Array(1 + env.code.split('\n').length);
 
39
        var promptText = pre.getAttribute('data-prompt') || '';
 
40
        if (promptText !== '') {
 
41
                lines = lines.join('<span data-prompt="' + promptText + '"></span>');
 
42
        } else {
 
43
                var user = pre.getAttribute('data-user') || 'user';
 
44
                var host = pre.getAttribute('data-host') || 'localhost';
 
45
                lines = lines.join('<span data-user="' + user + '" data-host="' + host + '"></span>');
 
46
        }
 
47
 
 
48
        // Create the wrapper element. -- cwells
 
49
        var prompt = document.createElement('span');
 
50
        prompt.className = 'command-line-prompt';
 
51
        prompt.innerHTML = lines;
 
52
 
 
53
        // Mark the output lines so they can be styled differently (no prompt). -- cwells
 
54
        var outputSections = pre.getAttribute('data-output') || '';
 
55
        outputSections = outputSections.split(',');
 
56
        for (var i = 0; i < outputSections.length; i++) {
 
57
                var outputRange = outputSections[i].split('-');
 
58
                var outputStart = parseInt(outputRange[0]);
 
59
                var outputEnd = outputStart; // Default: end at the first line when it's not an actual range. -- cwells
 
60
                if (outputRange.length === 2) {
 
61
                        outputEnd = parseInt(outputRange[1]);
 
62
                }
 
63
 
 
64
                if (!isNaN(outputStart) && !isNaN(outputEnd)) {
 
65
                        for (var j = outputStart; j <= outputEnd && j <= prompt.children.length; j++) {
 
66
                                var node = prompt.children[j - 1];
 
67
                                node.removeAttribute('data-user');
 
68
                                node.removeAttribute('data-host');
 
69
                                node.removeAttribute('data-prompt');
 
70
                        }
 
71
                }
 
72
        }
 
73
 
 
74
        env.element.innerHTML = prompt.outerHTML + env.element.innerHTML;
 
75
});
 
76
 
 
77
}());