Skip to content

Instantly share code, notes, and snippets.

@kenmazaika
Created February 21, 2012 11:18
Show Gist options
  • Save kenmazaika/1875933 to your computer and use it in GitHub Desktop.
Save kenmazaika/1875933 to your computer and use it in GitHub Desktop.
Embed Gist In Tumblr, File By File
<div class="gist">
<a href=
"https://gist.github.com/1870642.js?file=resque.rake">https://gist.github.com/1870642.js?file=resque.rake</a>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://gist.github.com/stylesheets/gist/embed.css"/>
<!-- Based off of:
http://inparens.tumblr.com/post/1362127815/embedding-github-gists-in-tumblr-posts -->
<script type="text/javascript">
//<![CDATA[
var gistPrefix = 'https://gist.github.com/',
// Cache document.write so that it can be restored once all Gists have been
// embedded.
cachedWrite = document.write,
body = $('body'),
// Map each p.gist to an object that contains the paragraph to be replaced
// and the Gist's identifier.
// Modified the p selector, so you don't have to edit in HTML to add class
gists = $('div.gist:contains("https://gist.github.com/")').map(function(n, div) {
p = $(div);
var a = $('a', div),
href = a.attr('href');
// Only return the mapping if a valid a exists in the p.
if (a.length && href.indexOf(gistPrefix) == 0) {
return {
p: p,
id: href.substring(gistPrefix.length)
};
}
else {
return undefined;
}
}).get(),
embedNextGist = function() {
// If there are no more Gists to embed, restore document.write.
if (gists.length == 0) {
document.write = cachedWrite;
}
else {
var gist = gists.shift();
// The Gist javascript file consists of a call to document.write to
// write the stylesheet link element and a second call to
// document.write to write the div containing the marked up Gist.
// So, override document.write to catch the first call. And inside
// that override, override document.write again to catch the second
// call.
document.write = function(styleLink) {
// On my Tumblr page, I include GitHub's Gist stylesheet in my
// overall page template so nothing happens here. If you don't
// want to do that, you could do something here like
// $('head').append(styleLink);
document.write = function(gistDiv) {
// Replace the original paragraph with the formatted div
// written by the Gist javascript file.
gist.p.replaceWith(gistDiv);
embedNextGist();
};
};
body.append('<scr' + 'ipt src="' + gistPrefix + gist.id +
'"><\/scr' + 'ipt>');
}
};
// One call to embedNextGist to get the ball rolling.
embedNextGist();
//]]>
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment