2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2011-04-28 18:34:19 +00:00
|
|
|
/*@
|
2012-05-31 10:32:54 +00:00
|
|
|
Ox.SyntaxHighlighter <f> Syntax Highlighter
|
|
|
|
([options[, self]]) -> <o:Ox.Element> Syntax Highlighter
|
2011-05-08 20:38:51 +00:00
|
|
|
options <o> Options
|
2012-06-23 18:24:06 +00:00
|
|
|
file <s|''> JavaScript file (alternative to `source` option)
|
2011-05-08 21:06:48 +00:00
|
|
|
lineLength <n|0> If larger than zero, show edge of page
|
2011-05-08 20:38:51 +00:00
|
|
|
offset <n|1> First line number
|
2012-04-06 12:10:21 +00:00
|
|
|
replace <[[]]|[]> Array of replacements
|
|
|
|
Each array element is an array of two arguments to be passed to the
|
|
|
|
replace function, like [str, str], [regexp, str] or [regexp, fn]
|
2011-05-08 21:14:56 +00:00
|
|
|
showLinebreaks <b|false> If true, show linebreaks
|
2011-05-08 20:38:51 +00:00
|
|
|
showLineNumbers <b|false> If true, show line numbers
|
2011-05-08 21:14:56 +00:00
|
|
|
showWhitespace <b|false> If true, show whitespace
|
2011-05-08 21:06:48 +00:00
|
|
|
showTabs <b|false> If true, show tabs
|
2012-05-26 11:11:06 +00:00
|
|
|
source <s|[o]|''> JavaScript source, or array of tokens
|
2011-05-08 20:38:51 +00:00
|
|
|
stripComments <b|false> If true, strip comments
|
2011-05-08 21:06:48 +00:00
|
|
|
tabSize <n|4> Number of spaces per tab
|
2011-05-08 21:14:56 +00:00
|
|
|
self <o> Shared private variable
|
2011-04-28 18:34:19 +00:00
|
|
|
@*/
|
|
|
|
|
|
|
|
Ox.SyntaxHighlighter = function(options, self) {
|
|
|
|
|
2011-11-03 15:42:41 +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({
|
2012-06-23 18:24:06 +00:00
|
|
|
file: '',
|
2011-05-08 21:06:48 +00:00
|
|
|
lineLength: 0,
|
|
|
|
offset: 1,
|
2012-04-06 12:10:21 +00:00
|
|
|
replace: [],
|
2011-05-08 21:14:56 +00:00
|
|
|
showLinebreaks: false,
|
2011-05-08 18:22:43 +00:00
|
|
|
showLineNumbers: false,
|
2011-05-08 21:06:48 +00:00
|
|
|
showTabs: false,
|
2011-05-08 21:14:56 +00:00
|
|
|
showWhitespace: false,
|
2011-05-08 21:06:48 +00:00
|
|
|
source: '',
|
|
|
|
stripComments: false,
|
2012-05-26 15:48:19 +00:00
|
|
|
tabSize: 4
|
2011-04-28 18:34:19 +00:00
|
|
|
})
|
|
|
|
.options(options || {})
|
2012-05-28 16:21:00 +00:00
|
|
|
.update(renderSource)
|
2011-04-28 18:34:19 +00:00
|
|
|
.addClass('OxSyntaxHighlighter');
|
|
|
|
|
2012-06-23 18:24:06 +00:00
|
|
|
if (self.options.file) {
|
|
|
|
Ox.get(self.options.file, function(source) {
|
|
|
|
self.options.source = source;
|
|
|
|
renderSource();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
renderSource();
|
|
|
|
}
|
2011-04-28 18:34:19 +00:00
|
|
|
|
2011-05-08 18:22:43 +00:00
|
|
|
function renderSource() {
|
2011-05-08 21:06:48 +00:00
|
|
|
var $lineNumbers, $line, $source, width,
|
|
|
|
lines, source = '', tokens,
|
2011-05-08 20:38:51 +00:00
|
|
|
linebreak = (
|
2011-11-03 15:42:41 +00:00
|
|
|
self.options.showLinebreaks
|
|
|
|
? '<span class="OxLinebreak">\u21A9</span>' : ''
|
2011-05-08 20:38:51 +00:00
|
|
|
) + '<br/>',
|
|
|
|
tab = (
|
|
|
|
self.options.showTabs ?
|
|
|
|
'<span class="OxTab">\u2192</span>' : ''
|
2011-05-08 21:06:48 +00:00
|
|
|
) + Ox.repeat(' ', self.options.tabSize - self.options.showTabs),
|
2011-05-08 21:14:56 +00:00
|
|
|
whitespace = self.options.showWhitespace ? '\u00B7' : ' ';
|
2012-05-26 11:11:06 +00:00
|
|
|
tokens = Ox.isArray(self.options.source)
|
|
|
|
? self.options.source
|
|
|
|
: Ox.tokenize(self.options.source);
|
2011-05-08 20:38:51 +00:00
|
|
|
tokens.forEach(function(token, i) {
|
2011-05-08 18:22:43 +00:00
|
|
|
var classNames,
|
2012-05-26 10:55:54 +00:00
|
|
|
type = token.type == 'identifier'
|
|
|
|
? Ox.identify(token.value) : token.type;
|
2011-05-08 18:22:43 +00:00
|
|
|
if (
|
2012-05-26 10:55:54 +00:00
|
|
|
!(self.options.stripComments && type == 'comment')
|
2011-05-08 18:22:43 +00:00
|
|
|
) {
|
2012-05-26 10:55:54 +00:00
|
|
|
classNames = 'Ox' + Ox.toTitleCase(type);
|
|
|
|
if (self.options.showWhitespace && type == 'whitespace') {
|
2011-05-08 18:22:43 +00:00
|
|
|
if (isAfterLinebreak() && hasIrregularSpaces()) {
|
2011-06-19 17:48:32 +00:00
|
|
|
classNames += ' OxLeading';
|
2011-05-08 18:22:43 +00:00
|
|
|
} else if (isBeforeLinebreak()) {
|
2011-06-19 17:48:32 +00:00
|
|
|
classNames += ' OxTrailing';
|
2011-05-08 18:22:43 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-08 20:38:51 +00:00
|
|
|
source += '<span class="' + classNames + '">' +
|
2012-05-27 10:40:02 +00:00
|
|
|
Ox.encodeHTMLEntities(token.value)
|
2011-05-08 20:38:51 +00:00
|
|
|
.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() {
|
2012-05-26 10:55:54 +00:00
|
|
|
return token.value.split('').reduce(function(prev, curr) {
|
2011-05-08 18:22:43 +00:00
|
|
|
return prev + (curr == ' ' ? 1 : 0);
|
2011-05-08 21:06:48 +00:00
|
|
|
}, 0) % self.options.tabSize;
|
2011-05-08 18:22:43 +00:00
|
|
|
}
|
|
|
|
});
|
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
|
|
|
if (self.options.showLineNumbers) {
|
2011-06-19 17:48:32 +00:00
|
|
|
$lineNumbers = Ox.Element()
|
2011-05-08 20:38:51 +00:00
|
|
|
.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
|
|
|
}
|
2012-04-06 12:10:21 +00:00
|
|
|
|
|
|
|
self.options.replace.forEach(function(replace) {
|
|
|
|
source = source.replace(replace[0], replace[1])
|
|
|
|
});
|
|
|
|
|
2011-06-19 17:48:32 +00:00
|
|
|
$source = Ox.Element()
|
2011-05-08 18:22:43 +00:00
|
|
|
.addClass('OxSourceCode')
|
2011-05-08 20:38:51 +00:00
|
|
|
.html(source)
|
2011-05-08 18:22:43 +00:00
|
|
|
.appendTo(that);
|
2011-05-08 21:06:48 +00:00
|
|
|
if (self.options.lineLength) {
|
2011-06-19 17:48:32 +00:00
|
|
|
$line = Ox.Element()
|
2011-05-08 21:06:48 +00:00
|
|
|
.css({
|
|
|
|
position: 'absolute',
|
|
|
|
top: '-1000px'
|
|
|
|
})
|
|
|
|
.html(Ox.repeat(' ', self.options.lineLength))
|
2011-06-19 17:48:32 +00:00
|
|
|
.appendTo(that);
|
|
|
|
width = $line.width() + 4; // add padding
|
2011-11-04 22:14:30 +00:00
|
|
|
$line.remove();
|
2011-05-08 21:06:48 +00:00
|
|
|
['moz', 'webkit'].forEach(function(browser) {
|
|
|
|
$source.css({
|
|
|
|
background: '-' + browser +
|
|
|
|
'-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-04-28 18:34:19 +00:00
|
|
|
return that;
|
|
|
|
|
2011-06-19 17:48:32 +00:00
|
|
|
};
|