Skip to content

Instantly share code, notes, and snippets.

View dryan's full-sized avatar
Black Lives Matter

Dan Ryan dryan

Black Lives Matter
View GitHub Profile

Keybase proof

I hereby claim:

  • I am dryan on github.
  • I am dryan (https://keybase.io/dryan) on keybase.
  • I have a public key whose fingerprint is 7E76 E4D1 C6A2 B6FD 8D5E 251E 0B1C D282 DD21 7A08

To claim this, I am signing this object:

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@dryan
dryan / post-merge
Created August 8, 2014 21:38
update dependencies on git merge
#!/bin/bash
if [ -f 'package.json' ];
then
npm install
fi
if [ -f 'bower.json' ];
then
bower install
fi
if [ -f 'requirements.txt' ];
@dryan
dryan / Django input class_name
Created June 23, 2009 21:09
Django templatetag for giving an input a class matching its type attr
from django.template import Library
from django.conf import settings
from django.template.defaultfilters import slugify
register = Library()
def input_class(input):
try:
slug = slugify(input.field.widget.__class__.__name__.replace('Input',''))
if hasattr(input.field.widget, 'attrs') and 'class' in input.field.widget.attrs:
@dryan
dryan / navurls.py
Created June 27, 2009 21:12
Django middleware for adding selected and ancestor classes to nav links
class NavURLs(object):
def process_response(self, request, response):
if response.has_header('Content-Type') and response['Content-Type'].startswith('text/html') and not response.has_header('Location') and response.status_code == 200:
try:
nav_search = re.compile('.*nav.*')
content = BeautifulSoup(response.content)
navs = content.body(lambda tag: tag.name == 'nav' or (tag.has_key('id') and nav_search.match(tag['id']) != None) or (tag.has_key('class') and nav_search.match(tag['class']) != None))
for nav in navs:
for link in nav("a"):
if link['href'] == request.META['PATH_INFO']:
@dryan
dryan / Make TextMate convert all entities to their hex equivalents
Created August 20, 2009 19:56
TextMate command to convert all entities to their hex equivalents
/* Convert Selection to Entities */
#!/usr/bin/env ruby
$KCODE = 'U'
$char_to_entity = { }
File.open("#{ENV['TM_BUNDLE_SUPPORT']}/entities.txt").read.scan(/^(\d+)\t(.+)$/) do |key, value|
$char_to_entity[[key.to_i].pack('U')] = value
end
def encode (text)
text.gsub(/[^\x00-\x7F]|["'<>&]/) do |ch|
@dryan
dryan / middleware.py
Created September 7, 2009 03:11
Django middleware for setting contexts in error documents
class ErrorHandler(object):
def process_response(self, request, response):
status = response.status_code
if status == 200:
return response
else:
from django.http import HttpResponse
from django.template import RequestContext, loader
try:
t = loader.get_template( u'%d.html' % status )
@dryan
dryan / pngoptimizer.py
Created September 21, 2009 20:36
Python script for batch running optipng
#!/usr/bin/env python
# requires optipng library
import os,optparse
parser = optparse.OptionParser()
parser.add_option('--file', '-f', default=False, help="The file to optimize. Omit to optimize all pngs in the current directory.")
parser.add_option('--optimization', '-o', default=7, help="The optimatization level to run. Defaults to 7.")
options,args = parser.parse_args()
@dryan
dryan / here.py
Created October 3, 2009 19:08
Python function for getting relative paths
# for determining current file path in python
import os
here = lambda x: os.path.join(os.path.abspath(os.path.dirname(__file__)), x)
@dryan
dryan / ampersands.py
Created November 18, 2009 00:43
Django middleware for wrapping ampersands in a span
# Requires BeautifulSoup
class Ampersands(object):
from BeautifulSoup import BeautifulSoup
import re
def process_response( self, request, response ):
if response.has_header('Content-Type') and response['Content-Type'].startswith('text/html') and request.META['PATH_INFO'][0:7] != '/admin/':
content = BeautifulSoup(response.content)
try:
for t in content.body.findAll(text = re.compile( r'.*&amp;.*' )):