3
if (typeof self === 'undefined' || !self.Prism || !self.document) {
7
Prism.hooks.add('complete', function (env) {
12
// Works only for <code> wrapped inside <pre> (not inline).
13
var pre = env.element.parentNode;
14
var clsReg = /\s*\bcommand-line\b\s*/;
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))
23
if (env.element.querySelector('.command-line-prompt')) {
24
// Abort if prompt already exists.
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, '');
32
if (!clsReg.test(pre.className)) {
33
// Add the class "command-line" to the <pre>
34
pre.className += ' command-line';
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>');
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>');
48
// Create the wrapper element. -- cwells
49
var prompt = document.createElement('span');
50
prompt.className = 'command-line-prompt';
51
prompt.innerHTML = lines;
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]);
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');
74
env.element.innerHTML = prompt.outerHTML + env.element.innerHTML;