Skip to content

Instantly share code, notes, and snippets.

@mrmartineau
Created July 13, 2013 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrmartineau/5991620 to your computer and use it in GitHub Desktop.
Save mrmartineau/5991620 to your computer and use it in GitHub Desktop.
Dave Rupert's data code block jQuery plugin
/* 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,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
};
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