forked from highlightjs/highlight.js
/
bash.js
122 lines (118 loc) · 3.22 KB
/
bash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Language: Bash
Author: vah <vahtenberg@gmail.com>
Contributrors: Benjamin Pannell <contact@sierrasoftworks.com>
Website: https://www.gnu.org/software/bash/
Category: common
*/
export default function(hljs) {
const VAR = {};
const BRACED_VAR = {
begin: /\$\{/, end:/\}/,
contains: [
{ begin: /:-/, contains: [VAR] } // default values
]
};
Object.assign(VAR,{
className: 'variable',
variants: [
{begin: /\$[\w\d#@][\w\d_]*/},
BRACED_VAR
]
});
const SUBST = {
className: 'subst',
begin: /\$\(/, end: /\)/,
contains: [hljs.BACKSLASH_ESCAPE]
};
const QUOTE_STRING = {
className: 'string',
begin: /"/, end: /"/,
contains: [
hljs.BACKSLASH_ESCAPE,
VAR,
SUBST
]
};
SUBST.contains.push(QUOTE_STRING);
const ESCAPED_QUOTE = {
className: '',
begin: /\\"/
};
const APOS_STRING = {
className: 'string',
begin: /'/, end: /'/
};
const ARITHMETIC = {
begin: /\$\(\(/,
end: /\)\)/,
contains: [
{ begin: /\d+#[0-9a-f]+/, className: "number" },
hljs.NUMBER_MODE,
VAR
]
};
const SH_LIKE_SHELLS = [
"fish",
"bash",
"zsh",
"sh",
"csh",
"ksh",
"tcsh",
"dash",
"scsh",
];
const KNOWN_SHEBANG = hljs.SHEBANG({
binary: `(${SH_LIKE_SHELLS.join("|")})`,
relevance: 10
});
const FUNCTION = {
className: 'function',
begin: /\w[\w\d_]*\s*\(\s*\)\s*\{/,
returnBegin: true,
contains: [hljs.inherit(hljs.TITLE_MODE, {begin: /\w[\w\d_]*/})],
relevance: 0
};
return {
name: 'Bash',
aliases: ['sh', 'zsh'],
keywords: {
$pattern: /\b-?[a-z\._]+\b/,
keyword:
'if then else elif fi for while in do done case esac function',
literal:
'true false',
built_in:
// Shell built-ins
// http://www.gnu.org/software/bash/manual/html_node/Shell-Builtin-Commands.html
'break cd continue eval exec exit export getopts hash pwd readonly return shift test times ' +
'trap umask unset ' +
// Bash built-ins
'alias bind builtin caller command declare echo enable help let local logout mapfile printf ' +
'read readarray source type typeset ulimit unalias ' +
// Shell modifiers
'set shopt ' +
// Zsh built-ins
'autoload bg bindkey bye cap chdir clone comparguments compcall compctl compdescribe compfiles ' +
'compgroups compquote comptags comptry compvalues dirs disable disown echotc echoti emulate ' +
'fc fg float functions getcap getln history integer jobs kill limit log noglob popd print ' +
'pushd pushln rehash sched setcap setopt stat suspend ttyctl unfunction unhash unlimit ' +
'unsetopt vared wait whence where which zcompile zformat zftp zle zmodload zparseopts zprof ' +
'zpty zregexparse zsocket zstyle ztcp',
_:
'-ne -eq -lt -gt -f -d -e -s -l -a' // relevance booster
},
contains: [
KNOWN_SHEBANG, // to catch known shells and boost relevancy
hljs.SHEBANG(), // to catch unknown shells but still highlight the shebang
FUNCTION,
ARITHMETIC,
hljs.HASH_COMMENT_MODE,
QUOTE_STRING,
ESCAPED_QUOTE,
APOS_STRING,
VAR
]
};
}