function format(tpl) {
var index = 1, items = arguments;
return (tpl || '').replace(/{(\w*)}/g, function(match, p1) {
return items[index++] || p1 || match;
});
}
function loadCSS(url, callback) {
return $('')
.attr({ href: url })
.on('load', callback)
.appendTo(document.head);
}
function lazyGist(element) {
var $btn = $(element);
var $self = $(element).parent();
var $link = $self.find('a');
$btn.prop('disabled', 'disabled').text('Loading...');
$.getJSON(format('{}.json?callback=?', $link.prop('href').replace($link.prop('hash'), '')))
.done(function(data) {
loadCSS(data.stylesheet, function() {
$self.replaceWith(data.div);
$('.gist .gist-file .gist-meta a').filter(function() { return this.href === $link.prop('href'); }).parents('.gist-file').siblings().remove();
});
})
.fail(function() { $self.replaceWith($('').attr('href', url).text(url)); });
}