Skip to content

Instantly share code, notes, and snippets.

View shesek's full-sized avatar

Nadav Ivgi shesek

View GitHub Profile
@shesek
shesek / template-auto-escaping.js
Created July 20, 2011 11:49
Underscore.js templates escaping support
_.extend(_.templateSettings, {
encode: /<%=([\s\S]+?)%>/g,
interpolate : /<%==([\s\S]+?)%>/g
});
_.extend(_, {
// Taken from Backbone.js's escapeHTML()
escape: function(string) {
return (''+string).replace(/&(?!\w+;|#\d+;|#x[\da-f]+;)/gi, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#x27;').replace(/\//g,'&#x2F;');
},
template: function(str, data) {
@shesek
shesek / Chainable.php
Created July 21, 2011 07:11
Chainable wrapper for PHP
<?php
class Chainable {
private $obj;
private $result;
public function __construct($obj){
$this->obj = $obj;
}
public function __call($method, $args) {
if (substr($method, 0, 1) == '_') {
array_unshift($args, $this->result);
@shesek
shesek / ResourceWrapper.php
Created July 21, 2011 07:23
Generic PHP object wrapper for resources
<?php
/**
* A wrapper class for PHP resourcse.
*
* Used for wrapping PHP resources in an object, and working with them in an
* Object-Oriented manner.
*
* Some methods are prefixed with `_` even though they're public to avoid
* clashes with resource-related functions.
*
@shesek
shesek / Toodledo.php
Created July 27, 2011 15:02
Toodledo PHP API
<?php
/* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details. */
// Written by Shesek, http://www.shesek.info/php/toodledo-api
class Toodledo {
@shesek
shesek / setDefault.js
Created July 30, 2011 11:19
JavaScript default arguments
window.setDefault = function (args) {
for (var i = args.length + 1; i<arguments.length; i++) {
args[i-1] = arguments[i];
}
};
@shesek
shesek / i18n.coffee
Created November 14, 2011 22:38 — forked from madrobby/i18n.coffee
i18n with CoffeeScript
# Works with `global` instead of `window` when available (nodejs environment), no Backbone dependency,
# no ev[ai]l for rendering variables and a shorter `{...}` syntax.
(global or window).t = (id, vars = {}) ->
(i18n[__locale][id] or i18n.en[id] or "(?) #{id}")
.replace /\{(\w+)\}/g, (a, k) -> vars[k] or "?#{k}"
###
i18n=
@shesek
shesek / gist:1444629
Created December 7, 2011 21:00
CS stuff
_ = require 'underscore'
{Assign, Value, Literal, Access, Block, Class, Op, Obj, Arr, For, Index, Call, Return, If, Throw} = nodes = require '../nodes'
exports.originals = originals = {}
exports.codeblock = codeblock = (block) ->
return block unless block?
if typeof block is 'function'
@shesek
shesek / inject.coffee
Created February 11, 2012 04:10
Executing code in the webpage context from Chrome extensions.
inject = (args..., fn) ->
script = document.createElement 'script'
script.innerHTML = """
Array.prototype.pop.call(document.getElementsByTagName('script')).innerText = JSON.stringify(function() {
try { return #{ fn.toString() }.apply(window, #{ JSON.stringify args }); }
catch(e) { return {isException: true, exception: e.toString()}; }
}());
"""
document.body.appendChild script
response = JSON.parse script.innerText
@shesek
shesek / autocomplete-format.coffee
Created February 26, 2012 10:43
Custom URL format in jQuery-UI autocomplete source
autocompleteFormat = (url) -> (request, response) -> $.get (url.replace '%s', encodeURIComponent request.term), response, 'json'
# Usage
$('#search').autocomplete
source: autocompleteFormat '/search/%s.json'
@shesek
shesek / object-create.coffee
Created February 26, 2012 13:45
ES5 Object.create helpers
defaults = writable: true, enumerable: true, configurable: true
initializer = (key) -> (val) ->
prop = Object.create defaults
prop[key] = val
prop
value = initializer 'value'
getter = initializer 'get'
setter = initializer 'set'