-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-prism-command-line.js
180 lines (159 loc) · 6.8 KB
/
custom-prism-command-line.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
(function() {
if (typeof self === 'undefined' || !self.Prism || !self.document) {
return;
}
var clsReg = /(?:^|\s)command-line(?:\s|$)/;
Prism.hooks.add('before-highlight', function(env) {
var vars = env.vars = env.vars || {};
var commandLine = vars['command-line'] = vars['command-line'] || {};
if (commandLine.complete || !env.code) {
commandLine.complete = true;
return;
}
// Works only for <code> wrapped inside <pre> (not inline).
var pre = env.element.parentNode;
if (!pre || !/pre/i.test(pre.nodeName) || // Abort only if neither the <pre> nor the <code> have the class
(!clsReg.test(pre.className) && !clsReg.test(env.element.className))) {
commandLine.complete = true;
return;
}
if (env.element.querySelector('.command-line-prompt')) { // Abort if prompt already exists.
commandLine.complete = true;
return;
}
var codeLines = env.code.split('\n');
commandLine.numberOfLines = codeLines.length;
var outputLines = commandLine.outputLines = [];
var outputSections = pre.getAttribute('data-output');
var outputFilter = pre.getAttribute('data-filter-output');
if (outputSections || outputSections === '') { // The user specified the output lines. -- cwells
outputSections = outputSections.split(',');
for (var i = 0; i < outputSections.length; i++) { // Parse the output sections into start/end ranges. -- cwells
var range = outputSections[i].split('-');
var outputStart = parseInt(range[0], 10);
var outputEnd = (range.length === 2 ? parseInt(range[1], 10) : outputStart);
if (!isNaN(outputStart) && !isNaN(outputEnd)) {
if (outputStart < 1) {
outputStart = 1;
}
if (outputEnd > codeLines.length) {
outputEnd = codeLines.length;
}
// Convert start and end to 0-based to simplify the arrays. -- cwells
outputStart--;
outputEnd--;
// Save the output line in an array and clear it in the code so it's not highlighted. -- cwells
for (var j = outputStart; j <= outputEnd; j++) {
outputLines[j] = codeLines[j];
codeLines[j] = '';
}
}
}
} else if (outputFilter) { // Treat lines beginning with this string as output. -- cwells
for (var i = 0; i < codeLines.length; i++) {
if (codeLines[i].indexOf(outputFilter) === 0) { // This line is output. -- cwells
outputLines[i] = codeLines[i].slice(outputFilter.length);
codeLines[i] = '';
}
}
}
env.code = codeLines.join('\n');
});
Prism.hooks.add('before-insert', function(env) {
var vars = env.vars = env.vars || {};
var commandLine = vars['command-line'] = vars['command-line'] || {};
if (commandLine.complete) {
return;
}
// Reinsert the output lines into the highlighted code. -- cwells
var codeLines = env.highlightedCode.split('\n');
for (var i = 0; i < commandLine.outputLines.length; i++) {
if (commandLine.outputLines.hasOwnProperty(i)) {
codeLines[i] = commandLine.outputLines[i];
}
}
env.highlightedCode = codeLines.join('\n');
});
Prism.hooks.add('complete', function(env) {
var vars = env.vars = env.vars || {};
var commandLine = vars['command-line'] = vars['command-line'] || {};
if (commandLine.complete) {
return;
}
var pre = env.element.parentNode;
if (clsReg.test(env.element.className)) { // Remove the class "command-line" from the <code>
env.element.className = env.element.className.replace(clsReg, ' ');
}
if (!clsReg.test(pre.className)) { // Add the class "command-line" to the <pre>
pre.className += ' command-line';
}
var getAttribute = function(key, defaultValue) {
return (pre.getAttribute(key) || defaultValue).replace(/"/g, '"');
};
// Create the "rows" that will become the command-line prompts. -- cwells
var promptLines = new Array(commandLine.numberOfLines + 1);
var promptText = getAttribute('data-prompt', '');
// if (promptText !== '') {
// promptLines = promptLines.join('<span data-prompt="' + promptText + '"></span>');
// } else {
// var user = getAttribute('data-user', 'user');
// var host = getAttribute('data-host', 'localhost');
// promptLines = promptLines.join('<span data-user="' + user + '" data-host="' + host + '"></span>');
// }
if (promptText !== '') {
if (promptText.includes('|')) {
// promptText format : 1|myPromptText;2-3|myOtherPrompt;4,8|myThirdPrompt
// promptChunks: an array of
var promptChunks = promptText.split(';');
// prompts: an array of line number => prompt text
var prompts = [];
for (var i = 0; i < promptChunks.length; i++) {
// promptSections: 0 - line range; 1 - prompt value
var promptSections = promptChunks[i].split('|');
var promptValue = promptSections[1];
var promptRanges = promptSections[0].split(',');
for (var p = 0; p < promptRanges.length; p++) {
var promptRange = promptRanges[p].split('-');
var promptStart = parseInt(promptRange[0]);
var promptEnd = promptStart;
if (promptRange.length === 2) {
promptEnd = parseInt(promptRange[1]);
}
if (!isNaN(promptStart) && !isNaN(promptEnd)) {
for (var j = promptStart; j <= promptEnd; j++) {
var index = j - 1;
prompts[index] = promptValue;
}
}
}
}
for (var i = 0; i < promptLines.length - 1; i++) {
var promptText = typeof prompts[i] === "undefined" ? ": >" : prompts[i];
promptLines[i] = '<span data-prompt="' + promptText + '"></span>';
}
promptLines = promptLines.join('');
} else {
promptLines = promptLines.join('<span data-prompt="' + promptText + '"></span>');
}
} else {
var user = getAttribute('data-user', 'user');
var host = getAttribute('data-host', 'localhost');
promptLines = promptLines.join('<span data-user="' + user + '" data-host="' + host + '"></span>');
}
// Create the wrapper element. -- cwells
var prompt = document.createElement('span');
prompt.className = 'command-line-prompt';
prompt.innerHTML = promptLines;
// Remove the prompt from the output lines. -- cwells
for (var i = 0; i < commandLine.outputLines.length; i++) {
if (commandLine.outputLines.hasOwnProperty(i)) {
var node = prompt.children[i];
node.removeAttribute('data-user');
node.removeAttribute('data-host');
node.removeAttribute('data-prompt');
}
}
env.element.insertBefore(prompt, env.element.firstChild);
commandLine.complete = true;
});
}());