2012-04-04 14:59:58 +00:00
|
|
|
'use strict';
|
|
|
|
|
2012-05-21 10:38:18 +00:00
|
|
|
/*@
|
2012-05-31 10:32:54 +00:00
|
|
|
Ox.SourceViewer <f> Source Viewer
|
2012-05-21 10:38:18 +00:00
|
|
|
options <o> Options
|
|
|
|
self <o> Shared private variable
|
2012-07-04 11:29:18 +00:00
|
|
|
([options[, self]]) -> <o:Ox.Container> Source Viewer
|
2012-05-21 10:38:18 +00:00
|
|
|
@*/
|
|
|
|
|
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: [],
|
2012-05-26 15:48:19 +00:00
|
|
|
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) {
|
2012-05-30 13:20:00 +00:00
|
|
|
// treat doc comments as code
|
|
|
|
var type = token.type == 'comment' && token.value[2] != '@'
|
|
|
|
? 'comment' : 'code';
|
|
|
|
// remove '//' comments
|
|
|
|
if (!/^\/\/[^@]/.test(token.value)) {
|
|
|
|
if (type == 'comment' ) {
|
2012-04-14 12:03:35 +00:00
|
|
|
i && sections.push({comment: '', code: ''});
|
2012-05-30 11:34:57 +00:00
|
|
|
token.value = Ox.parseMarkdown(
|
|
|
|
trim(token.value.slice(2, -2))
|
|
|
|
);
|
2012-04-14 12:03:35 +00:00
|
|
|
self.options.replaceComment.forEach(function(replace) {
|
2012-05-27 19:33:04 +00:00
|
|
|
token.value = token.value.replace(
|
|
|
|
replace[0], replace[1]
|
|
|
|
);
|
2012-04-14 12:03:35 +00:00
|
|
|
});
|
|
|
|
}
|
2012-05-27 19:33:04 +00:00
|
|
|
Ox.last(sections)[type] += token.value;
|
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>')
|
2013-07-19 08:42:25 +00:00
|
|
|
.addClass('OxComment OxSerif OxSelectable')
|
2012-05-29 10:14:58 +00:00
|
|
|
.html(Ox.addLinks(section.comment, true))
|
2012-04-06 12:10:21 +00:00
|
|
|
.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
|
|
|
});
|
2012-06-23 10:49:18 +00:00
|
|
|
setTimeout(function() {
|
|
|
|
var height = that.height();
|
|
|
|
if (self.$table.height() < height) {
|
|
|
|
self.$table.css({height: height + 'px'});
|
|
|
|
}
|
|
|
|
}, 100);
|
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
|
|
|
};
|