Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command line: Fixed class regex #1566

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions plugins/command-line/prism-command-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@ if (typeof self === 'undefined' || !self.Prism || !self.document) {
return;
}

var clsReg = /\s*\bcommand-line\b\s*/;
var clsReg = /(?:^|\s)command-line(?:\s|$)/;

Prism.hooks.add('before-highlight', function (env) {
env.vars = env.vars || {};
env.vars['command-line'] = env.vars['command-line'] || {};
var vars = env.vars = env.vars || {};
var commandLine = vars['command-line'] = vars['command-line'] || {};

if (env.vars['command-line'].complete || !env.code) {
env.vars['command-line'].complete = true;
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))) {
env.vars['command-line'].complete = true;
commandLine.complete = true;
return;
}

if (env.element.querySelector('.command-line-prompt')) { // Abort if prompt already exists.
env.vars['command-line'].complete = true;
commandLine.complete = true;
return;
}

var codeLines = env.code.split('\n');
env.vars['command-line'].numberOfLines = codeLines.length;
env.vars['command-line'].outputLines = [];
commandLine.numberOfLines = codeLines.length;
var outputLines = commandLine.outputLines = [];

var outputSections = pre.getAttribute('data-output');
var outputFilter = pre.getAttribute('data-filter-output');
Expand All @@ -53,15 +53,15 @@ Prism.hooks.add('before-highlight', function (env) {
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++) {
env.vars['command-line'].outputLines[j] = codeLines[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
env.vars['command-line'].outputLines[i] = codeLines[i].slice(outputFilter.length);
outputLines[i] = codeLines[i].slice(outputFilter.length);
codeLines[i] = '';
}
}
Expand All @@ -71,26 +71,26 @@ Prism.hooks.add('before-highlight', function (env) {
});

Prism.hooks.add('before-insert', function (env) {
env.vars = env.vars || {};
env.vars['command-line'] = env.vars['command-line'] || {};
if (env.vars['command-line'].complete) {
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 < env.vars['command-line'].outputLines.length; i++) {
if (env.vars['command-line'].outputLines.hasOwnProperty(i)) {
codeLines[i] = env.vars['command-line'].outputLines[i];
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) {
env.vars = env.vars || {};
env.vars['command-line'] = env.vars['command-line'] || {};
if (env.vars['command-line'].complete) {
var vars = env.vars = env.vars || {};
var commandLine = vars['command-line'] = vars['command-line'] || {};
if (commandLine.complete) {
return;
}

Expand All @@ -107,7 +107,7 @@ Prism.hooks.add('complete', function (env) {
};

// Create the "rows" that will become the command-line prompts. -- cwells
var promptLines = new Array(env.vars['command-line'].numberOfLines + 1);
var promptLines = new Array(commandLine.numberOfLines + 1);
var promptText = getAttribute('data-prompt', '');
if (promptText !== '') {
promptLines = promptLines.join('<span data-prompt="' + promptText + '"></span>');
Expand All @@ -123,8 +123,8 @@ Prism.hooks.add('complete', function (env) {
prompt.innerHTML = promptLines;

// Remove the prompt from the output lines. -- cwells
for (var i = 0; i < env.vars['command-line'].outputLines.length; i++) {
if (env.vars['command-line'].outputLines.hasOwnProperty(i)) {
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');
Expand All @@ -133,7 +133,7 @@ Prism.hooks.add('complete', function (env) {
}

env.element.insertBefore(prompt, env.element.firstChild);
env.vars['command-line'].complete = true;
commandLine.complete = true;
});

}());
2 changes: 1 addition & 1 deletion plugins/command-line/prism-command-line.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.