use new-style tokens

This commit is contained in:
rolux 2012-05-26 12:55:54 +02:00
parent 6c98a82800
commit ba4588f3fa

View file

@ -58,12 +58,13 @@ Ox.SyntaxHighlighter = function(options, self) {
tokens = Ox.tokenize(self.options.source); tokens = Ox.tokenize(self.options.source);
tokens.forEach(function(token, i) { tokens.forEach(function(token, i) {
var classNames, var classNames,
substr = self.options.source.substr(token.offset, token.length); type = token.type == 'identifier'
? Ox.identify(token.value) : token.type;
if ( if (
!(self.options.stripComments && token.type == 'comment') !(self.options.stripComments && type == 'comment')
) { ) {
classNames = 'Ox' + Ox.toTitleCase(token.type); classNames = 'Ox' + Ox.toTitleCase(type);
if (self.options.showWhitespace && token.type == 'whitespace') { if (self.options.showWhitespace && type == 'whitespace') {
if (isAfterLinebreak() && hasIrregularSpaces()) { if (isAfterLinebreak() && hasIrregularSpaces()) {
classNames += ' OxLeading'; classNames += ' OxLeading';
} else if (isBeforeLinebreak()) { } else if (isBeforeLinebreak()) {
@ -71,7 +72,7 @@ Ox.SyntaxHighlighter = function(options, self) {
} }
} }
source += '<span class="' + classNames + '">' + source += '<span class="' + classNames + '">' +
Ox.encodeHTML(substr) Ox.encodeHTML(token.value)
.replace(/ /g, whitespace) .replace(/ /g, whitespace)
.replace(/\t/g, tab) .replace(/\t/g, tab)
.replace(/\n/g, linebreak) + '</span>'; .replace(/\n/g, linebreak) + '</span>';
@ -85,7 +86,7 @@ Ox.SyntaxHighlighter = function(options, self) {
tokens[i + 1].type == 'linebreak'; tokens[i + 1].type == 'linebreak';
} }
function hasIrregularSpaces() { function hasIrregularSpaces() {
return substr.split('').reduce(function(prev, curr) { return token.value.split('').reduce(function(prev, curr) {
return prev + (curr == ' ' ? 1 : 0); return prev + (curr == ' ' ? 1 : 0);
}, 0) % self.options.tabSize; }, 0) % self.options.tabSize;
} }