Skip to content

Commit f907370

Browse files
Necrathexmarijnh
authored andcommitted
Added YAML mode
1 parent 6840d5d commit f907370

File tree

4 files changed

+166
-1
lines changed

4 files changed

+166
-1
lines changed

compress.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ <h1><span class="logo-braces">{ }</span> <a href="http://codemirror.net/">CodeMi
5757
<option value="http://codemirror.net/mode/plsql/plsql.js">plsql.js</option>
5858
<option value="http://codemirror.net/mode/lua/lua.js">lua.js</option>
5959
<option value="http://codemirror.net/mode/scheme/scheme.js">scheme.js</option>
60+
<option value="http://codemirror.net/mode/yaml/yaml.js">scheme.js</option>
6061
</optgroup>
6162
</select></p>
6263

index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ <h2 style="margin-top: 0">Supported modes:</h2>
4646
<li><a href="mode/stex/index.html">sTeX, LaTeX</a></li>
4747
<li><a href="mode/haskell/index.html">Haskell</a></li>
4848
<li><a href="mode/smalltalk/index.html">Smalltalk</a></li>
49-
<li><a href="mode/rst/index.html">reStructuredText</a></li>
5049
<li><a href="mode/plsql/index.html">PL/SQL</a></li>
5150
<li><a href="mode/lua/index.html">Lua</a></li>
5251
<li><a href="mode/scheme/index.html">Scheme</a></li>
52+
<li><a href="mode/rst/index.html">reStructuredText</a></li>
53+
<li><a href="mode/yaml/index.html">YAML</a></li>
5354
</ul>
5455

5556
</div><div class="left2 blk">

mode/yaml/index.html

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>CodeMirror 2: YAML mode</title>
5+
<link rel="stylesheet" href="../../lib/codemirror.css">
6+
<script src="../../lib/codemirror.js"></script>
7+
<script src="yaml.js"></script>
8+
<link rel="stylesheet" href="../../theme/default.css">
9+
<style>.CodeMirror { border-top: 1px solid #ddd; border-bottom: 1px solid #ddd; }</style>
10+
<link rel="stylesheet" href="../../css/docs.css">
11+
</head>
12+
<body>
13+
<h1>CodeMirror 2: YAML mode</h1>
14+
<form><textarea id="code" name="code">
15+
--- # Favorite movies
16+
- Casablanca
17+
- North by Northwest
18+
- The Man Who Wasn't There
19+
--- # Shopping list
20+
[milk, pumpkin pie, eggs, juice]
21+
--- # Indented Blocks, common in YAML data files, use indentation and new lines to separate the key: value pairs
22+
name: John Smith
23+
age: 33
24+
--- # Inline Blocks, common in YAML data streams, use commas to separate the key: value pairs between braces
25+
{name: John Smith, age: 33}
26+
---
27+
receipt: Oz-Ware Purchase Invoice
28+
date: 2007-08-06
29+
customer:
30+
given: Dorothy
31+
family: Gale
32+
33+
items:
34+
- part_no: A4786
35+
descrip: Water Bucket (Filled)
36+
price: 1.47
37+
quantity: 4
38+
39+
- part_no: E1628
40+
descrip: High Heeled "Ruby" Slippers
41+
size: 8
42+
price: 100.27
43+
quantity: 1
44+
45+
bill-to: &id001
46+
street: |
47+
123 Tornado Alley
48+
Suite 16
49+
city: East Centerville
50+
state: KS
51+
52+
ship-to: *id001
53+
54+
specialDelivery: >
55+
Follow the Yellow Brick
56+
Road to the Emerald City.
57+
Pay no attention to the
58+
man behind the curtain.
59+
...
60+
</textarea></form>
61+
<script>
62+
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {});
63+
</script>
64+
65+
<p><strong>MIME types defined:</strong> <code>text/x-yaml</code>.</p>
66+
67+
</body>
68+
</html>

mode/yaml/yaml.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
CodeMirror.defineMode("yaml", function() {
2+
3+
var cons = ['true', 'false', 'on', 'off', 'yes', 'no'];
4+
var keywordRegex = new RegExp("\\b(("+cons.join(")|(")+"))$", 'i');
5+
6+
return {
7+
token: function(stream, state) {
8+
var ch = stream.peek();
9+
var esc = state.escaped;
10+
state.escaped = false;
11+
/* comments */
12+
if (ch == "#") { stream.skipToEnd(); return "comment"; }
13+
if (state.literal && stream.indentation() > state.keyCol) {
14+
stream.skipToEnd(); return "string";
15+
} else if (state.literal) { state.literal = false; }
16+
if (stream.sol()) {
17+
state.keyCol = 0;
18+
state.pair = false;
19+
state.pairStart = false;
20+
/* document start */
21+
if(stream.match(/---/)) { return "def"; }
22+
/* document end */
23+
if (stream.match(/\.\.\./)) { return "def"; }
24+
/* array list item */
25+
if (stream.match(/\s*-\s+/)) { return 'meta'; }
26+
}
27+
/* pairs (associative arrays) -> key */
28+
if (!state.pair && stream.match(/^\s*([a-z0-9\._-])+(?=\s*:)/i)) {
29+
state.pair = true;
30+
state.keyCol = stream.indentation();
31+
return "atom";
32+
}
33+
if (state.pair && stream.match(/^:\s*/)) { state.pairStart = true; return 'meta'; }
34+
35+
/* inline pairs/lists */
36+
if (stream.match(/^(\{|\}|\[|\])/)) {
37+
if (ch == '{')
38+
state.inlinePairs++;
39+
else if (ch == '}')
40+
state.inlinePairs--;
41+
else if (ch == '[')
42+
state.inlineList++;
43+
else
44+
state.inlineList--;
45+
return 'meta';
46+
}
47+
48+
/* list seperator */
49+
if (state.inlineList > 0 && !esc && ch == ',') {
50+
stream.next();
51+
return 'meta';
52+
}
53+
/* pairs seperator */
54+
if (state.inlinePairs > 0 && !esc && ch == ',') {
55+
state.keyCol = 0;
56+
state.pair = false;
57+
state.pairStart = false;
58+
stream.next();
59+
return 'meta';
60+
}
61+
62+
/* start of value of a pair */
63+
if (state.pairStart) {
64+
/* block literals */
65+
if (stream.match(/^\s*(\||\>)\s*/)) { state.literal = true; return 'meta'; };
66+
/* references */
67+
if (stream.match(/^\s*(\&|\*)[a-z0-9\._-]+\b/i)) { return 'variable-2'; }
68+
/* numbers */
69+
if (state.inlinePairs == 0 && stream.match(/^\s*-?[0-9\.\,]+\s?$/)) { return 'number'; }
70+
if (state.inlinePairs > 0 && stream.match(/^\s*-?[0-9\.\,]+\s?(?=(,|}))/)) { return 'number'; }
71+
/* keywords */
72+
if (stream.match(keywordRegex)) { return 'keyword'; }
73+
}
74+
75+
/* nothing found, continue */
76+
state.pairStart = false;
77+
state.escaped = (ch == '\\');
78+
stream.next();
79+
return null;
80+
},
81+
startState: function() {
82+
return {
83+
pair: false,
84+
pairStart: false,
85+
keyCol: 0,
86+
inlinePairs: 0,
87+
inlineList: 0,
88+
literal: false,
89+
escaped: false
90+
};
91+
}
92+
};
93+
});
94+
95+
CodeMirror.defineMIME("text/x-yaml", "yaml");

0 commit comments

Comments
 (0)