Created
July 13, 2013 18:08
-
-
Save mrmartineau/5991620 to your computer and use it in GitHub Desktop.
Dave Rupert's data code block jQuery plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* jshint jquery:true */ | |
/* Author: Dave Rupert | |
* License: WTFPL | |
----------------------*/ | |
(function($){ | |
'use strict'; | |
$.fn.dataCodeBlock = function(){ | |
// Yoinked from Prototype.js | |
var escapeHTML = function( code ) { | |
return code.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>'); | |
}; | |
return $('[data-codeblock]').each(function(){ | |
var target = $(this).data('codeblock'); | |
var html = $(this).clone().removeAttr('data-codeblock')[0].outerHTML; | |
var codeblock = $('<pre><code>'); | |
codeblock.find('code').append( escapeHTML(html) ); | |
if(target) { | |
$(target).append(codeblock); | |
} else { | |
$(this).after(codeblock); | |
} | |
}); | |
}; | |
// Self Execute!! | |
$.fn.dataCodeBlock(); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment