Skip to content

Commit

Permalink
Merge pull request #787 from zeitgeist87/BashString
Browse files Browse the repository at this point in the history
Add support for Here-Documents for the Bash language
  • Loading branch information
Golmote committed Oct 2, 2015
2 parents 0e4d16f + 71bf8e9 commit b57a096
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 42 deletions.
72 changes: 42 additions & 30 deletions components/prism-bash.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
Prism.languages.bash = Prism.languages.extend('clike', {
'comment': {
pattern: /(^|[^"{\\])#.*/,
lookbehind: true
},
'string': {
//allow multiline string
pattern: /("|')(\\?[\s\S])*?\1/,
inside: {
//'property' class reused for bash variables
'property': /\$([a-zA-Z0-9_#\?\-\*!@]+|\{[^\}]+\})/
}
},
// Redefined to prevent highlighting of numbers in filenames
'number': {
pattern: /([^\w\.])-?(0x[\dA-Fa-f]+|\d*\.?\d+([Ee]-?\d+)?)\b/,
lookbehind: true
},
// Originally based on http://ss64.com/bash/
'function': /\b(?:alias|apropos|apt-get|aptitude|aspell|awk|basename|bash|bc|bg|builtin|bzip2|cal|cat|cd|cfdisk|chgrp|chmod|chown|chroot|chkconfig|cksum|clear|cmp|comm|command|cp|cron|crontab|csplit|cut|date|dc|dd|ddrescue|df|diff|diff3|dig|dir|dircolors|dirname|dirs|dmesg|du|egrep|eject|enable|env|ethtool|eval|exec|expand|expect|export|expr|fdformat|fdisk|fg|fgrep|file|find|fmt|fold|format|free|fsck|ftp|fuser|gawk|getopts|git|grep|groupadd|groupdel|groupmod|groups|gzip|hash|head|help|hg|history|hostname|htop|iconv|id|ifconfig|ifdown|ifup|import|install|jobs|join|kill|killall|less|link|ln|locate|logname|logout|look|lpc|lpr|lprint|lprintd|lprintq|lprm|ls|lsof|make|man|mkdir|mkfifo|mkisofs|mknod|more|most|mount|mtools|mtr|mv|mmv|nano|netstat|nice|nl|nohup|notify-send|nslookup|open|op|passwd|paste|pathchk|ping|pkill|popd|pr|printcap|printenv|printf|ps|pushd|pv|pwd|quota|quotacheck|quotactl|ram|rar|rcp|read|readarray|readonly|reboot|rename|renice|remsync|rev|rm|rmdir|rsync|screen|scp|sdiff|sed|seq|service|sftp|shift|shopt|shutdown|sleep|slocate|sort|source|split|ssh|stat|strace|su|sudo|sum|suspend|sync|tail|tar|tee|test|time|timeout|times|touch|top|traceroute|trap|tr|tsort|tty|type|ulimit|umask|umount|unalias|uname|unexpand|uniq|units|unrar|unshar|uptime|useradd|userdel|usermod|users|uuencode|uudecode|v|vdir|vi|vmstat|wait|watch|wc|wget|whereis|which|who|whoami|write|xargs|xdg-open|yes|zip)\b/,
'keyword': /\b(if|then|else|elif|fi|for|break|continue|while|in|case|function|select|do|done|until|echo|exit|return|set|declare)\b/
});
(function(Prism) {
var bashVars = {
variable: /\$(?:[a-zA-Z0-9_#\?\-\*!@]+|\{[^}]+\})/
};

Prism.languages.bash = Prism.languages.extend('clike', {
'comment': {
pattern: /(^|[^"{\\])#.*/,
lookbehind: true
},
'string': [
{
pattern: /"(?:\\?[\s\S])*?"/g,
inside: bashVars
},
// Single quote strings cannot have variables inside
/'(?:\\?[\s\S])*?'/g,
// Support for Here-Dokuments https://en.wikipedia.org/wiki/Here_document
{
pattern: /(<<\s*)(\w+?)\s*\r?\n(?:[\s\S])*?\r?\n\2/g,
lookbehind: true,
inside: bashVars
}
],
// Redefined to prevent highlighting of numbers in filenames
'number': {
pattern: /([^\w\.])-?(0x[\dA-Fa-f]+|\d*\.?\d+([Ee]-?\d+)?)\b/,
lookbehind: true
},
// Originally based on http://ss64.com/bash/
'function': /\b(?:alias|apropos|apt-get|aptitude|aspell|awk|basename|bash|bc|bg|builtin|bzip2|cal|cat|cd|cfdisk|chgrp|chmod|chown|chroot|chkconfig|cksum|clear|cmp|comm|command|cp|cron|crontab|csplit|cut|date|dc|dd|ddrescue|df|diff|diff3|dig|dir|dircolors|dirname|dirs|dmesg|du|egrep|eject|enable|env|ethtool|eval|exec|expand|expect|export|expr|fdformat|fdisk|fg|fgrep|file|find|fmt|fold|format|free|fsck|ftp|fuser|gawk|getopts|git|grep|groupadd|groupdel|groupmod|groups|gzip|hash|head|help|hg|history|hostname|htop|iconv|id|ifconfig|ifdown|ifup|import|install|jobs|join|kill|killall|less|link|ln|locate|logname|logout|look|lpc|lpr|lprint|lprintd|lprintq|lprm|ls|lsof|make|man|mkdir|mkfifo|mkisofs|mknod|more|most|mount|mtools|mtr|mv|mmv|nano|netstat|nice|nl|nohup|notify-send|nslookup|open|op|passwd|paste|pathchk|ping|pkill|popd|pr|printcap|printenv|printf|ps|pushd|pv|pwd|quota|quotacheck|quotactl|ram|rar|rcp|read|readarray|readonly|reboot|rename|renice|remsync|rev|rm|rmdir|rsync|screen|scp|sdiff|sed|seq|service|sftp|shift|shopt|shutdown|sleep|slocate|sort|source|split|ssh|stat|strace|su|sudo|sum|suspend|sync|tail|tar|tee|test|time|timeout|times|touch|top|traceroute|trap|tr|tsort|tty|type|ulimit|umask|umount|unalias|uname|unexpand|uniq|units|unrar|unshar|uptime|useradd|userdel|usermod|users|uuencode|uudecode|v|vdir|vi|vmstat|wait|watch|wc|wget|whereis|which|who|whoami|write|xargs|xdg-open|yes|zip)\b/,
'keyword': /\b(if|then|else|elif|fi|for|break|continue|while|in|case|function|select|do|done|until|echo|exit|return|set|declare)\b/
});

Prism.languages.insertBefore('bash', 'keyword', {
//'property' class reused for bash variables
'property': /\$([a-zA-Z0-9_#\?\-\*!@]+|\{[^}]+\})/
});
Prism.languages.insertBefore('bash', 'comment', {
//shebang must be before comment, 'important' class from css reused
'important': /^#!\s*\/bin\/bash|^#!\s*\/bin\/sh/
});
Prism.languages.insertBefore('bash', 'keyword', bashVars);
Prism.languages.insertBefore('bash', 'comment', {
// Shebang must be before comment
shebang: {
pattern: /^#!\s*\/bin\/bash|^#!\s*\/bin\/sh/,
alias: 'important'
}
});
})(Prism);
2 changes: 1 addition & 1 deletion components/prism-bash.min.js

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

6 changes: 5 additions & 1 deletion examples/prism-bash.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ <h2>Strings</h2>
'Single and
multi-line strings are supported.'
"Single and
multi-line strings are supported."</code></pre>
multi-line strings are supported."
cat &lt;&lt; EOF
Here-Documents
are also supported
EOF</code></pre>

<h2>Variables</h2>
<pre><code>echo $STRING
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
----------------------------------------------------

[
["important", "#!/bin/bash"]
["shebang", "#!/bin/bash"]
]

----------------------------------------------------
Expand Down
26 changes: 20 additions & 6 deletions tests/languages/bash/string_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,36 @@ bar"
bar'
"$@"
"${foo}"
<< STRING_END
foo
bar
STRING_END
<< EOF
foo $@
bar
EOF

----------------------------------------------------

[
["string", ["\"\""]],
["string", ["''"]],
["string", "''"],
["string", ["\"foo\""]],
["string", ["'foo'"]],
["string", "'foo'"],
["string", ["\"foo\r\nbar\""]],
["string", ["'foo\r\nbar'"]],
["string", "'foo\r\nbar'"],
["string", [
"\"", ["property", "$@"], "\""
"\"", ["variable", "$@"], "\""
]],
["string", [
"\"", ["property", "${foo}"], "\""
]]
"\"", ["variable", "${foo}"], "\""
]],
["operator", "<"],
["operator", "<"],
["string", ["STRING_END\r\nfoo\r\nbar\r\nSTRING_END"]],
["operator", "<"],
["operator", "<"],
["string", ["EOF\r\nfoo ", ["variable", "$@"], "\r\nbar\r\nEOF"]]
]

----------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions tests/languages/bash/variable_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ ${foo bar}
----------------------------------------------------

[
["property", "$foo"],
["property", "$@"],
["property", "${foo bar}"]
["variable", "$foo"],
["variable", "$@"],
["variable", "${foo bar}"]
]

----------------------------------------------------
Expand Down

0 comments on commit b57a096

Please sign in to comment.