Bug 22584: Add YAML support for Codemirror
[koha-equinox.git] / koha-tmpl / intranet-tmpl / lib / codemirror / yaml.js
1 /* CodeMirror version: 5.40.2 */
2 // CodeMirror, copyright (c) by Marijn Haverbeke and others
3 // Distributed under an MIT license: https://codemirror.net/LICENSE
4
5 (function(mod) {
6   if (typeof exports == "object" && typeof module == "object") // CommonJS
7     mod(require("../../lib/codemirror"));
8   else if (typeof define == "function" && define.amd) // AMD
9     define(["../../lib/codemirror"], mod);
10   else // Plain browser env
11     mod(CodeMirror);
12 })(function(CodeMirror) {
13 "use strict";
14
15 CodeMirror.defineMode("yaml", function() {
16
17   var cons = ['true', 'false', 'on', 'off', 'yes', 'no'];
18   var keywordRegex = new RegExp("\\b(("+cons.join(")|(")+"))$", 'i');
19
20   return {
21     token: function(stream, state) {
22       var ch = stream.peek();
23       var esc = state.escaped;
24       state.escaped = false;
25       /* comments */
26       if (ch == "#" && (stream.pos == 0 || /\s/.test(stream.string.charAt(stream.pos - 1)))) {
27         stream.skipToEnd();
28         return "comment";
29       }
30
31       if (stream.match(/^('([^']|\\.)*'?|"([^"]|\\.)*"?)/))
32         return "string";
33
34       if (state.literal && stream.indentation() > state.keyCol) {
35         stream.skipToEnd(); return "string";
36       } else if (state.literal) { state.literal = false; }
37       if (stream.sol()) {
38         state.keyCol = 0;
39         state.pair = false;
40         state.pairStart = false;
41         /* document start */
42         if(stream.match(/---/)) { return "def"; }
43         /* document end */
44         if (stream.match(/\.\.\./)) { return "def"; }
45         /* array list item */
46         if (stream.match(/\s*-\s+/)) { return 'meta'; }
47       }
48       /* inline pairs/lists */
49       if (stream.match(/^(\{|\}|\[|\])/)) {
50         if (ch == '{')
51           state.inlinePairs++;
52         else if (ch == '}')
53           state.inlinePairs--;
54         else if (ch == '[')
55           state.inlineList++;
56         else
57           state.inlineList--;
58         return 'meta';
59       }
60
61       /* list seperator */
62       if (state.inlineList > 0 && !esc && ch == ',') {
63         stream.next();
64         return 'meta';
65       }
66       /* pairs seperator */
67       if (state.inlinePairs > 0 && !esc && ch == ',') {
68         state.keyCol = 0;
69         state.pair = false;
70         state.pairStart = false;
71         stream.next();
72         return 'meta';
73       }
74
75       /* start of value of a pair */
76       if (state.pairStart) {
77         /* block literals */
78         if (stream.match(/^\s*(\||\>)\s*/)) { state.literal = true; return 'meta'; };
79         /* references */
80         if (stream.match(/^\s*(\&|\*)[a-z0-9\._-]+\b/i)) { return 'variable-2'; }
81         /* numbers */
82         if (state.inlinePairs == 0 && stream.match(/^\s*-?[0-9\.\,]+\s?$/)) { return 'number'; }
83         if (state.inlinePairs > 0 && stream.match(/^\s*-?[0-9\.\,]+\s?(?=(,|}))/)) { return 'number'; }
84         /* keywords */
85         if (stream.match(keywordRegex)) { return 'keyword'; }
86       }
87
88       /* pairs (associative arrays) -> key */
89       if (!state.pair && stream.match(/^\s*(?:[,\[\]{}&*!|>'"%@`][^\s'":]|[^,\[\]{}#&*!|>'"%@`])[^#]*?(?=\s*:($|\s))/)) {
90         state.pair = true;
91         state.keyCol = stream.indentation();
92         return "atom";
93       }
94       if (state.pair && stream.match(/^:\s*/)) { state.pairStart = true; return 'meta'; }
95
96       /* nothing found, continue */
97       state.pairStart = false;
98       state.escaped = (ch == '\\');
99       stream.next();
100       return null;
101     },
102     startState: function() {
103       return {
104         pair: false,
105         pairStart: false,
106         keyCol: 0,
107         inlinePairs: 0,
108         inlineList: 0,
109         literal: false,
110         escaped: false
111       };
112     },
113     lineComment: "#",
114     fold: "indent"
115   };
116 });
117
118 CodeMirror.defineMIME("text/x-yaml", "yaml");
119 CodeMirror.defineMIME("text/yaml", "yaml");
120
121 });