2011-04-28 18:34:19 +00:00
|
|
|
// vim: et:ts=4:sw=4:sts=4:ft=js
|
|
|
|
|
|
|
|
/*@
|
2011-05-08 20:38:51 +00:00
|
|
|
Ox.SyntaxHighlighter <function> Syntax Highlighter
|
|
|
|
(options[, self]) -> <o> Syntax Highlighter
|
|
|
|
options <o> Options
|
|
|
|
offset <n|1> First line number
|
|
|
|
showLineNumbers <b|false> If true, show line numbers
|
|
|
|
showLinebreaks <b|false> If true, show linebreaks
|
|
|
|
showWhitespace <b|false> If true, show whitespace
|
|
|
|
stripComments <b|false> If true, strip comments
|
|
|
|
self <o> Shared private
|
2011-04-28 18:34:19 +00:00
|
|
|
@*/
|
|
|
|
|
|
|
|
Ox.SyntaxHighlighter = function(options, self) {
|
|
|
|
|
2011-05-08 20:38:51 +00:00
|
|
|
self = self || {};
|
2011-05-08 18:22:43 +00:00
|
|
|
var that = Ox.Element({}, self)
|
2011-04-28 18:34:19 +00:00
|
|
|
.defaults({
|
|
|
|
height: 40,
|
|
|
|
lineLength: 80, //@ number of characters per line
|
2011-05-07 21:07:53 +00:00
|
|
|
offset: 1, //@ first line number
|
2011-05-08 18:22:43 +00:00
|
|
|
showLineNumbers: false,
|
2011-04-28 18:34:19 +00:00
|
|
|
showLinebreaks: false, //@ show linebreak symbols
|
|
|
|
showTabs: false, //@ show tab symbols
|
|
|
|
showWhitespace: false, //@ show irregular leading or trailing whitespace
|
|
|
|
source: '', //@ JavaScript source
|
|
|
|
stripComments: false, //@ strip all comments
|
|
|
|
stripLinebreaks: false, //@ strip multiple linebreaks, NOT IMPLEMENTED
|
|
|
|
stripWhitespace: false, //@ strip all whitespace, NOT IMPLEMENTED
|
|
|
|
tabLength: 4, //@ number of spaces per tab
|
|
|
|
width: 80
|
|
|
|
})
|
|
|
|
.options(options || {})
|
|
|
|
.addClass('OxSyntaxHighlighter');
|
|
|
|
|
2011-05-08 18:22:43 +00:00
|
|
|
renderSource();
|
2011-04-28 18:34:19 +00:00
|
|
|
|
2011-05-08 18:22:43 +00:00
|
|
|
function renderSource() {
|
2011-05-08 20:38:51 +00:00
|
|
|
var lines, source = '', tokens,
|
|
|
|
linebreak = (
|
|
|
|
self.options.showLinebreaks ?
|
|
|
|
'<span class="OxLinebreak">\u21A9</span>' : ''
|
|
|
|
) + '<br/>',
|
|
|
|
tab = (
|
|
|
|
self.options.showTabs ?
|
|
|
|
'<span class="OxTab">\u2192</span>' : ''
|
|
|
|
) + Ox.repeat(' ', self.options.tabLength - self.options.showTabs),
|
|
|
|
whitespace = self.options.showWhitespace ? '\u00B7' : ' ';
|
2011-05-08 18:22:43 +00:00
|
|
|
self.options.source = self.options.source
|
|
|
|
.replace(/\r\n/g, '\n')
|
|
|
|
.replace(/\r/g, '\n');
|
2011-05-08 20:38:51 +00:00
|
|
|
tokens = Ox.tokenize(self.options.source);
|
|
|
|
tokens.forEach(function(token, i) {
|
2011-05-08 18:22:43 +00:00
|
|
|
var classNames,
|
2011-05-08 20:38:51 +00:00
|
|
|
substr = self.options.source.substr(token.offset, token.length);
|
2011-05-08 18:22:43 +00:00
|
|
|
if (
|
|
|
|
!(self.options.stripComments && token.type == 'comment')
|
|
|
|
) {
|
|
|
|
classNames = 'Ox' + Ox.toTitleCase(token.type);
|
|
|
|
if (self.options.showWhitespace && token.type == 'whitespace') {
|
|
|
|
if (isAfterLinebreak() && hasIrregularSpaces()) {
|
|
|
|
classNames += ' OxLeading'
|
|
|
|
} else if (isBeforeLinebreak()) {
|
|
|
|
classNames += ' OxTrailing'
|
|
|
|
}
|
|
|
|
}
|
2011-05-08 20:38:51 +00:00
|
|
|
source += '<span class="' + classNames + '">' +
|
|
|
|
Ox.encodeHTML(substr)
|
|
|
|
.replace(/ /g, whitespace)
|
|
|
|
.replace(/\t/g, tab)
|
|
|
|
.replace(/\n/g, linebreak) + '</span>';
|
2011-05-08 18:22:43 +00:00
|
|
|
}
|
|
|
|
function isAfterLinebreak() {
|
|
|
|
return i == 0 ||
|
2011-05-08 20:38:51 +00:00
|
|
|
tokens[i - 1].type == 'linebreak';
|
2011-05-08 18:22:43 +00:00
|
|
|
}
|
|
|
|
function isBeforeLinebreak() {
|
2011-05-08 20:38:51 +00:00
|
|
|
return i == tokens.length - 1 ||
|
|
|
|
tokens[i + 1].type == 'linebreak';
|
2011-05-08 18:22:43 +00:00
|
|
|
}
|
|
|
|
function hasIrregularSpaces() {
|
2011-05-08 20:38:51 +00:00
|
|
|
return substr.split('').reduce(function(prev, curr) {
|
2011-05-08 18:22:43 +00:00
|
|
|
return prev + (curr == ' ' ? 1 : 0);
|
|
|
|
}, 0) % self.options.tabLength;
|
|
|
|
}
|
|
|
|
});
|
2011-05-08 20:38:51 +00:00
|
|
|
lines = source.split('<br/>');
|
2011-05-08 18:22:43 +00:00
|
|
|
that.empty();
|
2011-05-08 20:38:51 +00:00
|
|
|
///*
|
|
|
|
var $test = new Ox.Element()
|
2011-05-08 18:22:43 +00:00
|
|
|
.css({
|
2011-05-08 20:38:51 +00:00
|
|
|
position: 'absolute',
|
|
|
|
top: '-1000px'
|
2011-05-08 18:22:43 +00:00
|
|
|
})
|
2011-05-08 20:38:51 +00:00
|
|
|
.html(Ox.repeat(' ', self.options.lineLength))
|
2011-05-08 18:22:43 +00:00
|
|
|
.appendTo(that);
|
2011-05-08 20:38:51 +00:00
|
|
|
var width = $test.width() + 4; // add padding
|
|
|
|
$test.removeElement();
|
|
|
|
//*/
|
|
|
|
if (self.options.showLineNumbers) {
|
|
|
|
self.$lineNumbers = new Ox.Element()
|
|
|
|
.addClass('OxLineNumbers')
|
|
|
|
.html(
|
|
|
|
Ox.range(lines.length).map(function(line) {
|
|
|
|
return (line + self.options.offset);
|
|
|
|
}).join('<br/>')
|
|
|
|
)
|
|
|
|
.appendTo(that);
|
2011-05-08 18:22:43 +00:00
|
|
|
}
|
|
|
|
self.$source = new Ox.Element()
|
|
|
|
.addClass('OxSourceCode')
|
|
|
|
.css({
|
2011-05-08 20:38:51 +00:00
|
|
|
background: '-moz-linear-gradient(left, rgb(255, 255, 255), rgb(255, 255, 255) ' +
|
|
|
|
width + 'px, rgb(248, 248, 248) ' + width + 'px, rgb(248, 248, 248))'
|
|
|
|
})
|
|
|
|
.css({
|
|
|
|
background: '-webkit-linear-gradient(left, rgb(255, 255, 255) ' +
|
|
|
|
width + 'px, rgb(192, 192, 192) ' + width + 'px, rgb(255, 255, 255) ' +
|
|
|
|
(width + 1) + 'px)'
|
2011-05-08 18:22:43 +00:00
|
|
|
})
|
2011-05-08 20:38:51 +00:00
|
|
|
.html(source)
|
2011-05-08 18:22:43 +00:00
|
|
|
.appendTo(that);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.setOption = function(key, value) {
|
|
|
|
renderSource();
|
2011-04-28 18:34:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return that;
|
|
|
|
|
|
|
|
};
|