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