Created
July 26, 2011 15:30
-
-
Save sbp/1107023 to your computer and use it in GitHub Desktop.
jQuery to add "Download" links to PRE and XMP elements
This file contains hidden or 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
// Download Preformatted Sections | |
function makeDownloadable() { | |
// Iconza by Turbolink, Creative Commons BY 3.0 Unported | |
// http://www.softicons.com/free-icons/designers/turbomilk | |
var png = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAABxVBMVEUAAACFhYaBgYGHh4iHiImLjI2Gh4eGhodbW1xqa2toaGlpampVVVdhYWJTU1RmZmf///+JiYmkpKR8fHysrKyYmJiHh4e2trZmZmaRkZGIiIiTk5OKiopQUVKDhIZ+fn7ExMTGxsbMzMxhYWFkZGSCgoSGhoaurq5oaGhsbGxubm7JycmAgIJPT1CFhojq6uq+vr6cnJzh4eG1tbWUlJS8vLzn5+eenp69vb1nZ2dlZWXT09Ozs7PAwMCoqKrR0dHPz8/Ozs7i4uLCwsLBwcHZ2dnU1NS3t7eqqqp+f4CmpqaoqKjt7e3Hx8eVlZV7fH58fX6ampqZmZmdnZ3o6Ojl5eXk5OTf39/e3t50dXfd3d2Pj4+NjY2Li4uhoqOOjo6HiIrb29va2tp6enp3d3dub3BwcXOioqKBgYGFhYXv7+/IyMj5+fliYmLKystgYGBwcHBpaWpra21zc3NycnJcXFyQkJBhYmNlZWZtbW3Ly8xcXV6jo6Nra2u/v793eHpjY2N2dnaCg4OLjI7g4OBmZmeCgoLLy8u5ubmEhIRfX2Dc3NzW1tbKysqxsbFPT1Gvr69NTk9NTU9NTk5NTU7m5ua7u7tu8VCdAAAAEHRSTlMAnxe99/evvfC1qKf4+PGpMlfdYgAAAPdJREFUGBlFwYNuRAEQAMCt3e6zz7ZZ27Zt27b9vW2aSzoDySkX/9LSATKKfV9d3eW+y57e/ofH4SQYHRuvjpeUlplp+4JWt5wKrirdk71Ji0gWeSLf+y5QxSsqjz1BRC/nL3zWqMB4c1vDhRnEgCQIZNAIDs2reR7/LNaG/Q6oi9STDY34y002B+gWaOW8be0dbsROSWbnDvvghB4YHCJGQiFWTU1MMlMwPTOrpiy8zbqk16+sSmuwzmxsYsKWdXsHdgXbHiZEo/IB5BHUkSXK8zGD4TR2dp4JWVfXBCGLosiaTHfUfTZATq6iKC9O59v7x2d+AfwAhIM7aBVLV5oAAAAASUVORK5CYII='; | |
$('pre.data, xmp.data').each(function() { | |
var data = $(this); | |
if (data.attr('data-downloadable')) { return } | |
var text = data.text(); | |
if (this.tagName == 'XMP') | |
text = text.replace(/^[ \t\r\n]+/, ''); | |
var alt = 'alt="Download the following preformatted code"'; | |
var a = $('<a><img src="' + png + '" ' + alt + ' title="Download"></a>'); | |
a.attr('href', 'data:text/plain,' + encodeURI(text)); | |
var p = $('<p style="float:right;margin-right:32px"></p>'); | |
p.append(a); | |
data.before(p); | |
data.attr('data-downloadable', 'true'); | |
}); | |
} | |
$(makeDownloadable); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment