prism-bash.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. (function (Prism) {
  2. // $ set | grep '^[A-Z][^[:space:]]*=' | cut -d= -f1 | tr '\n' '|'
  3. // + LC_ALL, RANDOM, REPLY, SECONDS.
  4. // + make sure PS1..4 are here as they are not always set,
  5. // - some useless things.
  6. var envVars = '\\b(?:BASH|BASHOPTS|BASH_ALIASES|BASH_ARGC|BASH_ARGV|BASH_CMDS|BASH_COMPLETION_COMPAT_DIR|BASH_LINENO|BASH_REMATCH|BASH_SOURCE|BASH_VERSINFO|BASH_VERSION|COLORTERM|COLUMNS|COMP_WORDBREAKS|DBUS_SESSION_BUS_ADDRESS|DEFAULTS_PATH|DESKTOP_SESSION|DIRSTACK|DISPLAY|EUID|GDMSESSION|GDM_LANG|GNOME_KEYRING_CONTROL|GNOME_KEYRING_PID|GPG_AGENT_INFO|GROUPS|HISTCONTROL|HISTFILE|HISTFILESIZE|HISTSIZE|HOME|HOSTNAME|HOSTTYPE|IFS|INSTANCE|JOB|LANG|LANGUAGE|LC_ADDRESS|LC_ALL|LC_IDENTIFICATION|LC_MEASUREMENT|LC_MONETARY|LC_NAME|LC_NUMERIC|LC_PAPER|LC_TELEPHONE|LC_TIME|LESSCLOSE|LESSOPEN|LINES|LOGNAME|LS_COLORS|MACHTYPE|MAILCHECK|MANDATORY_PATH|NO_AT_BRIDGE|OLDPWD|OPTERR|OPTIND|ORBIT_SOCKETDIR|OSTYPE|PAPERSIZE|PATH|PIPESTATUS|PPID|PS1|PS2|PS3|PS4|PWD|RANDOM|REPLY|SECONDS|SELINUX_INIT|SESSION|SESSIONTYPE|SESSION_MANAGER|SHELL|SHELLOPTS|SHLVL|SSH_AUTH_SOCK|TERM|UID|UPSTART_EVENTS|UPSTART_INSTANCE|UPSTART_JOB|UPSTART_SESSION|USER|WINDOWID|XAUTHORITY|XDG_CONFIG_DIRS|XDG_CURRENT_DESKTOP|XDG_DATA_DIRS|XDG_GREETER_DATA_DIR|XDG_MENU_PREFIX|XDG_RUNTIME_DIR|XDG_SEAT|XDG_SEAT_PATH|XDG_SESSION_DESKTOP|XDG_SESSION_ID|XDG_SESSION_PATH|XDG_SESSION_TYPE|XDG_VTNR|XMODIFIERS)\\b';
  7. var commandAfterHeredoc = {
  8. pattern: /(^(["']?)\w+\2)[ \t]+\S.*/,
  9. lookbehind: true,
  10. alias: 'punctuation', // this looks reasonably well in all themes
  11. inside: null // see below
  12. };
  13. var insideString = {
  14. 'bash': commandAfterHeredoc,
  15. 'environment': {
  16. pattern: RegExp('\\$' + envVars),
  17. alias: 'constant'
  18. },
  19. 'variable': [
  20. // [0]: Arithmetic Environment
  21. {
  22. pattern: /\$?\(\([\s\S]+?\)\)/,
  23. greedy: true,
  24. inside: {
  25. // If there is a $ sign at the beginning highlight $(( and )) as variable
  26. 'variable': [
  27. {
  28. pattern: /(^\$\(\([\s\S]+)\)\)/,
  29. lookbehind: true
  30. },
  31. /^\$\(\(/
  32. ],
  33. 'number': /\b0x[\dA-Fa-f]+\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:[Ee]-?\d+)?/,
  34. // Operators according to https://www.gnu.org/software/bash/manual/bashref.html#Shell-Arithmetic
  35. 'operator': /--|\+\+|\*\*=?|<<=?|>>=?|&&|\|\||[=!+\-*/%<>^&|]=?|[?~:]/,
  36. // If there is no $ sign at the beginning highlight (( and )) as punctuation
  37. 'punctuation': /\(\(?|\)\)?|,|;/
  38. }
  39. },
  40. // [1]: Command Substitution
  41. {
  42. pattern: /\$\((?:\([^)]+\)|[^()])+\)|`[^`]+`/,
  43. greedy: true,
  44. inside: {
  45. 'variable': /^\$\(|^`|\)$|`$/
  46. }
  47. },
  48. // [2]: Brace expansion
  49. {
  50. pattern: /\$\{[^}]+\}/,
  51. greedy: true,
  52. inside: {
  53. 'operator': /:[-=?+]?|[!\/]|##?|%%?|\^\^?|,,?/,
  54. 'punctuation': /[\[\]]/,
  55. 'environment': {
  56. pattern: RegExp('(\\{)' + envVars),
  57. lookbehind: true,
  58. alias: 'constant'
  59. }
  60. }
  61. },
  62. /\$(?:\w+|[#?*!@$])/
  63. ],
  64. // Escape sequences from echo and printf's manuals, and escaped quotes.
  65. 'entity': /\\(?:[abceEfnrtv\\"]|O?[0-7]{1,3}|U[0-9a-fA-F]{8}|u[0-9a-fA-F]{4}|x[0-9a-fA-F]{1,2})/
  66. };
  67. Prism.languages.bash = {
  68. 'shebang': {
  69. pattern: /^#!\s*\/.*/,
  70. alias: 'important'
  71. },
  72. 'comment': {
  73. pattern: /(^|[^"{\\$])#.*/,
  74. lookbehind: true
  75. },
  76. 'function-name': [
  77. // a) function foo {
  78. // b) foo() {
  79. // c) function foo() {
  80. // but not “foo {”
  81. {
  82. // a) and c)
  83. pattern: /(\bfunction\s+)[\w-]+(?=(?:\s*\(?:\s*\))?\s*\{)/,
  84. lookbehind: true,
  85. alias: 'function'
  86. },
  87. {
  88. // b)
  89. pattern: /\b[\w-]+(?=\s*\(\s*\)\s*\{)/,
  90. alias: 'function'
  91. }
  92. ],
  93. // Highlight variable names as variables in for and select beginnings.
  94. 'for-or-select': {
  95. pattern: /(\b(?:for|select)\s+)\w+(?=\s+in\s)/,
  96. alias: 'variable',
  97. lookbehind: true
  98. },
  99. // Highlight variable names as variables in the left-hand part
  100. // of assignments (“=” and “+=”).
  101. 'assign-left': {
  102. pattern: /(^|[\s;|&]|[<>]\()\w+(?:\.\w+)*(?=\+?=)/,
  103. inside: {
  104. 'environment': {
  105. pattern: RegExp('(^|[\\s;|&]|[<>]\\()' + envVars),
  106. lookbehind: true,
  107. alias: 'constant'
  108. }
  109. },
  110. alias: 'variable',
  111. lookbehind: true
  112. },
  113. // Highlight parameter names as variables
  114. 'parameter': {
  115. pattern: /(^|\s)-{1,2}(?:\w+:[+-]?)?\w+(?:\.\w+)*(?=[=\s]|$)/,
  116. alias: 'variable',
  117. lookbehind: true
  118. },
  119. 'string': [
  120. // Support for Here-documents https://en.wikipedia.org/wiki/Here_document
  121. {
  122. pattern: /((?:^|[^<])<<-?\s*)(\w+)\s[\s\S]*?(?:\r?\n|\r)\2/,
  123. lookbehind: true,
  124. greedy: true,
  125. inside: insideString
  126. },
  127. // Here-document with quotes around the tag
  128. // → No expansion (so no “inside”).
  129. {
  130. pattern: /((?:^|[^<])<<-?\s*)(["'])(\w+)\2\s[\s\S]*?(?:\r?\n|\r)\3/,
  131. lookbehind: true,
  132. greedy: true,
  133. inside: {
  134. 'bash': commandAfterHeredoc
  135. }
  136. },
  137. // “Normal” string
  138. {
  139. // https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html
  140. pattern: /(^|[^\\](?:\\\\)*)"(?:\\[\s\S]|\$\([^)]+\)|\$(?!\()|`[^`]+`|[^"\\`$])*"/,
  141. lookbehind: true,
  142. greedy: true,
  143. inside: insideString
  144. },
  145. {
  146. // https://www.gnu.org/software/bash/manual/html_node/Single-Quotes.html
  147. pattern: /(^|[^$\\])'[^']*'/,
  148. lookbehind: true,
  149. greedy: true
  150. },
  151. {
  152. // https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html
  153. pattern: /\$'(?:[^'\\]|\\[\s\S])*'/,
  154. greedy: true,
  155. inside: {
  156. 'entity': insideString.entity
  157. }
  158. }
  159. ],
  160. 'environment': {
  161. pattern: RegExp('\\$?' + envVars),
  162. alias: 'constant'
  163. },
  164. 'variable': insideString.variable,
  165. 'function': {
  166. pattern: /(^|[\s;|&]|[<>]\()(?:add|apropos|apt|apt-cache|apt-get|aptitude|aspell|automysqlbackup|awk|basename|bash|bc|bconsole|bg|bzip2|cal|cargo|cat|cfdisk|chgrp|chkconfig|chmod|chown|chroot|cksum|clear|cmp|column|comm|composer|cp|cron|crontab|csplit|curl|cut|date|dc|dd|ddrescue|debootstrap|df|diff|diff3|dig|dir|dircolors|dirname|dirs|dmesg|docker|docker-compose|du|egrep|eject|env|ethtool|expand|expect|expr|fdformat|fdisk|fg|fgrep|file|find|fmt|fold|format|free|fsck|ftp|fuser|gawk|git|gparted|grep|groupadd|groupdel|groupmod|groups|grub-mkconfig|gzip|halt|head|hg|history|host|hostname|htop|iconv|id|ifconfig|ifdown|ifup|import|install|ip|java|jobs|join|kill|killall|less|link|ln|locate|logname|logrotate|look|lpc|lpr|lprint|lprintd|lprintq|lprm|ls|lsof|lynx|make|man|mc|mdadm|mkconfig|mkdir|mke2fs|mkfifo|mkfs|mkisofs|mknod|mkswap|mmv|more|most|mount|mtools|mtr|mutt|mv|nano|nc|netstat|nice|nl|node|nohup|notify-send|npm|nslookup|op|open|parted|passwd|paste|pathchk|ping|pkill|pnpm|podman|podman-compose|popd|pr|printcap|printenv|ps|pushd|pv|quota|quotacheck|quotactl|ram|rar|rcp|reboot|remsync|rename|renice|rev|rm|rmdir|rpm|rsync|scp|screen|sdiff|sed|sendmail|seq|service|sftp|sh|shellcheck|shuf|shutdown|sleep|slocate|sort|split|ssh|stat|strace|su|sudo|sum|suspend|swapon|sync|sysctl|tac|tail|tar|tee|time|timeout|top|touch|tr|traceroute|tsort|tty|umount|uname|unexpand|uniq|units|unrar|unshar|unzip|update-grub|uptime|useradd|userdel|usermod|users|uudecode|uuencode|v|vcpkg|vdir|vi|vim|virsh|vmstat|wait|watch|wc|wget|whereis|which|who|whoami|write|xargs|xdg-open|yarn|yes|zenity|zip|zsh|zypper)(?=$|[)\s;|&])/,
  167. lookbehind: true
  168. },
  169. 'keyword': {
  170. pattern: /(^|[\s;|&]|[<>]\()(?:case|do|done|elif|else|esac|fi|for|function|if|in|select|then|until|while)(?=$|[)\s;|&])/,
  171. lookbehind: true
  172. },
  173. // https://www.gnu.org/software/bash/manual/html_node/Shell-Builtin-Commands.html
  174. 'builtin': {
  175. pattern: /(^|[\s;|&]|[<>]\()(?:\.|:|alias|bind|break|builtin|caller|cd|command|continue|declare|echo|enable|eval|exec|exit|export|getopts|hash|help|let|local|logout|mapfile|printf|pwd|read|readarray|readonly|return|set|shift|shopt|source|test|times|trap|type|typeset|ulimit|umask|unalias|unset)(?=$|[)\s;|&])/,
  176. lookbehind: true,
  177. // Alias added to make those easier to distinguish from strings.
  178. alias: 'class-name'
  179. },
  180. 'boolean': {
  181. pattern: /(^|[\s;|&]|[<>]\()(?:false|true)(?=$|[)\s;|&])/,
  182. lookbehind: true
  183. },
  184. 'file-descriptor': {
  185. pattern: /\B&\d\b/,
  186. alias: 'important'
  187. },
  188. 'operator': {
  189. // Lots of redirections here, but not just that.
  190. pattern: /\d?<>|>\||\+=|=[=~]?|!=?|<<[<-]?|[&\d]?>>|\d[<>]&?|[<>][&=]?|&[>&]?|\|[&|]?/,
  191. inside: {
  192. 'file-descriptor': {
  193. pattern: /^\d/,
  194. alias: 'important'
  195. }
  196. }
  197. },
  198. 'punctuation': /\$?\(\(?|\)\)?|\.\.|[{}[\];\\]/,
  199. 'number': {
  200. pattern: /(^|\s)(?:[1-9]\d*|0)(?:[.,]\d+)?\b/,
  201. lookbehind: true
  202. }
  203. };
  204. commandAfterHeredoc.inside = Prism.languages.bash;
  205. /* Patterns in command substitution. */
  206. var toBeCopied = [
  207. 'comment',
  208. 'function-name',
  209. 'for-or-select',
  210. 'assign-left',
  211. 'parameter',
  212. 'string',
  213. 'environment',
  214. 'function',
  215. 'keyword',
  216. 'builtin',
  217. 'boolean',
  218. 'file-descriptor',
  219. 'operator',
  220. 'punctuation',
  221. 'number'
  222. ];
  223. var inside = insideString.variable[1].inside;
  224. for (var i = 0; i < toBeCopied.length; i++) {
  225. inside[toBeCopied[i]] = Prism.languages.bash[toBeCopied[i]];
  226. }
  227. Prism.languages.sh = Prism.languages.bash;
  228. Prism.languages.shell = Prism.languages.bash;
  229. }(Prism));