Skip to content

Instantly share code, notes, and snippets.

@comfuture
comfuture / sprintf.js
Created February 6, 2013 04:27
simple javascript implement of sprintf
function sprintf(format, arr) {
var i = -1;
var replacer = function(exp, p0, p1, p2, p3, p4) {
if (exp == '%%') return '%';
if (arr[++i] === undefined) return undefined;
var exp = p2 ? parseInt(p2.substr(1)) : undefined;
var base = p3 ? parseInt(p3.substr(1)) : undefined;
var val;
switch (p4) {
case 's': val = arr[i]; break;
@comfuture
comfuture / readme.md
Created December 7, 2011 09:49
proposal of javascript like template syntax

Overview

  • 각 html엘리먼트명에 대응하는 함수는 String, Array, Object의 3가지 형태의 파라메터를 받을 수 있다. 모든 파라메터를 생략 가능하고 순서는 상관 없다.
    • String형은 innerHTML에 대응
    • Object형은 attributes에 대응
    • Array형은 child elements에 대응, Array의 요소로써 String은 textNode에 대응

Templating

  • 템플릿처럼 사용하기 위해 $로 시작하는 변수를 context변수로 사용한다.
@comfuture
comfuture / ie_radius.js
Created October 31, 2011 15:15
make ie<9 to support border-radius
;(function($){
// append vml xmlns to document
if ($.browser.msie && document.namespaces["v"] == null) {
document.namespaces.add("v", "urn:schemas-microsoft-com:vml");
var ss = document.createStyleSheet().owningElement;
ss.styleSheet.cssText = "v\\:*{behavior:url(#default#VML);}"
}
var getHTML = function(o) {
return '<div class="ie_border_radius" style="position: absolute; left: 0px; top: 0px; z-index: -1; width:' +
@comfuture
comfuture / jquery.placeholder.js
Created October 29, 2011 09:16
emulate placeholder property to browser that didn't support placeholder property it self
jQuery(function() {
jQuery.support.placeholder = 'placeholder' in document.createElement('input');
if (jQuery.support.placeholder) return;
var isEmpty = function(el) {
return el.val().length === 0 || el.val() == el.attr('placeholder');
},
hold = function(el) {
if (isEmpty(el)) {
var e = el.get(0);
@comfuture
comfuture / web.py
Created August 13, 2011 18:38
flask extension that responses flexible result with negotiates accept header and checks permission
import json
from functools import wraps
from flask import render_template, url_for, request, redirect, session
from flask import Response
def require_role(role='student'):
def decorate(f):
@wraps(f)
def check(*args, **kwargs):
if session.get('role') != role:
@comfuture
comfuture / backup.sh
Created July 29, 2011 06:04
safe minecraft incremental backup script
#!/bin/sh
# init git repository with "git init" command
# minecraft server is running on screen named "mc"
# ^M means newline character. type c-V-M in your editor
# put this file in your minecraft server's directory
# edit crontab with "crontab -e" and add this line:
# 0 12 * * * /path/to/minecraft/backup.sh > /dev/null 2>&1
CWD=$(readlink -f `dirname $0`)
screen -S mc -X stuff "say [Backup] Server goes readonly in 10 seconds^M"
@comfuture
comfuture / DBStorageStream.php
Created May 23, 2011 06:14
using database as filesystem
<?php
class DBStorageStream
{
const DDL = <<<EOF
CREATE TABLE IF NOT EXISTS `table_dbfs` (
`path` VARCHAR(255) NOT NULL PRIMARY KEY,
`data` LONGTEXT,
`is_dir` CHAR(1) NOT NULL DEFAULT 'N',
`created_at` DATETIME,
<?
class Response
{
private $headers = array();
private $body = '';
function __construct($body='', $headers=array())
{
$this->body = $body;
$this->headers = array_merge($this->headers, $headers);
@comfuture
comfuture / Article.php
Created May 13, 2011 01:52
article class that contains orm and form annotations
<?php
/**
* @Entity
* @Form(method="POST")
*/
class Article
{
/** @Id @GeneratedValue @Column(type="integer") */
private $id;
<?php
namespace webapp;
/**
* base Doctrine entity for convinent orm association
*/
class BaseEntity
{
protected $_properties;