123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558 |
- // CodeMirror, copyright (c) by Marijn Haverbeke and others
- // Distributed under an MIT license: https://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');
- ///////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////
- });
|