oxjs/source/UI/js/Code/SourceViewer.js

96 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

'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
([options[, self]]) -> <o:Ox.Container> Source Viewer
2012-05-21 10:38:18 +00:00
@*/
Ox.SourceViewer = function(options, self) {
self = self || {};
var that = Ox.Container({}, self)
.defaults({
file: '',
replaceCode: [],
2012-05-26 15:48:19 +00:00
replaceComment: []
})
.options(options)
.addClass('OxSourceViewer');
self.options.replaceComment.unshift(
// removes indentation inside <pre> tags
[
/<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);
}).join('\n') + '</pre>';
}
]
);
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' ) {
i && sections.push({comment: '', code: ''});
2012-05-30 11:34:57 +00:00
token.value = Ox.parseMarkdown(
trim(token.value.slice(2, -2))
);
self.options.replaceComment.forEach(function(replace) {
2012-05-27 19:33:04 +00:00
token.value = token.value.replace(
replace[0], replace[1]
);
});
}
2012-05-27 19:33:04 +00:00
Ox.last(sections)[type] += token.value;
}
});
sections.forEach(function(section) {
var $section = $('<tr>')
.appendTo(self.$table),
$comment = $('<td>')
.addClass('OxComment OxSerif OxSelectable')
.html(Ox.addLinks(section.comment, true))
.appendTo($section),
$code = $('<td>')
.addClass('OxCode')
.append(
Ox.SyntaxHighlighter({
replace: self.options.replaceCode,
source: trim(section.code)
})
)
.appendTo($section);
});
setTimeout(function() {
var height = that.height();
if (self.$table.height() < height) {
self.$table.css({height: height + 'px'});
}
}, 100);
});
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
};