mirror of
https://github.com/jsbin/jsbin.git
synced 2026-02-01 16:46:05 +00:00
Add basic ability to preview bins
This is currently only a partial implementation of the PHP app.
This commit is contained in:
parent
f14555a272
commit
a51920a9bd
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user