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