Skip to content

Commit b57a096

Browse files
committed
Merge pull request #787 from zeitgeist87/BashString
Add support for Here-Documents for the Bash language
2 parents 0e4d16f + 71bf8e9 commit b57a096

File tree

6 files changed

+72
-42
lines changed

6 files changed

+72
-42
lines changed

components/prism-bash.js

+42-30
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,43 @@
1-
Prism.languages.bash = Prism.languages.extend('clike', {
2-
'comment': {
3-
pattern: /(^|[^"{\\])#.*/,
4-
lookbehind: true
5-
},
6-
'string': {
7-
//allow multiline string
8-
pattern: /("|')(\\?[\s\S])*?\1/,
9-
inside: {
10-
//'property' class reused for bash variables
11-
'property': /\$([a-zA-Z0-9_#\?\-\*!@]+|\{[^\}]+\})/
12-
}
13-
},
14-
// Redefined to prevent highlighting of numbers in filenames
15-
'number': {
16-
pattern: /([^\w\.])-?(0x[\dA-Fa-f]+|\d*\.?\d+([Ee]-?\d+)?)\b/,
17-
lookbehind: true
18-
},
19-
// Originally based on http://ss64.com/bash/
20-
'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/,
21-
'keyword': /\b(if|then|else|elif|fi|for|break|continue|while|in|case|function|select|do|done|until|echo|exit|return|set|declare)\b/
22-
});
1+
(function(Prism) {
2+
var bashVars = {
3+
variable: /\$(?:[a-zA-Z0-9_#\?\-\*!@]+|\{[^}]+\})/
4+
};
5+
6+
Prism.languages.bash = Prism.languages.extend('clike', {
7+
'comment': {
8+
pattern: /(^|[^"{\\])#.*/,
9+
lookbehind: true
10+
},
11+
'string': [
12+
{
13+
pattern: /"(?:\\?[\s\S])*?"/g,
14+
inside: bashVars
15+
},
16+
// Single quote strings cannot have variables inside
17+
/'(?:\\?[\s\S])*?'/g,
18+
// Support for Here-Dokuments https://en.wikipedia.org/wiki/Here_document
19+
{
20+
pattern: /(<<\s*)(\w+?)\s*\r?\n(?:[\s\S])*?\r?\n\2/g,
21+
lookbehind: true,
22+
inside: bashVars
23+
}
24+
],
25+
// Redefined to prevent highlighting of numbers in filenames
26+
'number': {
27+
pattern: /([^\w\.])-?(0x[\dA-Fa-f]+|\d*\.?\d+([Ee]-?\d+)?)\b/,
28+
lookbehind: true
29+
},
30+
// Originally based on http://ss64.com/bash/
31+
'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/,
32+
'keyword': /\b(if|then|else|elif|fi|for|break|continue|while|in|case|function|select|do|done|until|echo|exit|return|set|declare)\b/
33+
});
2334

24-
Prism.languages.insertBefore('bash', 'keyword', {
25-
//'property' class reused for bash variables
26-
'property': /\$([a-zA-Z0-9_#\?\-\*!@]+|\{[^}]+\})/
27-
});
28-
Prism.languages.insertBefore('bash', 'comment', {
29-
//shebang must be before comment, 'important' class from css reused
30-
'important': /^#!\s*\/bin\/bash|^#!\s*\/bin\/sh/
31-
});
35+
Prism.languages.insertBefore('bash', 'keyword', bashVars);
36+
Prism.languages.insertBefore('bash', 'comment', {
37+
// Shebang must be before comment
38+
shebang: {
39+
pattern: /^#!\s*\/bin\/bash|^#!\s*\/bin\/sh/,
40+
alias: 'important'
41+
}
42+
});
43+
})(Prism);

components/prism-bash.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/prism-bash.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ <h2>Strings</h2>
1212
'Single and
1313
multi-line strings are supported.'
1414
"Single and
15-
multi-line strings are supported."</code></pre>
15+
multi-line strings are supported."
16+
cat &lt;&lt; EOF
17+
Here-Documents
18+
are also supported
19+
EOF</code></pre>
1620

1721
<h2>Variables</h2>
1822
<pre><code>echo $STRING

tests/languages/bash/important_feature.test renamed to tests/languages/bash/shebang_feature.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
----------------------------------------------------
44

55
[
6-
["important", "#!/bin/bash"]
6+
["shebang", "#!/bin/bash"]
77
]
88

99
----------------------------------------------------

tests/languages/bash/string_feature.test

+20-6
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,36 @@ bar"
88
bar'
99
"$@"
1010
"${foo}"
11+
<< STRING_END
12+
foo
13+
bar
14+
STRING_END
15+
<< EOF
16+
foo $@
17+
bar
18+
EOF
1119

1220
----------------------------------------------------
1321

1422
[
1523
["string", ["\"\""]],
16-
["string", ["''"]],
24+
["string", "''"],
1725
["string", ["\"foo\""]],
18-
["string", ["'foo'"]],
26+
["string", "'foo'"],
1927
["string", ["\"foo\r\nbar\""]],
20-
["string", ["'foo\r\nbar'"]],
28+
["string", "'foo\r\nbar'"],
2129
["string", [
22-
"\"", ["property", "$@"], "\""
30+
"\"", ["variable", "$@"], "\""
2331
]],
2432
["string", [
25-
"\"", ["property", "${foo}"], "\""
26-
]]
33+
"\"", ["variable", "${foo}"], "\""
34+
]],
35+
["operator", "<"],
36+
["operator", "<"],
37+
["string", ["STRING_END\r\nfoo\r\nbar\r\nSTRING_END"]],
38+
["operator", "<"],
39+
["operator", "<"],
40+
["string", ["EOF\r\nfoo ", ["variable", "$@"], "\r\nbar\r\nEOF"]]
2741
]
2842

2943
----------------------------------------------------

tests/languages/bash/variable_feature.test

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ ${foo bar}
55
----------------------------------------------------
66

77
[
8-
["property", "$foo"],
9-
["property", "$@"],
10-
["property", "${foo bar}"]
8+
["variable", "$foo"],
9+
["variable", "$@"],
10+
["variable", "${foo bar}"]
1111
]
1212

1313
----------------------------------------------------

0 commit comments

Comments
 (0)