lockdown.js | |
---|---|
LockdownObfuscates code snippets by writing them to the canvas http://draeton.github.com/lockdown Copyright 2013, Matthew Cobbs Licensed under the MIT license. | /*global jQuery, FlashCanvas*/
(function (window, $) {
"use strict";
var document = window.document;
|
Some configuration defaults |
var defaults = {
/* font size and positioning */
fontsize : 12,
lineheight : 24,
font : "12px Courier",
baseline : "top",
yoffset : (24 - 12) / 2 - 2,
/* font colors */
linecolor : "blue",
codecolor : "black",
rowcolor : "LightCyan",
/* text start position */
top : 10,
left : 15,
indent : 8,
/* canvas width */
width : 600,
/* lines to remove from code before drawing */
filterexp : null
};
|
Lockdown constructorReturns a Lockdown singleton |
function Lockdown (options) {
var lockdown = window.Lockdown || this;
lockdown.init(options);
return lockdown;
}
|
Lockdown prototype |
Lockdown.prototype = {
|
initUpdate the settings object, perhaps when styling a new section of code blocks |
init: function (options) {
this.settings = $.extend({}, defaults, options);
},
|
lockLoop through each element to replace the code blocks with canvas elements |
lock: function ($elements) {
var self = this;
if ($elements) {
$elements = $elements.map(function (i, element) {
return self._replaceCodeBlock(element);
});
}
return $elements;
},
|
_getCodeDecode the base64 encoded code, get the code text as an array of lines, filter out the garbage lines |
_getCode: function (innerText, className) {
var s = this.settings;
/* check for base64 to determine decoding is needed */
if (/base64/.test(className)) {
innerText = decodeURIComponent(innerText);
}
/* remove whitespace and split on new lines */
var code = $.trim(innerText).split("\n");
/* if there is a filterexp regexp, filter lines */
if (s.filterexp) {
code = $.grep(code, function (line, i) {
return !s.filterexp.test(line);
});
}
/* return the array of code lines */
return code;
},
|
_getCanvasCreate the canvas element with height for the # of lines; FlashCanvas for IE support |
_getCanvas: function (code) {
var s = this.settings;
var canvas = document.createElement("canvas");
canvas.width = s.width;
canvas.height = code.length * s.lineheight + s.top * 2;
canvas.style.width = canvas.width + "px";
canvas.style.height = canvas.height + "px";
if (typeof FlashCanvas !== "undefined") {
FlashCanvas.initElement(canvas);
}
return canvas;
},
|
_writeTextWrite a piece of text to the canvas |
_writeText: function (context, text, x, y, color, align) {
var s = this.settings;
context.font = s.font;
context.textAlign = align || "left";
context.textBaseline = s.baseline;
context.fillStyle = color;
context.fillText(text, x, y);
},
|
_writeTextRightCurry |
_writeTextRight: function (context, text, x, y, color) {
this._writeText(context, text, x, y, color, "right");
},
|
_writeLineWrite the line number; write the code |
_writeLine: function (context, i, line) {
var s = this.settings;
var linex = s.left + s.indent - defaults.indent;
var liney = i * s.lineheight + s.top + s.yoffset;
var codex = s.left + s.indent;
var codey = i * s.lineheight + s.top + s.yoffset;
/* draw an alternating background */
if (i % 2) {
context.fillStyle = s.rowcolor;
context.fillRect(0, i * s.lineheight + s.top, s.width, s.lineheight);
}
this._writeTextRight(context, i + 1, linex, liney, s.linecolor);
this._writeText(context, line, codex, codey, s.codecolor);
},
|
_replaceCodeBlockParse the code from the pre element; place and get the canvas; get the drawing context |
_replaceCodeBlock: function (element) {
var self = this;
var s = this.settings;
/* Get the code array from the text */
var code = this._getCode($(element).text(), element.className);
/* determine the indent of the line numbers */
s.indent = defaults.indent * (code.length + "").length;
/* create a canvas */
var canvas = this._getCanvas(code);
var $canvas = $(canvas);
/* replace the element */
$(element).replaceWith(canvas);
/* get the drawing context and starting drawing lines */
var context = canvas.getContext("2d");
$.each(code, function (i, line) {
self._writeLine(context, i, line);
});
/* return the canvas with the code added to DOM data and a class of `lockdown` */
$canvas.data("code", code.join("\n")).addClass("lockdown");
return $canvas;
}
};
|
Set the global reference |
window.Lockdown = new Lockdown();
})(window, jQuery);
|