Sunday, March 6, 2011

How to embed code snippets in blogger

Blogger.com (this blogging service) is terrible for embedding code.  Everything gets mangled, double-spaced and mis-formatted. Just terrible. But there is an answer to code snippet embedding: Gist.

Gist is a free service run by Github, the open-source project hosting service. Gist allows you to post code snippets share them via a unique URL, and even allows for comments. Just go to The Gist Website, paste in your code, give it a title and description, then click "embed" and copy the link. You can paste the link into your blog, and (using javascript) it will embed an un-mangled code snippet, just like this:


/*
* Load Prototype and script.aculo.us, then slide a div up and down.
*/
google.load("prototype", "1");
google.load("scriptaculous", "1");
var up = false;
function slideUpDown() {
if (up) {
Effect.SlideDown('content', {'duration' : 0.2});
up = false;
} else {
Effect.SlideUp('content', {'duration' : 0.2});
up = true;
}
}
function OnLoad(){
var content = $('content');
content.innerHTML = '<div>The Content</div>';
window.setInterval(slideUpDown, 600);
}
google.setOnLoadCallback(OnLoad);​
view raw Test code post hosted with ❤ by GitHub

No comments:

Post a Comment