Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50650 views
1
// CodeMirror, copyright (c) by Marijn Haverbeke and others
2
// Distributed under an MIT license: http://codemirror.net/LICENSE
3
4
"use strict";
5
6
CodeMirror.defineMode('rst', function (config, options) {
7
8
var rx_strong = /^\*\*[^\*\s](?:[^\*]*[^\*\s])?\*\*/;
9
var rx_emphasis = /^\*[^\*\s](?:[^\*]*[^\*\s])?\*/;
10
var rx_literal = /^``[^`\s](?:[^`]*[^`\s])``/;
11
12
var rx_number = /^(?:[\d]+(?:[\.,]\d+)*)/;
13
var rx_positive = /^(?:\s\+[\d]+(?:[\.,]\d+)*)/;
14
var rx_negative = /^(?:\s\-[\d]+(?:[\.,]\d+)*)/;
15
16
var rx_uri_protocol = "[Hh][Tt][Tt][Pp][Ss]?://";
17
var rx_uri_domain = "(?:[\\d\\w.-]+)\\.(?:\\w{2,6})";
18
var rx_uri_path = "(?:/[\\d\\w\\#\\%\\&\\-\\.\\,\\/\\:\\=\\?\\~]+)*";
19
var rx_uri = new RegExp("^" + rx_uri_protocol + rx_uri_domain + rx_uri_path);
20
21
var overlay = {
22
token: function (stream) {
23
24
if (stream.match(rx_strong) && stream.match (/\W+|$/, false))
25
return 'strong';
26
if (stream.match(rx_emphasis) && stream.match (/\W+|$/, false))
27
return 'em';
28
if (stream.match(rx_literal) && stream.match (/\W+|$/, false))
29
return 'string-2';
30
if (stream.match(rx_number))
31
return 'number';
32
if (stream.match(rx_positive))
33
return 'positive';
34
if (stream.match(rx_negative))
35
return 'negative';
36
if (stream.match(rx_uri))
37
return 'link';
38
39
while (stream.next() != null) {
40
if (stream.match(rx_strong, false)) break;
41
if (stream.match(rx_emphasis, false)) break;
42
if (stream.match(rx_literal, false)) break;
43
if (stream.match(rx_number, false)) break;
44
if (stream.match(rx_positive, false)) break;
45
if (stream.match(rx_negative, false)) break;
46
if (stream.match(rx_uri, false)) break;
47
}
48
49
return null;
50
}
51
};
52
53
var mode = CodeMirror.getMode(
54
config, options.backdrop || 'rst-base'
55
);
56
57
return CodeMirror.overlayMode(mode, overlay, true); // combine
58
}, 'python', 'stex');
59
60
///////////////////////////////////////////////////////////////////////////////
61
///////////////////////////////////////////////////////////////////////////////
62
63
CodeMirror.defineMode('rst-base', function (config) {
64
65
///////////////////////////////////////////////////////////////////////////
66
///////////////////////////////////////////////////////////////////////////
67
68
function format(string) {
69
var args = Array.prototype.slice.call(arguments, 1);
70
return string.replace(/{(\d+)}/g, function (match, n) {
71
return typeof args[n] != 'undefined' ? args[n] : match;
72
});
73
}
74
75
///////////////////////////////////////////////////////////////////////////
76
///////////////////////////////////////////////////////////////////////////
77
78
var mode_python = CodeMirror.getMode(config, 'python');
79
var mode_stex = CodeMirror.getMode(config, 'stex');
80
81
///////////////////////////////////////////////////////////////////////////
82
///////////////////////////////////////////////////////////////////////////
83
84
var SEPA = "\\s+";
85
var TAIL = "(?:\\s*|\\W|$)",
86
rx_TAIL = new RegExp(format('^{0}', TAIL));
87
88
var NAME =
89
"(?:[^\\W\\d_](?:[\\w!\"#$%&'()\\*\\+,\\-\\.\/:;<=>\\?]*[^\\W_])?)",
90
rx_NAME = new RegExp(format('^{0}', NAME));
91
var NAME_WWS =
92
"(?:[^\\W\\d_](?:[\\w\\s!\"#$%&'()\\*\\+,\\-\\.\/:;<=>\\?]*[^\\W_])?)";
93
var REF_NAME = format('(?:{0}|`{1}`)', NAME, NAME_WWS);
94
95
var TEXT1 = "(?:[^\\s\\|](?:[^\\|]*[^\\s\\|])?)";
96
var TEXT2 = "(?:[^\\`]+)",
97
rx_TEXT2 = new RegExp(format('^{0}', TEXT2));
98
99
var rx_section = new RegExp(
100
"^([!'#$%&\"()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~])\\1{3,}\\s*$");
101
var rx_explicit = new RegExp(
102
format('^\\.\\.{0}', SEPA));
103
var rx_link = new RegExp(
104
format('^_{0}:{1}|^__:{1}', REF_NAME, TAIL));
105
var rx_directive = new RegExp(
106
format('^{0}::{1}', REF_NAME, TAIL));
107
var rx_substitution = new RegExp(
108
format('^\\|{0}\\|{1}{2}::{3}', TEXT1, SEPA, REF_NAME, TAIL));
109
var rx_footnote = new RegExp(
110
format('^\\[(?:\\d+|#{0}?|\\*)]{1}', REF_NAME, TAIL));
111
var rx_citation = new RegExp(
112
format('^\\[{0}\\]{1}', REF_NAME, TAIL));
113
114
var rx_substitution_ref = new RegExp(
115
format('^\\|{0}\\|', TEXT1));
116
var rx_footnote_ref = new RegExp(
117
format('^\\[(?:\\d+|#{0}?|\\*)]_', REF_NAME));
118
var rx_citation_ref = new RegExp(
119
format('^\\[{0}\\]_', REF_NAME));
120
var rx_link_ref1 = new RegExp(
121
format('^{0}__?', REF_NAME));
122
var rx_link_ref2 = new RegExp(
123
format('^`{0}`_', TEXT2));
124
125
var rx_role_pre = new RegExp(
126
format('^:{0}:`{1}`{2}', NAME, TEXT2, TAIL));
127
var rx_role_suf = new RegExp(
128
format('^`{1}`:{0}:{2}', NAME, TEXT2, TAIL));
129
var rx_role = new RegExp(
130
format('^:{0}:{1}', NAME, TAIL));
131
132
var rx_directive_name = new RegExp(format('^{0}', REF_NAME));
133
var rx_directive_tail = new RegExp(format('^::{0}', TAIL));
134
var rx_substitution_text = new RegExp(format('^\\|{0}\\|', TEXT1));
135
var rx_substitution_sepa = new RegExp(format('^{0}', SEPA));
136
var rx_substitution_name = new RegExp(format('^{0}', REF_NAME));
137
var rx_substitution_tail = new RegExp(format('^::{0}', TAIL));
138
var rx_link_head = new RegExp("^_");
139
var rx_link_name = new RegExp(format('^{0}|_', REF_NAME));
140
var rx_link_tail = new RegExp(format('^:{0}', TAIL));
141
142
var rx_verbatim = new RegExp('^::\\s*$');
143
var rx_examples = new RegExp('^\\s+(?:>>>|sage:|In \\[\\d+\\]:)\\s');
144
145
///////////////////////////////////////////////////////////////////////////
146
///////////////////////////////////////////////////////////////////////////
147
148
function to_normal(stream, state) {
149
var token = null;
150
151
if (stream.sol() && stream.match(rx_examples, false)) {
152
change(state, to_mode, {
153
mode: mode_python, local: CodeMirror.startState(mode_python)
154
});
155
} else if (stream.sol() && stream.match(rx_explicit)) {
156
change(state, to_explicit);
157
token = 'meta';
158
} else if (stream.sol() && stream.match(rx_section)) {
159
change(state, to_normal);
160
token = 'header';
161
} else if (phase(state) == rx_role_pre ||
162
stream.match(rx_role_pre, false)) {
163
164
switch (stage(state)) {
165
case 0:
166
change(state, to_normal, context(rx_role_pre, 1));
167
stream.match(/^:/);
168
token = 'meta';
169
break;
170
case 1:
171
change(state, to_normal, context(rx_role_pre, 2));
172
stream.match(rx_NAME);
173
token = 'keyword';
174
175
if (stream.current().match(/^(?:math|latex)/)) {
176
state.tmp_stex = true;
177
}
178
break;
179
case 2:
180
change(state, to_normal, context(rx_role_pre, 3));
181
stream.match(/^:`/);
182
token = 'meta';
183
break;
184
case 3:
185
if (state.tmp_stex) {
186
state.tmp_stex = undefined; state.tmp = {
187
mode: mode_stex, local: CodeMirror.startState(mode_stex)
188
};
189
}
190
191
if (state.tmp) {
192
if (stream.peek() == '`') {
193
change(state, to_normal, context(rx_role_pre, 4));
194
state.tmp = undefined;
195
break;
196
}
197
198
token = state.tmp.mode.token(stream, state.tmp.local);
199
break;
200
}
201
202
change(state, to_normal, context(rx_role_pre, 4));
203
stream.match(rx_TEXT2);
204
token = 'string';
205
break;
206
case 4:
207
change(state, to_normal, context(rx_role_pre, 5));
208
stream.match(/^`/);
209
token = 'meta';
210
break;
211
case 5:
212
change(state, to_normal, context(rx_role_pre, 6));
213
stream.match(rx_TAIL);
214
break;
215
default:
216
change(state, to_normal);
217
}
218
} else if (phase(state) == rx_role_suf ||
219
stream.match(rx_role_suf, false)) {
220
221
switch (stage(state)) {
222
case 0:
223
change(state, to_normal, context(rx_role_suf, 1));
224
stream.match(/^`/);
225
token = 'meta';
226
break;
227
case 1:
228
change(state, to_normal, context(rx_role_suf, 2));
229
stream.match(rx_TEXT2);
230
token = 'string';
231
break;
232
case 2:
233
change(state, to_normal, context(rx_role_suf, 3));
234
stream.match(/^`:/);
235
token = 'meta';
236
break;
237
case 3:
238
change(state, to_normal, context(rx_role_suf, 4));
239
stream.match(rx_NAME);
240
token = 'keyword';
241
break;
242
case 4:
243
change(state, to_normal, context(rx_role_suf, 5));
244
stream.match(/^:/);
245
token = 'meta';
246
break;
247
case 5:
248
change(state, to_normal, context(rx_role_suf, 6));
249
stream.match(rx_TAIL);
250
break;
251
default:
252
change(state, to_normal);
253
}
254
} else if (phase(state) == rx_role || stream.match(rx_role, false)) {
255
256
switch (stage(state)) {
257
case 0:
258
change(state, to_normal, context(rx_role, 1));
259
stream.match(/^:/);
260
token = 'meta';
261
break;
262
case 1:
263
change(state, to_normal, context(rx_role, 2));
264
stream.match(rx_NAME);
265
token = 'keyword';
266
break;
267
case 2:
268
change(state, to_normal, context(rx_role, 3));
269
stream.match(/^:/);
270
token = 'meta';
271
break;
272
case 3:
273
change(state, to_normal, context(rx_role, 4));
274
stream.match(rx_TAIL);
275
break;
276
default:
277
change(state, to_normal);
278
}
279
} else if (phase(state) == rx_substitution_ref ||
280
stream.match(rx_substitution_ref, false)) {
281
282
switch (stage(state)) {
283
case 0:
284
change(state, to_normal, context(rx_substitution_ref, 1));
285
stream.match(rx_substitution_text);
286
token = 'variable-2';
287
break;
288
case 1:
289
change(state, to_normal, context(rx_substitution_ref, 2));
290
if (stream.match(/^_?_?/)) token = 'link';
291
break;
292
default:
293
change(state, to_normal);
294
}
295
} else if (stream.match(rx_footnote_ref)) {
296
change(state, to_normal);
297
token = 'quote';
298
} else if (stream.match(rx_citation_ref)) {
299
change(state, to_normal);
300
token = 'quote';
301
} else if (stream.match(rx_link_ref1)) {
302
change(state, to_normal);
303
if (!stream.peek() || stream.peek().match(/^\W$/)) {
304
token = 'link';
305
}
306
} else if (phase(state) == rx_link_ref2 ||
307
stream.match(rx_link_ref2, false)) {
308
309
switch (stage(state)) {
310
case 0:
311
if (!stream.peek() || stream.peek().match(/^\W$/)) {
312
change(state, to_normal, context(rx_link_ref2, 1));
313
} else {
314
stream.match(rx_link_ref2);
315
}
316
break;
317
case 1:
318
change(state, to_normal, context(rx_link_ref2, 2));
319
stream.match(/^`/);
320
token = 'link';
321
break;
322
case 2:
323
change(state, to_normal, context(rx_link_ref2, 3));
324
stream.match(rx_TEXT2);
325
break;
326
case 3:
327
change(state, to_normal, context(rx_link_ref2, 4));
328
stream.match(/^`_/);
329
token = 'link';
330
break;
331
default:
332
change(state, to_normal);
333
}
334
} else if (stream.match(rx_verbatim)) {
335
change(state, to_verbatim);
336
}
337
338
else {
339
if (stream.next()) change(state, to_normal);
340
}
341
342
return token;
343
}
344
345
///////////////////////////////////////////////////////////////////////////
346
///////////////////////////////////////////////////////////////////////////
347
348
function to_explicit(stream, state) {
349
var token = null;
350
351
if (phase(state) == rx_substitution ||
352
stream.match(rx_substitution, false)) {
353
354
switch (stage(state)) {
355
case 0:
356
change(state, to_explicit, context(rx_substitution, 1));
357
stream.match(rx_substitution_text);
358
token = 'variable-2';
359
break;
360
case 1:
361
change(state, to_explicit, context(rx_substitution, 2));
362
stream.match(rx_substitution_sepa);
363
break;
364
case 2:
365
change(state, to_explicit, context(rx_substitution, 3));
366
stream.match(rx_substitution_name);
367
token = 'keyword';
368
break;
369
case 3:
370
change(state, to_explicit, context(rx_substitution, 4));
371
stream.match(rx_substitution_tail);
372
token = 'meta';
373
break;
374
default:
375
change(state, to_normal);
376
}
377
} else if (phase(state) == rx_directive ||
378
stream.match(rx_directive, false)) {
379
380
switch (stage(state)) {
381
case 0:
382
change(state, to_explicit, context(rx_directive, 1));
383
stream.match(rx_directive_name);
384
token = 'keyword';
385
386
if (stream.current().match(/^(?:math|latex)/))
387
state.tmp_stex = true;
388
else if (stream.current().match(/^python/))
389
state.tmp_py = true;
390
break;
391
case 1:
392
change(state, to_explicit, context(rx_directive, 2));
393
stream.match(rx_directive_tail);
394
token = 'meta';
395
396
if (stream.match(/^latex\s*$/) || state.tmp_stex) {
397
state.tmp_stex = undefined; change(state, to_mode, {
398
mode: mode_stex, local: CodeMirror.startState(mode_stex)
399
});
400
}
401
break;
402
case 2:
403
change(state, to_explicit, context(rx_directive, 3));
404
if (stream.match(/^python\s*$/) || state.tmp_py) {
405
state.tmp_py = undefined; change(state, to_mode, {
406
mode: mode_python, local: CodeMirror.startState(mode_python)
407
});
408
}
409
break;
410
default:
411
change(state, to_normal);
412
}
413
} else if (phase(state) == rx_link || stream.match(rx_link, false)) {
414
415
switch (stage(state)) {
416
case 0:
417
change(state, to_explicit, context(rx_link, 1));
418
stream.match(rx_link_head);
419
stream.match(rx_link_name);
420
token = 'link';
421
break;
422
case 1:
423
change(state, to_explicit, context(rx_link, 2));
424
stream.match(rx_link_tail);
425
token = 'meta';
426
break;
427
default:
428
change(state, to_normal);
429
}
430
} else if (stream.match(rx_footnote)) {
431
change(state, to_normal);
432
token = 'quote';
433
} else if (stream.match(rx_citation)) {
434
change(state, to_normal);
435
token = 'quote';
436
}
437
438
else {
439
stream.eatSpace();
440
if (stream.eol()) {
441
change(state, to_normal);
442
} else {
443
stream.skipToEnd();
444
change(state, to_comment);
445
token = 'comment';
446
}
447
}
448
449
return token;
450
}
451
452
///////////////////////////////////////////////////////////////////////////
453
///////////////////////////////////////////////////////////////////////////
454
455
function to_comment(stream, state) {
456
return as_block(stream, state, 'comment');
457
}
458
459
function to_verbatim(stream, state) {
460
return as_block(stream, state, 'meta');
461
}
462
463
function as_block(stream, state, token) {
464
if (stream.eol() || stream.eatSpace()) {
465
stream.skipToEnd();
466
return token;
467
} else {
468
change(state, to_normal);
469
return null;
470
}
471
}
472
473
///////////////////////////////////////////////////////////////////////////
474
///////////////////////////////////////////////////////////////////////////
475
476
function to_mode(stream, state) {
477
478
if (state.ctx.mode && state.ctx.local) {
479
480
if (stream.sol()) {
481
if (!stream.eatSpace()) change(state, to_normal);
482
return null;
483
}
484
485
return state.ctx.mode.token(stream, state.ctx.local);
486
}
487
488
change(state, to_normal);
489
return null;
490
}
491
492
///////////////////////////////////////////////////////////////////////////
493
///////////////////////////////////////////////////////////////////////////
494
495
function context(phase, stage, mode, local) {
496
return {phase: phase, stage: stage, mode: mode, local: local};
497
}
498
499
function change(state, tok, ctx) {
500
state.tok = tok;
501
state.ctx = ctx || {};
502
}
503
504
function stage(state) {
505
return state.ctx.stage || 0;
506
}
507
508
function phase(state) {
509
return state.ctx.phase;
510
}
511
512
///////////////////////////////////////////////////////////////////////////
513
///////////////////////////////////////////////////////////////////////////
514
515
return {
516
startState: function () {
517
return {tok: to_normal, ctx: context(undefined, 0)};
518
},
519
520
copyState: function (state) {
521
var ctx = state.ctx, tmp = state.tmp;
522
if (ctx.local)
523
ctx = {mode: ctx.mode, local: CodeMirror.copyState(ctx.mode, ctx.local)};
524
if (tmp)
525
tmp = {mode: tmp.mode, local: CodeMirror.copyState(tmp.mode, tmp.local)};
526
return {tok: state.tok, ctx: ctx, tmp: tmp};
527
},
528
529
innerMode: function (state) {
530
return state.tmp ? {state: state.tmp.local, mode: state.tmp.mode}
531
: state.ctx.mode ? {state: state.ctx.local, mode: state.ctx.mode}
532
: null;
533
},
534
535
token: function (stream, state) {
536
return state.tok(stream, state);
537
}
538
};
539
}, 'python', 'stex');
540
541
///////////////////////////////////////////////////////////////////////////////
542
///////////////////////////////////////////////////////////////////////////////
543
544
CodeMirror.defineMIME('text/x-rst', 'rst');
545
546
///////////////////////////////////////////////////////////////////////////////
547
///////////////////////////////////////////////////////////////////////////////
548
549
550