Skip to content

Instantly share code, notes, and snippets.

@drewlesueur
Created May 20, 2010 15:02
Show Gist options
  • Save drewlesueur/407668 to your computer and use it in GitHub Desktop.
Save drewlesueur/407668 to your computer and use it in GitHub Desktop.
Scripter
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
textarea {
width: 500px;
height: 500px;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<!--<script type="text/javascript" src="json.js"></script>-->
<script>
//underscore.js
(function(){var n=this,A=n._,r=typeof StopIteration!=="undefined"?StopIteration:"__break__",j=Array.prototype,l=Object.prototype,o=j.slice,B=j.unshift,C=l.toString,p=l.hasOwnProperty,s=j.forEach,t=j.map,u=j.reduce,v=j.reduceRight,w=j.filter,x=j.every,y=j.some,m=j.indexOf,z=j.lastIndexOf;l=Array.isArray;var D=Object.keys,b=function(a){return new k(a)};if(typeof exports!=="undefined")exports._=b;n._=b;b.VERSION="1.0.4";var i=b.forEach=function(a,c,d){try{if(s&&a.forEach===s)a.forEach(c,d);else if(b.isNumber(a.length))for(var e=
0,f=a.length;e<f;e++)c.call(d,a[e],e,a);else for(e in a)p.call(a,e)&&c.call(d,a[e],e,a)}catch(g){if(g!=r)throw g;}return a};b.map=function(a,c,d){if(t&&a.map===t)return a.map(c,d);var e=[];i(a,function(f,g,h){e.push(c.call(d,f,g,h))});return e};b.reduce=function(a,c,d,e){if(u&&a.reduce===u)return a.reduce(b.bind(d,e),c);i(a,function(f,g,h){c=d.call(e,c,f,g,h)});return c};b.reduceRight=function(a,c,d,e){if(v&&a.reduceRight===v)return a.reduceRight(b.bind(d,e),c);a=b.clone(b.toArray(a)).reverse();return b.reduce(a,
c,d,e)};b.detect=function(a,c,d){var e;i(a,function(f,g,h){if(c.call(d,f,g,h)){e=f;b.breakLoop()}});return e};b.filter=function(a,c,d){if(w&&a.filter===w)return a.filter(c,d);var e=[];i(a,function(f,g,h){c.call(d,f,g,h)&&e.push(f)});return e};b.reject=function(a,c,d){var e=[];i(a,function(f,g,h){!c.call(d,f,g,h)&&e.push(f)});return e};b.every=function(a,c,d){c=c||b.identity;if(x&&a.every===x)return a.every(c,d);var e=true;i(a,function(f,g,h){(e=e&&c.call(d,f,g,h))||b.breakLoop()});return e};b.some=
function(a,c,d){c=c||b.identity;if(y&&a.some===y)return a.some(c,d);var e=false;i(a,function(f,g,h){if(e=c.call(d,f,g,h))b.breakLoop()});return e};b.include=function(a,c){if(m&&a.indexOf===m)return a.indexOf(c)!=-1;var d=false;i(a,function(e){if(d=e===c)b.breakLoop()});return d};b.invoke=function(a,c){var d=b.rest(arguments,2);return b.map(a,function(e){return(c?e[c]:e).apply(e,d)})};b.pluck=function(a,c){return b.map(a,function(d){return d[c]})};b.max=function(a,c,d){if(!c&&b.isArray(a))return Math.max.apply(Math,
a);var e={computed:-Infinity};i(a,function(f,g,h){g=c?c.call(d,f,g,h):f;g>=e.computed&&(e={value:f,computed:g})});return e.value};b.min=function(a,c,d){if(!c&&b.isArray(a))return Math.min.apply(Math,a);var e={computed:Infinity};i(a,function(f,g,h){g=c?c.call(d,f,g,h):f;g<e.computed&&(e={value:f,computed:g})});return e.value};b.sortBy=function(a,c,d){return b.pluck(b.map(a,function(e,f,g){return{value:e,criteria:c.call(d,e,f,g)}}).sort(function(e,f){var g=e.criteria,h=f.criteria;return g<h?-1:g>h?
1:0}),"value")};b.sortedIndex=function(a,c,d){d=d||b.identity;for(var e=0,f=a.length;e<f;){var g=e+f>>1;d(a[g])<d(c)?(e=g+1):(f=g)}return e};b.toArray=function(a){if(!a)return[];if(a.toArray)return a.toArray();if(b.isArray(a))return a;if(b.isArguments(a))return o.call(a);return b.values(a)};b.size=function(a){return b.toArray(a).length};b.first=function(a,c,d){return c&&!d?o.call(a,0,c):a[0]};b.rest=function(a,c,d){return o.call(a,b.isUndefined(c)||d?1:c)};b.last=function(a){return a[a.length-1]};
b.compact=function(a){return b.filter(a,function(c){return!!c})};b.flatten=function(a){return b.reduce(a,[],function(c,d){if(b.isArray(d))return c.concat(b.flatten(d));c.push(d);return c})};b.without=function(a){var c=b.rest(arguments);return b.filter(a,function(d){return!b.include(c,d)})};b.uniq=function(a,c){return b.reduce(a,[],function(d,e,f){if(0==f||(c===true?b.last(d)!=e:!b.include(d,e)))d.push(e);return d})};b.intersect=function(a){var c=b.rest(arguments);return b.filter(b.uniq(a),function(d){return b.every(c,
function(e){return b.indexOf(e,d)>=0})})};b.zip=function(){for(var a=b.toArray(arguments),c=b.max(b.pluck(a,"length")),d=new Array(c),e=0;e<c;e++)d[e]=b.pluck(a,String(e));return d};b.indexOf=function(a,c){if(m&&a.indexOf===m)return a.indexOf(c);for(var d=0,e=a.length;d<e;d++)if(a[d]===c)return d;return-1};b.lastIndexOf=function(a,c){if(z&&a.lastIndexOf===z)return a.lastIndexOf(c);for(var d=a.length;d--;)if(a[d]===c)return d;return-1};b.range=function(a,c,d){var e=b.toArray(arguments),f=e.length<=
1;a=f?0:e[0];c=f?e[0]:e[1];d=e[2]||1;e=Math.ceil((c-a)/d);if(e<=0)return[];e=new Array(e);f=a;for(var g=0;;f+=d){if((d>0?f-c:c-f)>=0)return e;e[g++]=f}};b.bind=function(a,c){var d=b.rest(arguments,2);return function(){return a.apply(c||{},d.concat(b.toArray(arguments)))}};b.bindAll=function(a){var c=b.rest(arguments);if(c.length==0)c=b.functions(a);i(c,function(d){a[d]=b.bind(a[d],a)});return a};b.memoize=function(a,c){var d={};c=c||b.identity;return function(){var e=c.apply(this,arguments);return e in
d?d[e]:(d[e]=a.apply(this,arguments))}};b.delay=function(a,c){var d=b.rest(arguments,2);return setTimeout(function(){return a.apply(a,d)},c)};b.defer=function(a){return b.delay.apply(b,[a,1].concat(b.rest(arguments)))};b.wrap=function(a,c){return function(){var d=[a].concat(b.toArray(arguments));return c.apply(c,d)}};b.compose=function(){var a=b.toArray(arguments);return function(){for(var c=b.toArray(arguments),d=a.length-1;d>=0;d--)c=[a[d].apply(this,c)];return c[0]}};b.keys=D||function(a){if(b.isArray(a))return b.range(0,
a.length);var c=[];for(var d in a)p.call(a,d)&&c.push(d);return c};b.values=function(a){return b.map(a,b.identity)};b.functions=function(a){return b.filter(b.keys(a),function(c){return b.isFunction(a[c])}).sort()};b.extend=function(a){i(b.rest(arguments),function(c){for(var d in c)a[d]=c[d]});return a};b.clone=function(a){if(b.isArray(a))return a.slice(0);return b.extend({},a)};b.tap=function(a,c){c(a);return a};b.isEqual=function(a,c){if(a===c)return true;var d=typeof a;if(d!=typeof c)return false;
if(a==c)return true;if(!a&&c||a&&!c)return false;if(a.isEqual)return a.isEqual(c);if(b.isDate(a)&&b.isDate(c))return a.getTime()===c.getTime();if(b.isNaN(a)&&b.isNaN(c))return false;if(b.isRegExp(a)&&b.isRegExp(c))return a.source===c.source&&a.global===c.global&&a.ignoreCase===c.ignoreCase&&a.multiline===c.multiline;if(d!=="object")return false;if(a.length&&a.length!==c.length)return false;d=b.keys(a);var e=b.keys(c);if(d.length!=e.length)return false;for(var f in a)if(!(f in c)||!b.isEqual(a[f],
c[f]))return false;return true};b.isEmpty=function(a){if(b.isArray(a)||b.isString(a))return a.length===0;for(var c in a)if(p.call(a,c))return false;return true};b.isElement=function(a){return!!(a&&a.nodeType==1)};b.isArray=l||function(a){return!!(a&&a.concat&&a.unshift&&!a.callee)};b.isArguments=function(a){return a&&a.callee};b.isFunction=function(a){return!!(a&&a.constructor&&a.call&&a.apply)};b.isString=function(a){return!!(a===""||a&&a.charCodeAt&&a.substr)};b.isNumber=function(a){return a===
+a||C.call(a)==="[object Number]"};b.isBoolean=function(a){return a===true||a===false};b.isDate=function(a){return!!(a&&a.getTimezoneOffset&&a.setUTCFullYear)};b.isRegExp=function(a){return!!(a&&a.test&&a.exec&&(a.ignoreCase||a.ignoreCase===false))};b.isNaN=function(a){return b.isNumber(a)&&isNaN(a)};b.isNull=function(a){return a===null};b.isUndefined=function(a){return typeof a=="undefined"};b.noConflict=function(){n._=A;return this};b.identity=function(a){return a};b.times=function(a,c,d){for(var e=
0;e<a;e++)c.call(d,e)};b.breakLoop=function(){throw r;};b.mixin=function(a){i(b.functions(a),function(c){E(c,b[c]=a[c])})};var F=0;b.uniqueId=function(a){var c=F++;return a?a+c:c};b.templateSettings={start:"<%",end:"%>",interpolate:/<%=(.+?)%>/g};b.template=function(a,c){var d=b.templateSettings,e=new RegExp("'(?=[^"+d.end.substr(0,1)+"]*"+d.end.replace(/([.*+?^${}()|[\]\/\\])/g,"\\$1")+")","g");d=new Function("obj","var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push('"+a.replace(/[\r\t\n]/g,
" ").replace(e,"\t").split("'").join("\\'").split("\t").join("'").replace(d.interpolate,"',$1,'").split(d.start).join("');").split(d.end).join("p.push('")+"');}return p.join('');");return c?d(c):d};b.each=b.forEach;b.foldl=b.inject=b.reduce;b.foldr=b.reduceRight;b.select=b.filter;b.all=b.every;b.any=b.some;b.head=b.first;b.tail=b.rest;b.methods=b.functions;var k=function(a){this._wrapped=a},q=function(a,c){return c?b(a).chain():a},E=function(a,c){k.prototype[a]=function(){var d=b.toArray(arguments);
B.call(d,this._wrapped);return q(c.apply(b,d),this._chain)}};b.mixin(b);i(["pop","push","reverse","shift","sort","splice","unshift"],function(a){var c=j[a];k.prototype[a]=function(){c.apply(this._wrapped,arguments);return q(this._wrapped,this._chain)}});i(["concat","join","slice"],function(a){var c=j[a];k.prototype[a]=function(){return q(c.apply(this._wrapped,arguments),this._chain)}});k.prototype.chain=function(){this._chain=true;return this};k.prototype.value=function(){return this._wrapped}})();
function cols_to_obj(cols) {
var ret = {}
cols.replace(/^([^\t]+)\t([^\t]*)$/mg, function(o, k, v){
ret[k] = v;
})
return ret;
}
function time() {
return (new Date()).getTime()
}
function htmlspecialchars (string, quote_style, charset, double_encode) {
http://github.com/kvz/phpjs/raw/master/functions/strings/htmlspecialchars.js
var optTemp = 0, i = 0, noquotes= false;
if (typeof quote_style === 'undefined' || quote_style === null) {
quote_style = 2;
}
string = string.toString();
if (double_encode !== false) { // Put this first to avoid double-encoding
string = string.replace(/&/g, '&amp;');
}
string = string.replace(/</g, '&lt;').replace(/>/g, '&gt;');
var OPTS = {
'ENT_NOQUOTES': 0,
'ENT_HTML_QUOTE_SINGLE' : 1,
'ENT_HTML_QUOTE_DOUBLE' : 2,
'ENT_COMPAT': 2,
'ENT_QUOTES': 3,
'ENT_IGNORE' : 4
};
if (quote_style === 0) {
noquotes = true;
}
if (typeof quote_style !== 'number') { // Allow for a single string or an array of string flags
quote_style = [].concat(quote_style);
for (i=0; i < quote_style.length; i++) {
// Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4
if (OPTS[quote_style[i]] === 0) {
noquotes = true;
}
else if (OPTS[quote_style[i]]) {
optTemp = optTemp | OPTS[quote_style[i]];
}
}
quote_style = optTemp;
}
if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
string = string.replace(/'/g, '&#039;');
}
if (!noquotes) {
string = string.replace(/"/g, '&quot;');
}
return string;
}
function h(x) {
return htmlspecialchars(x, 'ENT_QUOTES');
}
function rnd(low, high) {
return Math.floor(Math.random() * (high-low+1)) + low
}
function tokenize(line, delimeter ) {
delimeter = delimeter || /:,+/
line = line.split("");
i = 0;
var state = "out";
var cur_token = []
var tokens = [];
var last_chr = "";
while (i < line.length) {
chr = line[i];
if (chr == " " && state == "out") {
tokens.push(cur_token.join(""))
cur_token = []
} else if (chr == '"' && state == "out") {
state = "in";
cur_token.push("'")
} else if (chr == '"' && state == "in" && last_chr != "\\") {
state = "out";
} else if (chr.match(delimeter) && state == "out") {
tokens.push(cur_token.join(""))
tokens.push(chr)
cur_token = []
} else {
cur_token.push(chr)
}
last_chr = chr;
i++
}
if (cur_token.length > 0) {
tokens.push(cur_token.join(""))
}
return tokens
}
function array_split(splitter, arr) {
var ret = []
var cur = []
var word;
for (var i in arr) {
word = arr[i];
if (word == splitter) {
if (cur.length > 0) {
ret.push(cur)
}
cur = []
} else {
cur.push(word)
}
}
if (cur.length > 0) {
ret.push(cur)
}
return ret;
}
//counting keys in an object
function count(foo) {
var count = 0;
for (var k in foo) {
if (foo.hasOwnProperty(k) && k != "toJSON") {
++count;
}
}
return count;
}
function _s(val, start, end) {
var need_to_join = false;
var ret = []
if (typeof val == "string") {
val = val.split("")
need_to_join = true;
}
if (start >= 0) {
} else {
start = val.length + start
}
if (end == null) {
ret = val.slice(start)
} else {
if (end < 0) {
end = val.length + end;
} else {
end = end + start
}
ret = val.slice(start, end)
}
if (need_to_join) {
return ret.join("")
} else {
return ret;
}
}
function _sup(s,o) {
return s.replace(/{([^{}]*)}/g,
function (a, b) {
var r = o[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
}
);
};
function array_to_object(a) {
ret = {};
for (i = 0; i < a.length; i++) {
ret[a[i]] = "";
}
return ret
}
function compare_array(a,b) {
if (is_array(a)) {
a = array_to_object(a);
}
if (is_array(b)) {
b = array_to_object(b)
}
var l = {} //left extra
var r = {} //right extra
var d = {} //different values
for (var i in a) {
if (!(i in b)) {
l[i] = a[i]
} else if (a[i] != b[i]){
d[i] = [a[i], b[i]]
}
}
for (var i in b) {
if (!(i in a)) {
r[i] = b[i]
}
}
return [l,r,d]
}
function is_array(value) {
return value && typeof value === 'object' && value.constructor === Array;
}
function table_html(rows, cols) {
ret = []
ret.push('<table cellspacing="0" cellpadding="0">')
for (var i =0; i < rows; i++) {
ret.push('<tr>')
for (var j =0; j <cols; j++) {
ret.push('<td><'+'/td>');
}
ret.push('</tr>')
}
ret.push("</table>")
return ret.join("")
}
function get_cell(table, x,y) {
return $($(table)[0].rows[x].cells[y])
}
//http://www.go4expert.com/forums/showthread.php?t=886
function days_in_month(iMonth, iYear)
{
return 32 - new Date(iYear, iMonth, 32).getDate();
}
function textual_date_subtract(date1, date2) {
//what about jan 31 to feb 28 ?? is that a month?
//doesn't work with negative answers
var now_year, now_month, now_day;
var date1_year, date1_month, date1_day;
var date2_year, date2_month, date2_day;
if (date1 == 'now' || date2 == 'now') {
var now = new Date();
var now_year = now.getFullYear();
var now_day = now.getDate();
var now_month = now.getMonth() + 1;
}
if (date1 == 'now') {
date1_year = now_year;
date1_month = now_month;
date1_day = now_day;
} else {
date1 = date1.split('-');
date1_year = date1[0] - 0;
date1_month = date1[1] - 0;
date1_day = date1[2] - 0;
}
if (date2 == 'now') {
date2_year = now_year;
date2_month = now_month;
date2_day = now_day;
} else {
date2 = date2.split('-');
date2_year = date2[0] - 0;
date2_month = date2[1] - 0;
date2_day = date2[2] - 0;
}
var year_diff = date1_year - date2_year;
var month_diff = date1_month - date2_month;
var day_diff = date1_day - date2_day;
if (day_diff < 0) {
if (month_diff < 0) {
month_diff = (12 - date2_month - 1) + (date1_month)
year_diff -= 1;
} else {
month_diff -= 1;
}
}
if (month_diff < 0) {
year_diff -= 1;
}
if (month_diff < 0) {
month_diff = 12 + month_diff
}
if (day_diff < 0) {
day_diff = (days_in_month(date2_month-1, date2_year) - date2_day) + date1_day
}
return {year: year_diff, month: month_diff, day: day_diff}
//takes two dates '2010-11-12' and '1984-11-12' and subtracts them textually.
}
function strtotime (str, now) {
// http://kevin.vanzonneveld.net
// + original by: Caio Ariede (http://caioariede.com)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: David
// + improved by: Caio Ariede (http://caioariede.com)
// + improved by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Wagner B. Soares
// + bugfixed by: Artur Tchernychev
// % note 1: Examples all have a fixed timestamp to prevent tests to fail because of variable time(zones)
// * example 1: strtotime('+1 day', 1129633200);
// * returns 1: 1129719600
// * example 2: strtotime('+1 week 2 days 4 hours 2 seconds', 1129633200);
// * returns 2: 1130425202
// * example 3: strtotime('last month', 1129633200);
// * returns 3: 1127041200
// * example 4: strtotime('2009-05-04 08:30:00');
// * returns 4: 1241418600
var i, match, s, strTmp = '', parse = '';
strTmp = str;
strTmp = strTmp.replace(/\s{2,}|^\s|\s$/g, ' '); // unecessary spaces
strTmp = strTmp.replace(/[\t\r\n]/g, ''); // unecessary chars
if (strTmp == 'now') {
return (new Date()).getTime()/1000; // Return seconds, not milli-seconds
} else if (!isNaN(parse = Date.parse(strTmp))) {
return (parse/1000);
} else if (now) {
now = new Date(now*1000); // Accept PHP-style seconds
} else {
now = new Date();
}
strTmp = strTmp.toLowerCase();
var __is =
{
day:
{
'sun': 0,
'mon': 1,
'tue': 2,
'wed': 3,
'thu': 4,
'fri': 5,
'sat': 6
},
mon:
{
'jan': 0,
'feb': 1,
'mar': 2,
'apr': 3,
'may': 4,
'jun': 5,
'jul': 6,
'aug': 7,
'sep': 8,
'oct': 9,
'nov': 10,
'dec': 11
}
};
var process = function (m) {
var ago = (m[2] && m[2] == 'ago');
var num = (num = m[0] == 'last' ? -1 : 1) * (ago ? -1 : 1);
switch (m[0]) {
case 'last':
case 'next':
switch (m[1].substring(0, 3)) {
case 'yea':
now.setFullYear(now.getFullYear() + num);
break;
case 'mon':
now.setMonth(now.getMonth() + num);
break;
case 'wee':
now.setDate(now.getDate() + (num * 7));
break;
case 'day':
now.setDate(now.getDate() + num);
break;
case 'hou':
now.setHours(now.getHours() + num);
break;
case 'min':
now.setMinutes(now.getMinutes() + num);
break;
case 'sec':
now.setSeconds(now.getSeconds() + num);
break;
default:
var day;
if (typeof (day = __is.day[m[1].substring(0, 3)]) != 'undefined') {
var diff = day - now.getDay();
if (diff == 0) {
diff = 7 * num;
} else if (diff > 0) {
if (m[0] == 'last') {diff -= 7;}
} else {
if (m[0] == 'next') {diff += 7;}
}
now.setDate(now.getDate() + diff);
}
}
break;
default:
if (/\d+/.test(m[0])) {
num *= parseInt(m[0], 10);
switch (m[1].substring(0, 3)) {
case 'yea':
now.setFullYear(now.getFullYear() + num);
break;
case 'mon':
now.setMonth(now.getMonth() + num);
break;
case 'wee':
now.setDate(now.getDate() + (num * 7));
break;
case 'day':
now.setDate(now.getDate() + num);
break;
case 'hou':
now.setHours(now.getHours() + num);
break;
case 'min':
now.setMinutes(now.getMinutes() + num);
break;
case 'sec':
now.setSeconds(now.getSeconds() + num);
break;
}
} else {
return false;
}
break;
}
return true;
};
match = strTmp.match(/^(\d{2,4}-\d{2}-\d{2})(?:\s(\d{1,2}:\d{2}(:\d{2})?)?(?:\.(\d+))?)?$/);
if (match != null) {
if (!match[2]) {
match[2] = '00:00:00';
} else if (!match[3]) {
match[2] += ':00';
}
s = match[1].split(/-/g);
for (i in __is.mon) {
if (__is.mon[i] == s[1] - 1) {
s[1] = i;
}
}
s[0] = parseInt(s[0], 10);
s[0] = (s[0] >= 0 && s[0] <= 69) ? '20'+(s[0] < 10 ? '0'+s[0] : s[0]+'') : (s[0] >= 70 && s[0] <= 99) ? '19'+s[0] : s[0]+'';
return parseInt(this.strtotime(s[2] + ' ' + s[1] + ' ' + s[0] + ' ' + match[2])+(match[4] ? match[4]/1000 : ''), 10);
}
var regex = '([+-]?\\d+\\s'+
'(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?'+
'|sun\\.?|sunday|mon\\.?|monday|tue\\.?|tuesday|wed\\.?|wednesday'+
'|thu\\.?|thursday|fri\\.?|friday|sat\\.?|saturday)'+
'|(last|next)\\s'+
'(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?'+
'|sun\\.?|sunday|mon\\.?|monday|tue\\.?|tuesday|wed\\.?|wednesday'+
'|thu\\.?|thursday|fri\\.?|friday|sat\\.?|saturday))'+
'(\\sago)?';
match = strTmp.match(new RegExp(regex, 'gi')); // Brett: seems should be case insensitive per docs, so added 'i'
if (match == null) {
return false;
}
for (i = 0; i < match.length; i++) {
if (!process(match[i].split(' '))) {
return false;
}
}
return (now.getTime()/1000);
}
function date(format, timestamp) {
// http://kevin.vanzonneveld.net
// + original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
// + parts by: Peter-Paul Koch (http://www.quirksmode.org/js/beat.html)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: MeEtc (http://yass.meetcweb.com)
// + improved by: Brad Touesnard
// + improved by: Tim Wiel
// + improved by: Bryan Elliott
// + improved by: Brett Zamir (http://brett-zamir.me)
// + improved by: David Randall
// + input by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Brett Zamir (http://brett-zamir.me)
// + improved by: Brett Zamir (http://brett-zamir.me)
// + improved by: Theriault
// + derived from: gettimeofday
// + input by: majak
// + bugfixed by: majak
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: Alex
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// + improved by: Theriault
// + improved by: Brett Zamir (http://brett-zamir.me)
// + improved by: Theriault
// + improved by: Thomas Beaucourt (http://www.webapp.fr)
// + improved by: JT
// + improved by: Theriault
// + improved by: Rafał Kukawski (http://blog.kukawski.pl)
// % note 1: Uses global: php_js to store the default timezone
// * example 1: date('H:m:s \\m \\i\\s \\m\\o\\n\\t\\h', 1062402400);
// * returns 1: '09:09:40 m is month'
// * example 2: date('F j, Y, g:i a', 1062462400);
// * returns 2: 'September 2, 2003, 2:26 am'
// * example 3: date('Y W o', 1062462400);
// * returns 3: '2003 36 2003'
// * example 4: x = date('Y m d', (new Date()).getTime()/1000);
// * example 4: (x+'').length == 10 // 2009 01 09
// * returns 4: true
// * example 5: date('W', 1104534000);
// * returns 5: '53'
// * example 6: date('B t', 1104534000);
// * returns 6: '999 31'
// * example 7: date('W U', 1293750000.82); // 2010-12-31
// * returns 7: '52 1293750000'
// * example 8: date('W', 1293836400); // 2011-01-01
// * returns 8: '52'
// * example 9: date('W Y-m-d', 1293974054); // 2011-01-02
// * returns 9: '52 2011-01-02'
var that = this,
jsdate, f, formatChr = /\\?([a-z])/gi, formatChrCb,
// Keep this here (works, but for code commented-out
// below for file size reasons)
//, tal= [],
_pad = function (n, c) {
if ((n = n + "").length < c) {
return new Array((++c) - n.length).join("0") + n;
} else {
return n;
}
},
txt_words = ["Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"],
txt_ordin = {
1: "st",
2: "nd",
3: "rd",
21: "st",
22: "nd",
23: "rd",
31: "st"
};
formatChrCb = function (t, s) {
return f[t] ? f[t]() : s;
};
f = {
// Day
d: function () { // Day of month w/leading 0; 01..31
return _pad(f.j(), 2);
},
D: function () { // Shorthand day name; Mon...Sun
return f.l().slice(0, 3);
},
j: function () { // Day of month; 1..31
return jsdate.getDate();
},
l: function () { // Full day name; Monday...Sunday
return txt_words[f.w()] + 'day';
},
N: function () { // ISO-8601 day of week; 1[Mon]..7[Sun]
return f.w() || 7;
},
S: function () { // Ordinal suffix for day of month; st, nd, rd, th
return txt_ordin[f.j()] || 'th';
},
w: function () { // Day of week; 0[Sun]..6[Sat]
return jsdate.getDay();
},
z: function () { // Day of year; 0..365
var a = new Date(f.Y(), f.n() - 1, f.j()),
b = new Date(f.Y(), 0, 1);
return Math.round((a - b) / 864e5) + 1;
},
// Week
W: function () { // ISO-8601 week number
var a = new Date(f.Y(), f.n() - 1, f.j() - f.N() + 3),
b = new Date(a.getFullYear(), 0, 4);
return 1 + Math.round((a - b) / 864e5 / 7);
},
// Month
F: function () { // Full month name; January...December
return txt_words[6 + f.n()];
},
m: function () { // Month w/leading 0; 01...12
return _pad(f.n(), 2);
},
M: function () { // Shorthand month name; Jan...Dec
return f.F().slice(0, 3);
},
n: function () { // Month; 1...12
return jsdate.getMonth() + 1;
},
t: function () { // Days in month; 28...31
return (new Date(f.Y(), f.n(), 0)).getDate();
},
// Year
L: function () { // Is leap year?; 0 or 1
var y = f.Y(), a = y & 3, b = y % 4e2, c = y % 1e2;
return 0 + (!a && (c || !b));
},
o: function () { // ISO-8601 year
var n = f.n(), W = f.W(), Y = f.Y();
return Y + (n === 12 && W < 9 ? -1 : n === 1 && W > 9);
},
Y: function () { // Full year; e.g. 1980...2010
return jsdate.getFullYear();
},
y: function () { // Last two digits of year; 00...99
return (f.Y() + "").slice(-2);
},
// Time
a: function () { // am or pm
return jsdate.getHours() > 11 ? "pm" : "am";
},
A: function () { // AM or PM
return f.a().toUpperCase();
},
B: function () { // Swatch Internet time; 000..999
var H = jsdate.getUTCHours() * 36e2, // Hours
i = jsdate.getUTCMinutes() * 60, // Minutes
s = jsdate.getUTCSeconds(); // Seconds
return _pad(Math.floor((H + i + s + 36e2) / 86.4) % 1e3, 3);
},
g: function () { // 12-Hours; 1..12
return f.G() % 12 || 12;
},
G: function () { // 24-Hours; 0..23
return jsdate.getHours();
},
h: function () { // 12-Hours w/leading 0; 01..12
return _pad(f.g(), 2);
},
H: function () { // 24-Hours w/leading 0; 00..23
return _pad(f.G(), 2);
},
i: function () { // Minutes w/leading 0; 00..59
return _pad(jsdate.getMinutes(), 2);
},
s: function () { // Seconds w/leading 0; 00..59
return _pad(jsdate.getSeconds(), 2);
},
u: function () { // Microseconds; 000000-999000
return _pad(jsdate.getMilliseconds() * 1000, 6);
},
// Timezone
e: function () { // Timezone identifier; e.g. Atlantic/Azores, ...
// The following works, but requires inclusion of the very large
// timezone_abbreviations_list() function.
/* var abbr = '', i = 0, os = 0;
if (that.php_js && that.php_js.default_timezone) {
return that.php_js.default_timezone;
}
if (!tal.length) {
tal = that.timezone_abbreviations_list();
}
for (abbr in tal) {
for (i = 0; i < tal[abbr].length; i++) {
os = -jsdate.getTimezoneOffset() * 60;
if (tal[abbr][i].offset === os) {
return tal[abbr][i].timezone_id;
}
}
}
*/
return 'UTC';
},
I: function () { // DST observed?; 0 or 1
// Compares Jan 1 minus Jan 1 UTC to Jul 1 minus Jul 1 UTC.
// If they are not equal, then DST is observed.
var a = new Date(f.Y(), 0), // Jan 1
c = Date.UTC(f.Y(), 0), // Jan 1 UTC
b = new Date(f.Y(), 6), // Jul 1
d = Date.UTC(f.Y(), 6); // Jul 1 UTC
return 0 + ((a - c) !== (b - d));
},
O: function () { // Difference to GMT in hour format; e.g. +0200
var a = jsdate.getTimezoneOffset();
return (a > 0 ? "-" : "+") + _pad(Math.abs(a / 60 * 100), 4);
},
P: function () { // Difference to GMT w/colon; e.g. +02:00
var O = f.O();
return (O.substr(0, 3) + ":" + O.substr(3, 2));
},
T: function () { // Timezone abbreviation; e.g. EST, MDT, ...
// The following works, but requires inclusion of the very
// large timezone_abbreviations_list() function.
/* var abbr = '', i = 0, os = 0, default = 0;
if (!tal.length) {
tal = that.timezone_abbreviations_list();
}
if (that.php_js && that.php_js.default_timezone) {
default = that.php_js.default_timezone;
for (abbr in tal) {
for (i=0; i < tal[abbr].length; i++) {
if (tal[abbr][i].timezone_id === default) {
return abbr.toUpperCase();
}
}
}
}
for (abbr in tal) {
for (i = 0; i < tal[abbr].length; i++) {
os = -jsdate.getTimezoneOffset() * 60;
if (tal[abbr][i].offset === os) {
return abbr.toUpperCase();
}
}
}
*/
return 'UTC';
},
Z: function () { // Timezone offset in seconds (-43200...50400)
return -jsdate.getTimezoneOffset() * 60;
},
// Full Date/Time
c: function () { // ISO-8601 date.
return 'Y-m-d\\Th:i:sP'.replace(formatChr, formatChrCb);
},
r: function () { // RFC 2822
return 'D, d M Y H:i:s O'.replace(formatChr, formatChrCb);
},
U: function () { // Seconds since UNIX epoch
return jsdate.getTime() / 1000 | 0;
}
};
this.date = function (format, timestamp) {
that = this;
jsdate = (
(typeof timestamp === 'undefined') ? new Date() : // Not provided
(timestamp instanceof Date) ? new Date(timestamp) : // JS Date()
new Date(timestamp * 1000) // UNIX timestamp (auto-convert to int)
);
return format.replace(formatChr, formatChrCb);
};
return this.date(format, timestamp);
}
function t() {
return $('#t').val()
}
function r(x) {
$('#r').val(x)
}
function save() {
var html = "<!doctype html><html>" + document.documentElement.innerHTML + "</html>"
document.location = "data:text/html," + encodeURIComponent(html);
}
$(document).ready(function(){
$('#t').blur(function(){
eval( $('#c').val() )
})
})
</script>
</head>
<textarea id="t" wrap="off"></textarea>
<textarea id="r" wrap="off"></textarea>
<br>
<textarea id="c" wrap="off"></textarea>
<input type="button" onclick="save()" value="save">
<pre>
a = $('#t').val()
a = a.split("\n");
ret = [];
for (i in a) {
ret.push()
}
$('#r').val(ret.join("\n"))
</pre>
<svg width="100%" height="100%" version="1.1"
xmlns="http://www.w3.org/2000/svg">
<rect width="300" height="100"
style="fill:rgb(0,0,255);stroke-width:1;
stroke:rgb(0,0,0)"/>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
viewBox="0 0 100 100" preserveAspectRatio="xMidYMid slice"
style="width:100%; height:100%; position:absolute; top:0; left:0; z-index:-1;">
<linearGradient id="gradient">
<stop class="begin" offset="0%"/>
<stop class="end" offset="100%"/>
</linearGradient>
<rect x="0" y="0" width="100" height="100" style="fill:url(#gradient)" />
<circle cx="50" cy="50" r="30" style="fill:url(#gradient)" />
</svg>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment