Skip to content

Instantly share code, notes, and snippets.

View simonw's full-sized avatar

Simon Willison simonw

View GitHub Profile
function generateChart(figures) {
// figures is an object mapping labels to numbers
var cht = 'p'; // Chart type: pie
var chs = '460x200'; // Image dimensions
var chd = []; // Chart data
var chl = []; // Corresponding labels
var min = 0;
var max = 0;
$.each(figures, function(label, value) {
chl[chl.length] = label;
@simonw
simonw / jquery.comments.js
Created February 25, 2009 16:06
jQuery methods for accessing HTML comments and comment text. Only tested in Safari.
/* jQuery methods for accessing HTML comments and comment text
Only tested in Safari. */
jQuery.fn.comments = function() {
return this.contents().filter(function() {
return this.nodeType == 8;
})
};
jQuery.fn.commentText = function() {
var s = [];
@simonw
simonw / gist:92481
Created April 9, 2009 13:58
Compile nginx standalone without root access
# Compile nginx standalone without root access
mkdir ~/installed
mkdir ~/installed/nginx
mkdir ~/src
cd ~/src
# PCRE dependency - we'll compile against this statically
wget http://kent.dl.sourceforge.net/sourceforge/pcre/pcre-7.8.tar.gz
tar -xzvf pcre-7.8.tar.gz
@simonw
simonw / baseconv.py
Created April 11, 2009 10:32
Convert numbers from base 10 integers to base X strings and back again.
"""
Convert numbers from base 10 integers to base X strings and back again.
Sample usage:
>>> base20 = BaseConverter('0123456789abcdefghij')
>>> base20.from_decimal(1234)
'31e'
>>> base20.from_decimal('31e')
1234
@simonw
simonw / shorten-bookmarklet.js
Created April 11, 2009 15:19
Expanded form of a bookmarklet for extracting rev=canonical OR tinyurling a page
/* Expanded form of a bookmarklet for extracting rev=canonical OR tinyurling a page */
(function(){
var url=document.location;
var links=document.getElementsByTagName('link');
var found=0;
for(var i = 0, l; l = links[i]; i++) {
if (l.getAttribute('rev') == 'canonical' || (/alternate short/).exec(l.getAttribute('rel'))) {
found=l.getAttribute('href');
break;
}
@simonw
simonw / shorten-bookmarklet.js
Created April 12, 2009 12:32 — forked from singpolyma/shorten-bookmarklet.js
singpolyma's bit.ly version updated to detect rev=canonical on A elements as well
/* Expanded form of a bookmarklet for extracting rev=canonical OR tinyurling a page */
(function(){
var url=document.location;
var links=document.getElementsByTagName('link');
var found=0;
for(var i = 0, l; l = links[i]; i++) {
if (l.getAttribute('rev') == 'canonical' || (/alternate short/).exec(l.getAttribute('rel'))) {
found=l.getAttribute('href');
break;
}
(*
Open a new tab in Safari (and move focus to the URL bar)
I have this configured as a Quicksilver trigger for ctrl+alt+T
*)
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "New Tab" of menu "File" of menu bar 1
end tell
http://sugg.search.yahoo.net/sg/?output=jsonp&nresults=10&command=django
http://sugg.search.yahoo.net/sg/?output=json&nresults=10&command=django
http://sugg.search.yahoo.net/sg/?output=xml&nresults=10&command=django
@simonw
simonw / gist:104413
Created April 30, 2009 11:19
Turn a BeautifulSoup form in to a dict of fields and default values - useful for screen scraping forms and then resubmitting them
def extract_form_fields(self, soup):
"Turn a BeautifulSoup form in to a dict of fields and default values"
fields = {}
for input in soup.findAll('input'):
# ignore submit/image with no name attribute
if input['type'] in ('submit', 'image') and not input.has_key('name'):
continue
# single element nome/value fields
if input['type'] in ('text', 'hidden', 'password', 'submit', 'image'):
# Code for using http://developer.yahoo.com/geo/placemaker/
import urllib, httplib2
h = httplib2.Http()
URL = 'http://wherein.yahooapis.com/v1/document'
API_KEY = 'your-Yahoo-API-key'
def placemaker(text, doctype='text/plain'):
head, response = h.request(URL, 'POST', headers={