2012-04-04 14:59:58 +00:00
|
|
|
'use strict';
|
|
|
|
|
2012-05-21 10:38:18 +00:00
|
|
|
/*@
|
2012-05-22 13:14:40 +00:00
|
|
|
Ox.SourceViewer <f:Ox.Container> Source Viewer
|
2012-05-21 10:38:18 +00:00
|
|
|
(options[, self]) -> <o> Source Viewer
|
|
|
|
options <o> Options
|
|
|
|
self <o> Shared private variable
|
|
|
|
@*/
|
|
|
|
|
2012-04-04 14:59:58 +00:00
|
|
|
Ox.SourceViewer = function(options, self) {
|
|
|
|
|
|
|
|
self = self || {};
|
|
|
|
var that = Ox.Container({}, self)
|
|
|
|
.defaults({
|
|
|
|
file: '',
|
2012-04-06 12:10:21 +00:00
|
|
|
replaceCode: [],
|
|
|
|
replaceComment: [],
|
2012-04-04 14:59:58 +00:00
|
|
|
})
|
|
|
|
.options(options)
|
|
|
|
.addClass('OxSourceViewer');
|
|
|
|
|
2012-04-06 12:10:21 +00:00
|
|
|
self.options.replaceComment.unshift(
|
|
|
|
// removes indentation inside <pre> tags
|
|
|
|
[
|
2012-04-04 14:59:58 +00:00
|
|
|
/<pre>([\s\S]+)<\/pre>/g,
|
|
|
|
function(pre, text) {
|
|
|
|
var lines = trim(text).split('\n'),
|
|
|
|
indent = Ox.min(lines.map(function(line) {
|
|
|
|
var match = line.match(/^\s+/);
|
|
|
|
return match ? match[0].length : 0;
|
|
|
|
}));
|
|
|
|
return '<pre>' + lines.map(function(line) {
|
2012-05-24 09:47:33 +00:00
|
|
|
return line.slice(indent);
|
2012-04-04 14:59:58 +00:00
|
|
|
}).join('\n') + '</pre>';
|
|
|
|
}
|
2012-04-06 12:10:21 +00:00
|
|
|
]
|
2012-04-04 14:59:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
self.$table = $('<table>').appendTo(that.$content);
|
|
|
|
|
|
|
|
Ox.get(self.options.file, function(source) {
|
|
|
|
var sections = [{comment: '', code: ''}];
|
|
|
|
Ox.tokenize(source).forEach(function(token, i) {
|
|
|
|
var text = source.substr(token.offset, token.length),
|
|
|
|
type = token.type == 'comment' ? 'comment' : 'code';
|
2012-04-14 12:03:35 +00:00
|
|
|
if (!/^\/\//.test(text)) {
|
|
|
|
if (type == 'comment') {
|
|
|
|
i && sections.push({comment: '', code: ''});
|
2012-05-24 09:02:59 +00:00
|
|
|
text = text.slice(2, -2);
|
2012-04-14 12:03:35 +00:00
|
|
|
self.options.replaceComment.forEach(function(replace) {
|
|
|
|
text = text.replace(replace[0], replace[1]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Ox.last(sections)[type] += text;
|
2012-04-04 14:59:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
sections.forEach(function(section) {
|
2012-04-06 12:10:21 +00:00
|
|
|
var $section = $('<tr>')
|
|
|
|
.appendTo(self.$table),
|
2012-04-04 14:59:58 +00:00
|
|
|
$comment = $('<td>')
|
2012-04-06 12:10:21 +00:00
|
|
|
.addClass('OxComment OxSerif')
|
|
|
|
.html(trim(section.comment))
|
|
|
|
.appendTo($section),
|
2012-04-04 14:59:58 +00:00
|
|
|
$code = $('<td>')
|
|
|
|
.addClass('OxCode')
|
|
|
|
.append(
|
|
|
|
Ox.SyntaxHighlighter({
|
2012-04-06 12:10:21 +00:00
|
|
|
replace: self.options.replaceCode,
|
2012-04-04 14:59:58 +00:00
|
|
|
source: trim(section.code)
|
|
|
|
})
|
|
|
|
)
|
2012-04-06 12:10:21 +00:00
|
|
|
.appendTo($section);
|
2012-04-04 14:59:58 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function trim(str) {
|
|
|
|
// removes leading or trailing empty line
|
|
|
|
return str.replace(/^\s*\n/, '').replace(/\n\s*$/, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
return that;
|
|
|
|
|
2012-05-21 10:38:18 +00:00
|
|
|
};
|