Add basic ability to preview bins

This is currently only a partial implementation of the PHP app.
This commit is contained in:
Aron Carroll 2012-04-23 09:47:32 +01:00
parent f14555a272
commit a51920a9bd

View File

@ -20,7 +20,19 @@ module.exports = function (app) {
handlers.render(req, res, req.bin);
},
getBinPreview: function (req, res) {
res.send('bin: ' + req.bin.id);
handlers.formatPreview(req.bin, function (formatted) {
// TODO: Implement "quiet" flag.
if (formatted && false) {
formatted += '';
}
if (formatted) {
res.send(formatted);
} else {
res.contentType('js');
res.send(req.bin.javascript);
}
});
},
getBinSource: function (req, res) {
res.contentType('json');
@ -184,6 +196,34 @@ module.exports = function (app) {
return 'default.' + ext;
});
},
formatPreview: function (bin, fn) {
var formatted = bin.html || '',
insert, parts, last;
// TODO: Re implement this entire block with an HTML parser.
if (formatted) {
if (formatted.indexOf('%code%') > -1) {
formatted = formatted.replace(/%code%/g, bin.javascript);
} else {
insert = '<script>' + bin.javascript + '</script>';
parts = formatted.split('</body>');
last = parts.pop();
formatted = parts.join('</body>') + insert + '</body>' + last;
}
if (formatted.indexOf('%css%') > -1) {
formatted = formatted.replace(/%css%/g, bin.css);
} else {
insert = '<style>' + bin.css + '</style>';
parts = formatted.split('</head>');
last = parts.pop();
formatted = parts.join('</head>') + insert + '</head>' + last;
}
// TODO: Insert comment here.
fn(formatted);
} else {
fn(formatted);
}
},
NotFound: NotFound
};
return handlers;