-
Notifications
You must be signed in to change notification settings - Fork 447
Highlight bash commands #1544
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
Open
st0012
wants to merge
2
commits into
master
Choose a base branch
from
highlight-bash-commands
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+381
−36
Open
Highlight bash commands #1544
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
lib/rdoc/generator/template/aliki/js/bash_highlighter.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| /** | ||
| * Client-side shell syntax highlighter for RDoc | ||
| * Highlights: $ prompts, commands, options, strings, env vars, comments | ||
| */ | ||
|
|
||
| (function() { | ||
| 'use strict'; | ||
|
|
||
| function escapeHtml(text) { | ||
| return text | ||
| .replace(/&/g, '&') | ||
| .replace(/</g, '<') | ||
| .replace(/>/g, '>') | ||
| .replace(/"/g, '"') | ||
| .replace(/'/g, '''); | ||
| } | ||
|
|
||
| function wrap(className, text) { | ||
| return '<span class="' + className + '">' + escapeHtml(text) + '</span>'; | ||
| } | ||
|
|
||
| function highlightLine(line) { | ||
| if (line.trim() === '') return escapeHtml(line); | ||
|
|
||
| var result = ''; | ||
| var i = 0; | ||
| var len = line.length; | ||
|
|
||
| // Preserve leading whitespace | ||
| while (i < len && (line[i] === ' ' || line[i] === '\t')) { | ||
| result += escapeHtml(line[i++]); | ||
| } | ||
|
|
||
| // Check for $ prompt ($ followed by space or end of line) | ||
| if (line[i] === '$' && (line[i + 1] === ' ' || line[i + 1] === undefined)) { | ||
| result += wrap('sh-prompt', '$'); | ||
| i++; | ||
| } | ||
|
|
||
| // Check for # comment at start | ||
| if (line[i] === '#') { | ||
| return result + wrap('sh-comment', line.slice(i)); | ||
| } | ||
|
|
||
| var seenCommand = false; | ||
| var afterSpace = true; | ||
|
|
||
| while (i < len) { | ||
| var ch = line[i]; | ||
|
|
||
| // Whitespace | ||
| if (ch === ' ' || ch === '\t') { | ||
| result += escapeHtml(ch); | ||
| i++; | ||
| afterSpace = true; | ||
| continue; | ||
| } | ||
|
|
||
| // Comment after whitespace | ||
| if (ch === '#' && afterSpace) { | ||
| result += wrap('sh-comment', line.slice(i)); | ||
| break; | ||
| } | ||
|
|
||
| // Double-quoted string | ||
| if (ch === '"') { | ||
| var end = i + 1; | ||
| while (end < len && line[end] !== '"') { | ||
| if (line[end] === '\\' && end + 1 < len) end += 2; | ||
| else end++; | ||
| } | ||
| if (end < len) end++; | ||
| result += wrap('sh-string', line.slice(i, end)); | ||
| i = end; | ||
| afterSpace = false; | ||
| continue; | ||
| } | ||
|
|
||
| // Single-quoted string | ||
| if (ch === "'") { | ||
| var end = i + 1; | ||
| while (end < len && line[end] !== "'") end++; | ||
| if (end < len) end++; | ||
| result += wrap('sh-string', line.slice(i, end)); | ||
| i = end; | ||
| afterSpace = false; | ||
| continue; | ||
| } | ||
|
|
||
| // Environment variable (ALLCAPS=) | ||
| if (afterSpace && /[A-Z]/.test(ch)) { | ||
| var match = line.slice(i).match(/^[A-Z][A-Z0-9_]*=/); | ||
| if (match) { | ||
| result += wrap('sh-envvar', match[0]); | ||
| i += match[0].length; | ||
| // Read unquoted value | ||
| var valEnd = i; | ||
| while (valEnd < len && line[valEnd] !== ' ' && line[valEnd] !== '\t' && line[valEnd] !== '"' && line[valEnd] !== "'") valEnd++; | ||
| if (valEnd > i) { | ||
| result += escapeHtml(line.slice(i, valEnd)); | ||
| i = valEnd; | ||
| } | ||
| afterSpace = false; | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| // Option (must be after whitespace) | ||
| if (ch === '-' && afterSpace) { | ||
| var match = line.slice(i).match(/^--?[a-zA-Z0-9_-]+(=[^\s]*)?/); | ||
| if (match) { | ||
| result += wrap('sh-option', match[0]); | ||
| i += match[0].length; | ||
| afterSpace = false; | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| // Command (first word: regular, ./path, ../path, ~/path, @scope/pkg) | ||
| if (!seenCommand && afterSpace) { | ||
| var isCmd = /[a-zA-Z0-9@~]/.test(ch) || | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about adding |
||
| (ch === '.' && (line[i + 1] === '/' || (line[i + 1] === '.' && line[i + 2] === '/'))); | ||
| if (isCmd) { | ||
| var end = i; | ||
| while (end < len && line[end] !== ' ' && line[end] !== '\t') end++; | ||
| result += wrap('sh-command', line.slice(i, end)); | ||
| i = end; | ||
| seenCommand = true; | ||
| afterSpace = false; | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| // Everything else | ||
| result += escapeHtml(ch); | ||
| i++; | ||
| afterSpace = false; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| function highlightShell(code) { | ||
| return code.split('\n').map(highlightLine).join('\n'); | ||
| } | ||
|
|
||
| function initHighlighting() { | ||
| var selectors = [ | ||
| 'pre.bash', 'pre.sh', 'pre.shell', 'pre.console', | ||
| 'pre[data-language="bash"]', 'pre[data-language="sh"]', | ||
| 'pre[data-language="shell"]', 'pre[data-language="console"]' | ||
| ]; | ||
|
|
||
| var blocks = document.querySelectorAll(selectors.join(', ')); | ||
| blocks.forEach(function(block) { | ||
| if (block.getAttribute('data-highlighted') === 'true') return; | ||
| block.innerHTML = highlightShell(block.textContent); | ||
| block.setAttribute('data-highlighted', 'true'); | ||
| }); | ||
| } | ||
|
|
||
| if (document.readyState === 'loading') { | ||
| document.addEventListener('DOMContentLoaded', initHighlighting); | ||
| } else { | ||
| initHighlighting(); | ||
| } | ||
| })(); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Assuming this input
git commit --message="initial commit"How about
(=[^\s]*)?→(=[^"\s]*)?""part will be handled in// Double-quoted stringlogic above.