Created
February 23, 2012 17:09
-
-
Save amasad/1893780 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var runMode = function (code, language, elem) { | |
var $elem = $(elem), | |
// MODES is a hash containing all ace modes we support. | |
mode = MODES[lang.toLowerCase()].mode, | |
lines = code.split('\n'), | |
// Tokenizer initial state. | |
last = 'start', | |
// Tokens to be rendered as text nodes. | |
textToken = { | |
"text": true, | |
"rparen": true, | |
"lparen": true | |
}; | |
// Add the theme class for the parent element. | |
// We use textmate theme hence "tm". | |
$elem.addClass('ace-tm').empty(); | |
lines.forEach(function (l, i) { | |
var tokenizedLine = mode.getTokenizer().getLineTokens(l, last), | |
tokens = tokenizedLine.tokens; | |
// Update tokenizer state. | |
last = tokenizedLine.state; | |
// Process line. | |
var line = tokens.map(function (token) { | |
if (!textToken[token.type]) { | |
var classes = 'ace_' + token.type.replace(/\./g, ' ace_'); | |
return '<span class="' + classes + '">' + token.value + '</span>'; | |
} else { | |
return token.value; | |
} | |
}).join(''); | |
$elem.append($('<span class="ace_line" />').html(line)); | |
// Add line break after each line except for the last one. | |
if (i !== lines.length - 1){ | |
$elem.append('<br>'); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment