Skip to content

Instantly share code, notes, and snippets.

View braveulysses's full-sized avatar

Jacob Childress braveulysses

View GitHub Profile
@braveulysses
braveulysses / sanitize_html.py
Created May 29, 2009 20:25
HTML sanitization using Python and BeautifulSoup
def sanitize(untrusted_html, additional_tags=None):
"""Strips potentially harmful tags and attributes from HTML, but preserves
all tags in a whitelist.
Passing the list additional_tags will add the specified tags to the whitelist.
The sanitizer does NOT encode reserved characters into XML entities. It is up
to the template code, if any, to take care of that.
Based on the work of:
@braveulysses
braveulysses / gist:3114134
Created July 15, 2012 00:43
Temporarily advertise AT&T U-verse 2WIRE gateway over Bonjour
dns-sd -P "U-verse Gateway" _http._tcp local 80 gateway.2wire.net 172.16.0.1
@braveulysses
braveulysses / strip_tags.py
Created May 29, 2009 20:24
Strip HTML tags using BeautifulSoup
def strip(untrusted_html):
"""Strips out all tags from untrusted_html, leaving only text.
Converts XML entities to Unicode characters. This is desirable because it
reduces the likelihood that a filter further down the text processing chain
will double-encode the XML entities."""
soup = BeautifulStoneSoup(untrusted_html, convertEntities=BeautifulStoneSoup.ALL_ENTITIES)
safe_html = ''.join(soup.findAll(text=True))
return safe_html
@braveulysses
braveulysses / json-tidy.py
Created January 4, 2012 20:03
JSON pretty print
#!/usr/bin/env python
import sys
try:
import json
except ImportError:
print "Can't import JSON module. Make sure you're using Python 2.6 or above."
sys.exit(1)
try:
require 'rubygems'
require 'sinatra'
require 'redis'
# To use, simply start your Redis server and boot this
# example app with:
# ruby example_note_keeping_app.rb
#
# Point your browser to http://localhost:4567 and enjoy!
#
@braveulysses
braveulysses / gist:870019
Created March 14, 2011 22:33
iTerm: launch multiple sessions
-- An iTerm 2 launch script. Opens three terminal windows,
-- logs them in to three different hosts,
-- and cd's to a common directory.
--
-- Place in ~/Library/Application Support/iTerm/Scripts
--
global remoteDirectory
set remoteDirectory to "/my/project"
@braveulysses
braveulysses / kaprekar.rb
Created November 11, 2010 19:54
Kaprekar's routine. Undoubtedly not the best way to do it.
#!/usr/bin/env ruby
# See: http://en.wikipedia.org/wiki/Kaprekar_constant
$MAX_DIGITS = 4
$KAPREKARS_CONSTANT = 6174
class Fixnum
def sort(padding=0)
ordered = Array.new
@braveulysses
braveulysses / venv.sh
Created June 13, 2010 03:28
Simple wrapper script for Python virtualenv
#!/bin/sh
###############################################################################
#
# venv: A simple wrapper for Python virtualenv
#
###############################################################################
VIRTUALENV_ROOT_DIR=~/Projects/Virtualenv
@braveulysses
braveulysses / py-git-bash.sh
Created February 2, 2010 05:27 — forked from flashingpumpkin/py-git-bash.sh
show git status at bash prompt
function _bash_git_status() {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
}
function _bash_git() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/±\1$(_bash_git_status)/'
}
function prompt() {
local BLACK="\[\033[0;30m\]"
@braveulysses
braveulysses / cheat_on_ruby-1.9.1.diff
Created June 1, 2009 15:18
Patch to run cheat with Ruby 1.9.1
--- /opt/local/lib/ruby1.9/gems/1.9.1/gems/cheat-1.2.1/lib/cheat.rb 2009-06-01 10:30:32.000000000 -0500
+++ cheat.rb 2009-06-01 10:30:19.000000000 -0500
@@ -1,5 +1,10 @@
$:.unshift File.dirname(__FILE__)
%w[rubygems tempfile fileutils net/http yaml open-uri wrap].each { |f| require f }
+begin
+ PLATFORM = RUBY_PLATFORM
+rescue NameError
+ # ... then we're probably using Ruby < 1.9
+end