LaravelTest
469 строк · 13.5 Кб
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"));7} else if (typeof define == "function" && define.amd) { // AMD8define(["../../lib/codemirror"], mod);9} else { // Plain browser env10mod(CodeMirror);11}12})(function(CodeMirror) {13"use strict";14
15var TOKEN_STYLES = {16addition: "positive",17attributes: "attribute",18bold: "strong",19cite: "keyword",20code: "atom",21definitionList: "number",22deletion: "negative",23div: "punctuation",24em: "em",25footnote: "variable",26footCite: "qualifier",27header: "header",28html: "comment",29image: "string",30italic: "em",31link: "link",32linkDefinition: "link",33list1: "variable-2",34list2: "variable-3",35list3: "keyword",36notextile: "string-2",37pre: "operator",38p: "property",39quote: "bracket",40span: "quote",41specialChar: "tag",42strong: "strong",43sub: "builtin",44sup: "builtin",45table: "variable-3",46tableHeading: "operator"47};48
49function startNewLine(stream, state) {50state.mode = Modes.newLayout;51state.tableHeading = false;52
53if (state.layoutType === "definitionList" && state.spanningLayout &&54stream.match(RE("definitionListEnd"), false))55state.spanningLayout = false;56}57
58function handlePhraseModifier(stream, state, ch) {59if (ch === "_") {60if (stream.eat("_"))61return togglePhraseModifier(stream, state, "italic", /__/, 2);62else63return togglePhraseModifier(stream, state, "em", /_/, 1);64}65
66if (ch === "*") {67if (stream.eat("*")) {68return togglePhraseModifier(stream, state, "bold", /\*\*/, 2);69}70return togglePhraseModifier(stream, state, "strong", /\*/, 1);71}72
73if (ch === "[") {74if (stream.match(/\d+\]/)) state.footCite = true;75return tokenStyles(state);76}77
78if (ch === "(") {79var spec = stream.match(/^(r|tm|c)\)/);80if (spec)81return tokenStylesWith(state, TOKEN_STYLES.specialChar);82}83
84if (ch === "<" && stream.match(/(\w+)[^>]+>[^<]+<\/\1>/))85return tokenStylesWith(state, TOKEN_STYLES.html);86
87if (ch === "?" && stream.eat("?"))88return togglePhraseModifier(stream, state, "cite", /\?\?/, 2);89
90if (ch === "=" && stream.eat("="))91return togglePhraseModifier(stream, state, "notextile", /==/, 2);92
93if (ch === "-" && !stream.eat("-"))94return togglePhraseModifier(stream, state, "deletion", /-/, 1);95
96if (ch === "+")97return togglePhraseModifier(stream, state, "addition", /\+/, 1);98
99if (ch === "~")100return togglePhraseModifier(stream, state, "sub", /~/, 1);101
102if (ch === "^")103return togglePhraseModifier(stream, state, "sup", /\^/, 1);104
105if (ch === "%")106return togglePhraseModifier(stream, state, "span", /%/, 1);107
108if (ch === "@")109return togglePhraseModifier(stream, state, "code", /@/, 1);110
111if (ch === "!") {112var type = togglePhraseModifier(stream, state, "image", /(?:\([^\)]+\))?!/, 1);113stream.match(/^:\S+/); // optional Url portion114return type;115}116return tokenStyles(state);117}118
119function togglePhraseModifier(stream, state, phraseModifier, closeRE, openSize) {120var charBefore = stream.pos > openSize ? stream.string.charAt(stream.pos - openSize - 1) : null;121var charAfter = stream.peek();122if (state[phraseModifier]) {123if ((!charAfter || /\W/.test(charAfter)) && charBefore && /\S/.test(charBefore)) {124var type = tokenStyles(state);125state[phraseModifier] = false;126return type;127}128} else if ((!charBefore || /\W/.test(charBefore)) && charAfter && /\S/.test(charAfter) &&129stream.match(new RegExp("^.*\\S" + closeRE.source + "(?:\\W|$)"), false)) {130state[phraseModifier] = true;131state.mode = Modes.attributes;132}133return tokenStyles(state);134};135
136function tokenStyles(state) {137var disabled = textileDisabled(state);138if (disabled) return disabled;139
140var styles = [];141if (state.layoutType) styles.push(TOKEN_STYLES[state.layoutType]);142
143styles = styles.concat(activeStyles(144state, "addition", "bold", "cite", "code", "deletion", "em", "footCite",145"image", "italic", "link", "span", "strong", "sub", "sup", "table", "tableHeading"));146
147if (state.layoutType === "header")148styles.push(TOKEN_STYLES.header + "-" + state.header);149
150return styles.length ? styles.join(" ") : null;151}152
153function textileDisabled(state) {154var type = state.layoutType;155
156switch(type) {157case "notextile":158case "code":159case "pre":160return TOKEN_STYLES[type];161default:162if (state.notextile)163return TOKEN_STYLES.notextile + (type ? (" " + TOKEN_STYLES[type]) : "");164return null;165}166}167
168function tokenStylesWith(state, extraStyles) {169var disabled = textileDisabled(state);170if (disabled) return disabled;171
172var type = tokenStyles(state);173if (extraStyles)174return type ? (type + " " + extraStyles) : extraStyles;175else176return type;177}178
179function activeStyles(state) {180var styles = [];181for (var i = 1; i < arguments.length; ++i) {182if (state[arguments[i]])183styles.push(TOKEN_STYLES[arguments[i]]);184}185return styles;186}187
188function blankLine(state) {189var spanningLayout = state.spanningLayout, type = state.layoutType;190
191for (var key in state) if (state.hasOwnProperty(key))192delete state[key];193
194state.mode = Modes.newLayout;195if (spanningLayout) {196state.layoutType = type;197state.spanningLayout = true;198}199}200
201var REs = {202cache: {},203single: {204bc: "bc",205bq: "bq",206definitionList: /- .*?:=+/,207definitionListEnd: /.*=:\s*$/,208div: "div",209drawTable: /\|.*\|/,210foot: /fn\d+/,211header: /h[1-6]/,212html: /\s*<(?:\/)?(\w+)(?:[^>]+)?>(?:[^<]+<\/\1>)?/,213link: /[^"]+":\S/,214linkDefinition: /\[[^\s\]]+\]\S+/,215list: /(?:#+|\*+)/,216notextile: "notextile",217para: "p",218pre: "pre",219table: "table",220tableCellAttributes: /[\/\\]\d+/,221tableHeading: /\|_\./,222tableText: /[^"_\*\[\(\?\+~\^%@|-]+/,223text: /[^!"_=\*\[\(<\?\+~\^%@-]+/224},225attributes: {226align: /(?:<>|<|>|=)/,227selector: /\([^\(][^\)]+\)/,228lang: /\[[^\[\]]+\]/,229pad: /(?:\(+|\)+){1,2}/,230css: /\{[^\}]+\}/231},232createRe: function(name) {233switch (name) {234case "drawTable":235return REs.makeRe("^", REs.single.drawTable, "$");236case "html":237return REs.makeRe("^", REs.single.html, "(?:", REs.single.html, ")*", "$");238case "linkDefinition":239return REs.makeRe("^", REs.single.linkDefinition, "$");240case "listLayout":241return REs.makeRe("^", REs.single.list, RE("allAttributes"), "*\\s+");242case "tableCellAttributes":243return REs.makeRe("^", REs.choiceRe(REs.single.tableCellAttributes,244RE("allAttributes")), "+\\.");245case "type":246return REs.makeRe("^", RE("allTypes"));247case "typeLayout":248return REs.makeRe("^", RE("allTypes"), RE("allAttributes"),249"*\\.\\.?", "(\\s+|$)");250case "attributes":251return REs.makeRe("^", RE("allAttributes"), "+");252
253case "allTypes":254return REs.choiceRe(REs.single.div, REs.single.foot,255REs.single.header, REs.single.bc, REs.single.bq,256REs.single.notextile, REs.single.pre, REs.single.table,257REs.single.para);258
259case "allAttributes":260return REs.choiceRe(REs.attributes.selector, REs.attributes.css,261REs.attributes.lang, REs.attributes.align, REs.attributes.pad);262
263default:264return REs.makeRe("^", REs.single[name]);265}266},267makeRe: function() {268var pattern = "";269for (var i = 0; i < arguments.length; ++i) {270var arg = arguments[i];271pattern += (typeof arg === "string") ? arg : arg.source;272}273return new RegExp(pattern);274},275choiceRe: function() {276var parts = [arguments[0]];277for (var i = 1; i < arguments.length; ++i) {278parts[i * 2 - 1] = "|";279parts[i * 2] = arguments[i];280}281
282parts.unshift("(?:");283parts.push(")");284return REs.makeRe.apply(null, parts);285}286};287
288function RE(name) {289return (REs.cache[name] || (REs.cache[name] = REs.createRe(name)));290}291
292var Modes = {293newLayout: function(stream, state) {294if (stream.match(RE("typeLayout"), false)) {295state.spanningLayout = false;296return (state.mode = Modes.blockType)(stream, state);297}298var newMode;299if (!textileDisabled(state)) {300if (stream.match(RE("listLayout"), false))301newMode = Modes.list;302else if (stream.match(RE("drawTable"), false))303newMode = Modes.table;304else if (stream.match(RE("linkDefinition"), false))305newMode = Modes.linkDefinition;306else if (stream.match(RE("definitionList")))307newMode = Modes.definitionList;308else if (stream.match(RE("html"), false))309newMode = Modes.html;310}311return (state.mode = (newMode || Modes.text))(stream, state);312},313
314blockType: function(stream, state) {315var match, type;316state.layoutType = null;317
318if (match = stream.match(RE("type")))319type = match[0];320else321return (state.mode = Modes.text)(stream, state);322
323if (match = type.match(RE("header"))) {324state.layoutType = "header";325state.header = parseInt(match[0][1]);326} else if (type.match(RE("bq"))) {327state.layoutType = "quote";328} else if (type.match(RE("bc"))) {329state.layoutType = "code";330} else if (type.match(RE("foot"))) {331state.layoutType = "footnote";332} else if (type.match(RE("notextile"))) {333state.layoutType = "notextile";334} else if (type.match(RE("pre"))) {335state.layoutType = "pre";336} else if (type.match(RE("div"))) {337state.layoutType = "div";338} else if (type.match(RE("table"))) {339state.layoutType = "table";340}341
342state.mode = Modes.attributes;343return tokenStyles(state);344},345
346text: function(stream, state) {347if (stream.match(RE("text"))) return tokenStyles(state);348
349var ch = stream.next();350if (ch === '"')351return (state.mode = Modes.link)(stream, state);352return handlePhraseModifier(stream, state, ch);353},354
355attributes: function(stream, state) {356state.mode = Modes.layoutLength;357
358if (stream.match(RE("attributes")))359return tokenStylesWith(state, TOKEN_STYLES.attributes);360else361return tokenStyles(state);362},363
364layoutLength: function(stream, state) {365if (stream.eat(".") && stream.eat("."))366state.spanningLayout = true;367
368state.mode = Modes.text;369return tokenStyles(state);370},371
372list: function(stream, state) {373var match = stream.match(RE("list"));374state.listDepth = match[0].length;375var listMod = (state.listDepth - 1) % 3;376if (!listMod)377state.layoutType = "list1";378else if (listMod === 1)379state.layoutType = "list2";380else381state.layoutType = "list3";382
383state.mode = Modes.attributes;384return tokenStyles(state);385},386
387link: function(stream, state) {388state.mode = Modes.text;389if (stream.match(RE("link"))) {390stream.match(/\S+/);391return tokenStylesWith(state, TOKEN_STYLES.link);392}393return tokenStyles(state);394},395
396linkDefinition: function(stream, state) {397stream.skipToEnd();398return tokenStylesWith(state, TOKEN_STYLES.linkDefinition);399},400
401definitionList: function(stream, state) {402stream.match(RE("definitionList"));403
404state.layoutType = "definitionList";405
406if (stream.match(/\s*$/))407state.spanningLayout = true;408else409state.mode = Modes.attributes;410
411return tokenStyles(state);412},413
414html: function(stream, state) {415stream.skipToEnd();416return tokenStylesWith(state, TOKEN_STYLES.html);417},418
419table: function(stream, state) {420state.layoutType = "table";421return (state.mode = Modes.tableCell)(stream, state);422},423
424tableCell: function(stream, state) {425if (stream.match(RE("tableHeading")))426state.tableHeading = true;427else428stream.eat("|");429
430state.mode = Modes.tableCellAttributes;431return tokenStyles(state);432},433
434tableCellAttributes: function(stream, state) {435state.mode = Modes.tableText;436
437if (stream.match(RE("tableCellAttributes")))438return tokenStylesWith(state, TOKEN_STYLES.attributes);439else440return tokenStyles(state);441},442
443tableText: function(stream, state) {444if (stream.match(RE("tableText")))445return tokenStyles(state);446
447if (stream.peek() === "|") { // end of cell448state.mode = Modes.tableCell;449return tokenStyles(state);450}451return handlePhraseModifier(stream, state, stream.next());452}453};454
455CodeMirror.defineMode("textile", function() {456return {457startState: function() {458return { mode: Modes.newLayout };459},460token: function(stream, state) {461if (stream.sol()) startNewLine(stream, state);462return state.mode(stream, state);463},464blankLine: blankLine465};466});467
468CodeMirror.defineMIME("text/x-textile", "textile");469});470