-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Skip highlighting for output lines. #856
Conversation
f86253d
to
b64a904
Compare
Thanks! Wouldn't it be more usable if there was a character in the beginning of such lines to indicate them instead of having to figure out line numbers for |
Do you mean some sort of non-code text at the beginning of every line of output? My instinct says that's at least as cumbersome as identifying the output lines in an attribute, but then I haven't tried it. :) There are 2 other concerns that come to mind:
I may have misunderstood what you were suggesting, though. Please correct me if I'm misinterpreting it. |
} | ||
} | ||
}); | ||
|
||
env.element.innerHTML = prompt.outerHTML + env.element.innerHTML; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is not part of this PR, but I've just noticed this line. How about something like this:
pre.insertBefore(prompt, env.element);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or even better:
env.element.insertBefore(prompt, env.element.firstChild);
It is a bit strange, that user has to specify the output lines with EDIT: I guess it depends on the type of content. If you have more commands or more output lines... |
Exactly. If the purpose of the plugin is to highlight commands, then the output lines would be the outliers. That's the way I see it, anyway. |
Your argument seems like a philosophical concern, whereas the problem I pointed out is very real: Specifying line numbers is not only difficult (how do you even know if you're not using the line number plugin? Do you count them, one by one, with your finger?), but also fragile (all numbers change if new lines are added).
Same way as any metacharacter or keyword in any language, ever: You provide an escape sequence to escape it in the odd chance it's actually used as code. We're not treading new ground here :P |
That seems easier than manually adding text before every line of output, but that could be solely because I've only done it a handful of times using the current approach and no other. From a usability perspective, would you rather count the lines or paste special chars before each line? To simplify that, we could instead insert beginning and ending markers around the output lines, but then we're effectively resorting to marking it up.
Obviously, we could make it very unlikely that the characters would match the output (e.g., |
From a usability perspective, I would definitely prefer to add a prefix before these lines than to have to find their line numbers and keep them in sync with the code. |
If that's a concern, then it might make sense to add extra data. I'd appreciate retaining the attribute's functionality so I wouldn't have to add the data in my case, but maybe I'll change my mind once I see it in action.
|
I think it is a good idea to also support a marker character for the output lines. You already have the code lines in the variable // using > as line marker and \> to escape it
for (var i = 0; i < codeLines.length; ++i) {
codeLines[i] = codeLines[i].replace(/(^\s*)(\\?>)(.*)/, function(match, p1, p2, p3) {
if (p2 == '>') {
outputLines[i] = p1 + p3;
return '';
} else {
return p1 + '>' + p3;
}
});
} |
Is If it's being used to mark the output lines, is there any reason to allow whitespace in front of the marker? I would think it should come at the beginning of the line when used as a marker. |
No, that was just an example. Feel free to use something different. I chose it, because it reminded me of the multiline string marker in bash:
You are right, there is no good reason. If we disallow whitespace, then we don't need a regex and we can simplify the code. |
I think |
@zeitgeist87: thoughts on I'd lean toward all lowercase or all uppercase, but we might as well make it case-insensitive anyway. |
@LeaVerou, @zeitgeist87: Would you expect the EDIT: Actually, I'd lean toward skipping the line prefix check if the user provides a |
I think it's better not to allow them at the same time. As you pointed out, there is no need to force the user to escape code that only uses the
We could do it like the
would be escaped like this:
Notice that the output lines also have a space added in front of them. We don't have to use |
The only issue I see with such an approach is that would mean a little more effort to prefix every line--even when there are no output lines. Although, we could allow an empty |
89de6ce
to
51a9f5d
Compare
Allowed specifying output prefix using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking pretty good! Please take a look at my comments.
var outputFilter = pre.getAttribute('data-filter-output'); | ||
if (outputSections || outputSections === '') { // The user specified the output lines. -- cwells | ||
outputSections = outputSections.split(','); | ||
for (var i in outputSections) { // Parse the output sections into start/end ranges. -- cwells |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a big fan of iterating through Arrays with for/in
loops. Could you change this to a regular for
loop please?
outputSections = outputSections.split(','); | ||
for (var i in outputSections) { // Parse the output sections into start/end ranges. -- cwells | ||
var range = outputSections[i].split('-'); | ||
var outputStart = parseInt(range[0]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might want to specify parseInt
's second parameter to 10
, just to make 012-013
work the same as 12-13
.
} | ||
} | ||
} else if (outputFilter) { // Treat lines beginning with this string as output. -- cwells | ||
for (var i in codeLines) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, please use a regular for
loop.
|
||
// Reinsert the output lines into the highlighted code. -- cwells | ||
var codeLines = env.highlightedCode.split('\n'); | ||
for (var i in env.vars['command-line'].outputLines) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
|
||
var pre = env.element.parentNode; | ||
var clsReg = /\s*\bcommand-line\b\s*/; | ||
if (clsReg.test(env.element.className)) { // Remove the class "command-line" from the <code> | ||
env.element.className = env.element.className.replace(clsReg, ''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering: if the class of the <code>
is something like language-shell command-line foobar
, won't it be replaced with language-shellfoobar
here? The replacement string should probably be a space ' '
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't believe I missed that. :( Thanks for catching it.
} | ||
|
||
var pre = env.element.parentNode; | ||
var clsReg = /\s*\bcommand-line\b\s*/; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you could move that declaration outside of the hooks definitions, since you use it twice.
} | ||
} | ||
// Remove the prompt from the output lines. -- cwells | ||
for (var i in env.vars['command-line'].outputLines) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, please use a regular for
loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
outputLines
should only have an i
for lines that are output, so I leveraged that in each loop through its properties. I switched the loops anyway and guarded with hasOwnProperty()
.
5dd9959
to
9c5ad6d
Compare
I've made the updates and squashed it all into 1 commit. |
Alright, looks good to me! |
Lines marked as output are skipped by the highlighting process to prevent false positives.