5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"), require("../htmlmixed/htmlmixed"), require("../ruby/ruby"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror", "../htmlmixed/htmlmixed", "../ruby/ruby"], mod);
11
})(function(CodeMirror) {
15
CodeMirror.defineMode("haml", function(config) {
16
var htmlMode = CodeMirror.getMode(config, {name: "htmlmixed"});
17
var rubyMode = CodeMirror.getMode(config, "ruby");
19
function rubyInQuote(endQuote) {
20
return function(stream, state) {
21
var ch = stream.peek();
22
if (ch == endQuote && state.rubyState.tokenize.length == 1) {
25
state.tokenize = html;
26
return "closeAttributeTag";
28
return ruby(stream, state);
33
function ruby(stream, state) {
34
if (stream.match("-#")) {
38
return rubyMode.token(stream, state.rubyState);
41
function html(stream, state) {
42
var ch = stream.peek();
46
if (state.previousToken.style == "comment" ) {
47
if (state.indented > state.previousToken.indented) {
53
if (state.startOfLine) {
54
if (ch == "!" && stream.match("!!")) {
57
} else if (stream.match(/^%[\w:#\.]+=/)) {
58
state.tokenize = ruby;
60
} else if (stream.match(/^%[\w:]+/)) {
62
} else if (ch == "/" ) {
68
if (state.startOfLine || state.previousToken.style == "hamlTag") {
69
if ( ch == "#" || ch == ".") {
70
stream.match(/[\w-#\.]*/);
71
return "hamlAttribute";
76
if (state.startOfLine && !stream.match("-->", false) && (ch == "=" || ch == "-" )) {
77
state.tokenize = ruby;
78
return state.tokenize(stream, state);
81
if (state.previousToken.style == "hamlTag" ||
82
state.previousToken.style == "closeAttributeTag" ||
83
state.previousToken.style == "hamlAttribute") {
85
state.tokenize = rubyInQuote(")");
86
return state.tokenize(stream, state);
87
} else if (ch == "{") {
88
if (!stream.match(/^\{%.*/)) {
89
state.tokenize = rubyInQuote("}");
90
return state.tokenize(stream, state);
95
return htmlMode.token(stream, state.htmlState);
100
startState: function() {
101
var htmlState = CodeMirror.startState(htmlMode);
102
var rubyState = CodeMirror.startState(rubyMode);
104
htmlState: htmlState,
105
rubyState: rubyState,
107
previousToken: { style: null, indented: 0},
112
copyState: function(state) {
114
htmlState : CodeMirror.copyState(htmlMode, state.htmlState),
115
rubyState: CodeMirror.copyState(rubyMode, state.rubyState),
116
indented: state.indented,
117
previousToken: state.previousToken,
118
tokenize: state.tokenize
122
token: function(stream, state) {
124
state.indented = stream.indentation();
125
state.startOfLine = true;
127
if (stream.eatSpace()) return null;
128
var style = state.tokenize(stream, state);
129
state.startOfLine = false;
132
if (style && style != "commentLine") {
133
state.previousToken = { style: style, indented: state.indented };
137
if (stream.eol() && state.tokenize == ruby) {
139
var ch = stream.peek();
141
if (ch && ch != ",") {
142
state.tokenize = html;
146
if (style == "hamlTag") {
148
} else if (style == "commentLine") {
150
} else if (style == "hamlAttribute") {
152
} else if (style == "closeAttributeTag") {
158
}, "htmlmixed", "ruby");
160
CodeMirror.defineMIME("text/x-haml", "haml");