UNPKG

2.07 kBJavaScriptView Raw
1Prism.languages.groovy = Prism.languages.extend('clike', {
2 'keyword': /\b(?:as|def|in|abstract|assert|boolean|break|byte|case|catch|char|class|const|continue|default|do|double|else|enum|extends|final|finally|float|for|goto|if|implements|import|instanceof|int|interface|long|native|new|package|private|protected|public|return|short|static|strictfp|super|switch|synchronized|this|throw|throws|trait|transient|try|void|volatile|while)\b/,
3 'string': [
4 {
5 pattern: /("""|''')[\s\S]*?\1|(?:\$\/)(?:\$\/\$|[\s\S])*?\/\$/,
6 greedy: true
7 },
8 {
9 pattern: /(["'\/])(?:\\.|(?!\1)[^\\\r\n])*\1/,
10 greedy: true
11 }
12 ],
13 'number': /\b(?:0b[01_]+|0x[\da-f_]+(?:\.[\da-f_p\-]+)?|[\d_]+(?:\.[\d_]+)?(?:e[+-]?[\d]+)?)[glidf]?\b/i,
14 'operator': {
15 pattern: /(^|[^.])(?:~|==?~?|\?[.:]?|\*(?:[.=]|\*=?)?|\.[@&]|\.\.<|\.{1,2}(?!\.)|-[-=>]?|\+[+=]?|!=?|<(?:<=?|=>?)?|>(?:>>?=?|=)?|&[&=]?|\|[|=]?|\/=?|\^=?|%=?)/,
16 lookbehind: true
17 },
18 'punctuation': /\.+|[{}[\];(),:$]/
19});
20
21Prism.languages.insertBefore('groovy', 'string', {
22 'shebang': {
23 pattern: /#!.+/,
24 alias: 'comment'
25 }
26});
27
28Prism.languages.insertBefore('groovy', 'punctuation', {
29 'spock-block': /\b(?:setup|given|when|then|and|cleanup|expect|where):/
30});
31
32Prism.languages.insertBefore('groovy', 'function', {
33 'annotation': {
34 alias: 'punctuation',
35 pattern: /(^|[^.])@\w+/,
36 lookbehind: true
37 }
38});
39
40// Handle string interpolation
41Prism.hooks.add('wrap', function(env) {
42 if (env.language === 'groovy' && env.type === 'string') {
43 var delimiter = env.content[0];
44
45 if (delimiter != "'") {
46 var pattern = /([^\\])(?:\$(?:\{.*?\}|[\w.]+))/;
47 if (delimiter === '$') {
48 pattern = /([^\$])(?:\$(?:\{.*?\}|[\w.]+))/;
49 }
50
51 // To prevent double HTML-encoding we have to decode env.content first
52 env.content = env.content.replace(/&lt;/g, '<').replace(/&amp;/g, '&');
53
54 env.content = Prism.highlight(env.content, {
55 'expression': {
56 pattern: pattern,
57 lookbehind: true,
58 inside: Prism.languages.groovy
59 }
60 });
61
62 env.classes.push(delimiter === '/' ? 'regex' : 'gstring');
63 }
64 }
65});