UNPKG

4.95 kBJavaScriptView Raw
1/**
2 * Original by Samuel Flores
3 *
4 * Adds the following new token classes:
5 * constant, builtin, variable, symbol, regex
6 */
7(function (Prism) {
8 Prism.languages.ruby = Prism.languages.extend('clike', {
9 'comment': {
10 pattern: /#.*|^=begin\s[\s\S]*?^=end/m,
11 greedy: true
12 },
13 'class-name': {
14 pattern: /(\b(?:class|module)\s+|\bcatch\s+\()[\w.\\]+|\b[A-Z_]\w*(?=\s*\.\s*new\b)/,
15 lookbehind: true,
16 inside: {
17 'punctuation': /[.\\]/
18 }
19 },
20 'keyword': /\b(?:BEGIN|END|alias|and|begin|break|case|class|def|define_method|defined|do|each|else|elsif|end|ensure|extend|for|if|in|include|module|new|next|nil|not|or|prepend|private|protected|public|raise|redo|require|rescue|retry|return|self|super|then|throw|undef|unless|until|when|while|yield)\b/,
21 'operator': /\.{2,3}|&\.|===|<?=>|[!=]?~|(?:&&|\|\||<<|>>|\*\*|[+\-*/%<>!^&|=])=?|[?:]/,
22 'punctuation': /[(){}[\].,;]/,
23 });
24
25 Prism.languages.insertBefore('ruby', 'operator', {
26 'double-colon': {
27 pattern: /::/,
28 alias: 'punctuation'
29 },
30 });
31
32 var interpolation = {
33 pattern: /((?:^|[^\\])(?:\\{2})*)#\{(?:[^{}]|\{[^{}]*\})*\}/,
34 lookbehind: true,
35 inside: {
36 'content': {
37 pattern: /^(#\{)[\s\S]+(?=\}$)/,
38 lookbehind: true,
39 inside: Prism.languages.ruby
40 },
41 'delimiter': {
42 pattern: /^#\{|\}$/,
43 alias: 'punctuation'
44 }
45 }
46 };
47
48 delete Prism.languages.ruby.function;
49
50 var percentExpression = '(?:' + [
51 /([^a-zA-Z0-9\s{(\[<=])(?:(?!\1)[^\\]|\\[\s\S])*\1/.source,
52 /\((?:[^()\\]|\\[\s\S]|\((?:[^()\\]|\\[\s\S])*\))*\)/.source,
53 /\{(?:[^{}\\]|\\[\s\S]|\{(?:[^{}\\]|\\[\s\S])*\})*\}/.source,
54 /\[(?:[^\[\]\\]|\\[\s\S]|\[(?:[^\[\]\\]|\\[\s\S])*\])*\]/.source,
55 /<(?:[^<>\\]|\\[\s\S]|<(?:[^<>\\]|\\[\s\S])*>)*>/.source
56 ].join('|') + ')';
57
58 var symbolName = /(?:"(?:\\.|[^"\\\r\n])*"|(?:\b[a-zA-Z_]\w*|[^\s\0-\x7F]+)[?!]?|\$.)/.source;
59
60 Prism.languages.insertBefore('ruby', 'keyword', {
61 'regex-literal': [
62 {
63 pattern: RegExp(/%r/.source + percentExpression + /[egimnosux]{0,6}/.source),
64 greedy: true,
65 inside: {
66 'interpolation': interpolation,
67 'regex': /[\s\S]+/
68 }
69 },
70 {
71 pattern: /(^|[^/])\/(?!\/)(?:\[[^\r\n\]]+\]|\\.|[^[/\\\r\n])+\/[egimnosux]{0,6}(?=\s*(?:$|[\r\n,.;})#]))/,
72 lookbehind: true,
73 greedy: true,
74 inside: {
75 'interpolation': interpolation,
76 'regex': /[\s\S]+/
77 }
78 }
79 ],
80 'variable': /[@$]+[a-zA-Z_]\w*(?:[?!]|\b)/,
81 'symbol': [
82 {
83 pattern: RegExp(/(^|[^:]):/.source + symbolName),
84 lookbehind: true,
85 greedy: true
86 },
87 {
88 pattern: RegExp(/([\r\n{(,][ \t]*)/.source + symbolName + /(?=:(?!:))/.source),
89 lookbehind: true,
90 greedy: true
91 },
92 ],
93 'method-definition': {
94 pattern: /(\bdef\s+)\w+(?:\s*\.\s*\w+)?/,
95 lookbehind: true,
96 inside: {
97 'function': /\b\w+$/,
98 'keyword': /^self\b/,
99 'class-name': /^\w+/,
100 'punctuation': /\./
101 }
102 }
103 });
104
105 Prism.languages.insertBefore('ruby', 'string', {
106 'string-literal': [
107 {
108 pattern: RegExp(/%[qQiIwWs]?/.source + percentExpression),
109 greedy: true,
110 inside: {
111 'interpolation': interpolation,
112 'string': /[\s\S]+/
113 }
114 },
115 {
116 pattern: /("|')(?:#\{[^}]+\}|#(?!\{)|\\(?:\r\n|[\s\S])|(?!\1)[^\\#\r\n])*\1/,
117 greedy: true,
118 inside: {
119 'interpolation': interpolation,
120 'string': /[\s\S]+/
121 }
122 },
123 {
124 pattern: /<<[-~]?([a-z_]\w*)[\r\n](?:.*[\r\n])*?[\t ]*\1/i,
125 alias: 'heredoc-string',
126 greedy: true,
127 inside: {
128 'delimiter': {
129 pattern: /^<<[-~]?[a-z_]\w*|\b[a-z_]\w*$/i,
130 inside: {
131 'symbol': /\b\w+/,
132 'punctuation': /^<<[-~]?/
133 }
134 },
135 'interpolation': interpolation,
136 'string': /[\s\S]+/
137 }
138 },
139 {
140 pattern: /<<[-~]?'([a-z_]\w*)'[\r\n](?:.*[\r\n])*?[\t ]*\1/i,
141 alias: 'heredoc-string',
142 greedy: true,
143 inside: {
144 'delimiter': {
145 pattern: /^<<[-~]?'[a-z_]\w*'|\b[a-z_]\w*$/i,
146 inside: {
147 'symbol': /\b\w+/,
148 'punctuation': /^<<[-~]?'|'$/,
149 }
150 },
151 'string': /[\s\S]+/
152 }
153 }
154 ],
155 'command-literal': [
156 {
157 pattern: RegExp(/%x/.source + percentExpression),
158 greedy: true,
159 inside: {
160 'interpolation': interpolation,
161 'command': {
162 pattern: /[\s\S]+/,
163 alias: 'string'
164 }
165 }
166 },
167 {
168 pattern: /`(?:#\{[^}]+\}|#(?!\{)|\\(?:\r\n|[\s\S])|[^\\`#\r\n])*`/,
169 greedy: true,
170 inside: {
171 'interpolation': interpolation,
172 'command': {
173 pattern: /[\s\S]+/,
174 alias: 'string'
175 }
176 }
177 }
178 ]
179 });
180
181 delete Prism.languages.ruby.string;
182
183 Prism.languages.insertBefore('ruby', 'number', {
184 'builtin': /\b(?:Array|Bignum|Binding|Class|Continuation|Dir|Exception|FalseClass|File|Fixnum|Float|Hash|IO|Integer|MatchData|Method|Module|NilClass|Numeric|Object|Proc|Range|Regexp|Stat|String|Struct|Symbol|TMS|Thread|ThreadGroup|Time|TrueClass)\b/,
185 'constant': /\b[A-Z][A-Z0-9_]*(?:[?!]|\b)/
186 });
187
188 Prism.languages.rb = Prism.languages.ruby;
189}(Prism));
190
\No newline at end of file