Skip to content

Instantly share code, notes, and snippets.

View micahasmith's full-sized avatar

Micah Smith micahasmith

View GitHub Profile
@micahasmith
micahasmith / product-list.js
Created December 29, 2011 00:39
multiply all items in an array
var product=function(list) {
var len=list.length,
sum=1,
i=0;
for(;i<len;i+=1) {
sum=sum*list[i];
}
return sum;
}
@micahasmith
micahasmith / sum-list.js
Created December 29, 2011 00:35
sum items in an array
var sum=function(list) {
var len=list.length,
sum=0,
i=0;
for(;i<len;i+=1) {
sum+=list[i];
}
return sum;
}
@micahasmith
micahasmith / hogan-express-app.js
Created December 23, 2011 21:29
Example node.js app using hogan.js and express.js
var express=require('express'),
hogan=require('hogan.js'),
adapter=require('./hogan-express.js'),
app = express.createServer();
app.configure(function(){
app.use(express.static(__dirname + '/public'));
app.use(express.logger());
app.use(express.bodyParser());
app.use(express.cookieParser());
@micahasmith
micahasmith / hogan-express.js
Created December 23, 2011 21:28
hogan.js express.js view engine adapter
var HoganExpressAdapter=(function(){
var init=function(hogan) {
var compile=function(source){
return function(options) {
return hogan.compile(source).render(options);
};
}
return {compile:compile};
};
return {init:init};
@micahasmith
micahasmith / html5-vid.sh
Created December 23, 2011 16:21
ffmpeg html5 video maker script
#!/bin/bash
# html5 videos maker
ffmpeg -i $1 -b 1500k -vcodec libx264 -g 30 -s 640x360 $1.mp4
ffmpeg -i $1 -b 1500k -vcodec libvpx -acodec libvorbis -ab 55000 -f webm -g 30 -s 640x360 $1.webm
ffmpeg -i $1 -b 1500k -vcodec libtheora -acodec libvorbis -ab 55000 -g 30 -s 640x360 $1.ogv
@micahasmith
micahasmith / d3-js-enter.js
Created December 17, 2011 21:19
d3.js enter example
//so there's two divs on the page for this to select
d3.select('body')
.selectAll('div')
.data(["hello","there","world"])
//this will map "hello" and "there" to the only two divs
.html(function(d){return d;})
//but theres 3 pieces of data...?
//this is why d3 has the enter() function
//think of it as "withTheLeftOverDataAfterMapping()" instead of "enter()"
.enter()
@micahasmith
micahasmith / d3-js-simple-example.html
Created December 16, 2011 03:41
d3.js simple animation/data example
<html>
<head>
<title>d3test</title>
<link href="bootstrap-1.1.0.min.css" type="text/css"></link>
<script src="d3.min.js" type="text/javascript"></script>
</head>
<body>
<div id="demo">
<svg id="svgdemo"></svg>
</div>
@micahasmith
micahasmith / gist:1484318
Created December 16, 2011 03:32
d3.js data example
//select all divs
d3.selectAll('div')
//map some data to those divs, in order that they were found in the DOM
.data(["hello","world","hi"])
//and now do something to the divs with that data
//perhaps, put the data in the divs
//so this function param "d" is actually the data for that div
.html(function(d) {
return d;
});
@micahasmith
micahasmith / gist:1484274
Created December 16, 2011 03:17
d3.js simple example
//grab all divs on the page
//and set their width to 300
d3.selectAll('div')
.attr("width",300)
.attr("height",400);
@micahasmith
micahasmith / ckeditor-selection-wrap-with-rangy.js
Created November 11, 2011 20:08
Wrap a selection in CKEditor with Rangy
/**
* allows you to wrap or insert an html tag over a selection/range using rangy
* @param iframe the CKEditor iframe html element
* @param tagName string representation of the tag, such as 'a' for anchor
* @param withNodeFunc function to allow outside modification of the element before injecting/wrapping
*/
function wrapOrInsert(iframe, tagName, withNodeFunc) {
var iframedoc = iframe.contentDocument || iframe.contentWindow.document,
tag = iframedoc.createElement(tagName),
selection = rangy.getIframeSelection(iframe),