UNPKG

4.74 kBJavaScriptView Raw
1(function() {
2
3if (typeof self === 'undefined' || !self.Prism || !self.document) {
4 return;
5}
6
7var clsReg = /(?:^|\s)command-line(?:\s|$)/;
8
9Prism.hooks.add('before-highlight', function (env) {
10 var vars = env.vars = env.vars || {};
11 var commandLine = vars['command-line'] = vars['command-line'] || {};
12
13 if (commandLine.complete || !env.code) {
14 commandLine.complete = true;
15 return;
16 }
17
18 // Works only for <code> wrapped inside <pre> (not inline).
19 var pre = env.element.parentNode;
20 if (!pre || !/pre/i.test(pre.nodeName) || // Abort only if neither the <pre> nor the <code> have the class
21 (!clsReg.test(pre.className) && !clsReg.test(env.element.className))) {
22 commandLine.complete = true;
23 return;
24 }
25
26 if (env.element.querySelector('.command-line-prompt')) { // Abort if prompt already exists.
27 commandLine.complete = true;
28 return;
29 }
30
31 var codeLines = env.code.split('\n');
32 commandLine.numberOfLines = codeLines.length;
33 var outputLines = commandLine.outputLines = [];
34
35 var outputSections = pre.getAttribute('data-output');
36 var outputFilter = pre.getAttribute('data-filter-output');
37 if (outputSections || outputSections === '') { // The user specified the output lines. -- cwells
38 outputSections = outputSections.split(',');
39 for (var i = 0; i < outputSections.length; i++) { // Parse the output sections into start/end ranges. -- cwells
40 var range = outputSections[i].split('-');
41 var outputStart = parseInt(range[0], 10);
42 var outputEnd = (range.length === 2 ? parseInt(range[1], 10) : outputStart);
43
44 if (!isNaN(outputStart) && !isNaN(outputEnd)) {
45 if (outputStart < 1) {
46 outputStart = 1;
47 }
48 if (outputEnd > codeLines.length) {
49 outputEnd = codeLines.length;
50 }
51 // Convert start and end to 0-based to simplify the arrays. -- cwells
52 outputStart--;
53 outputEnd--;
54 // Save the output line in an array and clear it in the code so it's not highlighted. -- cwells
55 for (var j = outputStart; j <= outputEnd; j++) {
56 outputLines[j] = codeLines[j];
57 codeLines[j] = '';
58 }
59 }
60 }
61 } else if (outputFilter) { // Treat lines beginning with this string as output. -- cwells
62 for (var i = 0; i < codeLines.length; i++) {
63 if (codeLines[i].indexOf(outputFilter) === 0) { // This line is output. -- cwells
64 outputLines[i] = codeLines[i].slice(outputFilter.length);
65 codeLines[i] = '';
66 }
67 }
68 }
69
70 env.code = codeLines.join('\n');
71});
72
73Prism.hooks.add('before-insert', function (env) {
74 var vars = env.vars = env.vars || {};
75 var commandLine = vars['command-line'] = vars['command-line'] || {};
76 if (commandLine.complete) {
77 return;
78 }
79
80 // Reinsert the output lines into the highlighted code. -- cwells
81 var codeLines = env.highlightedCode.split('\n');
82 for (var i = 0, l = (commandLine.outputLines || []).length; i < l; i++) {
83 if (commandLine.outputLines.hasOwnProperty(i)) {
84 codeLines[i] = commandLine.outputLines[i];
85 }
86 }
87 env.highlightedCode = codeLines.join('\n');
88});
89
90Prism.hooks.add('complete', function (env) {
91 var vars = env.vars = env.vars || {};
92 var commandLine = vars['command-line'] = vars['command-line'] || {};
93 if (commandLine.complete) {
94 return;
95 }
96
97 var pre = env.element.parentNode;
98 if (clsReg.test(env.element.className)) { // Remove the class "command-line" from the <code>
99 env.element.className = env.element.className.replace(clsReg, ' ');
100 }
101 if (!clsReg.test(pre.className)) { // Add the class "command-line" to the <pre>
102 pre.className += ' command-line';
103 }
104
105 var getAttribute = function(key, defaultValue) {
106 return (pre.getAttribute(key) || defaultValue).replace(/"/g, '&quot');
107 };
108
109 // Create the "rows" that will become the command-line prompts. -- cwells
110 var promptLines = new Array((commandLine.numberOfLines || 0) + 1);
111 var promptText = getAttribute('data-prompt', '');
112 if (promptText !== '') {
113 promptLines = promptLines.join('<span data-prompt="' + promptText + '"></span>');
114 } else {
115 var user = getAttribute('data-user', 'user');
116 var host = getAttribute('data-host', 'localhost');
117 promptLines = promptLines.join('<span data-user="' + user + '" data-host="' + host + '"></span>');
118 }
119
120 // Create the wrapper element. -- cwells
121 var prompt = document.createElement('span');
122 prompt.className = 'command-line-prompt';
123 prompt.innerHTML = promptLines;
124
125 // Remove the prompt from the output lines. -- cwells
126 for (var i = 0, l = (commandLine.outputLines || []).length; i < l; i++) {
127 if (commandLine.outputLines.hasOwnProperty(i)) {
128 var node = prompt.children[i];
129 node.removeAttribute('data-user');
130 node.removeAttribute('data-host');
131 node.removeAttribute('data-prompt');
132 }
133 }
134
135 env.element.insertBefore(prompt, env.element.firstChild);
136 commandLine.complete = true;
137});
138
139}());