Skip to content

Instantly share code, notes, and snippets.

@dougalcampbell
dougalcampbell / gitzip.sh
Created May 27, 2011 13:25
gitzip -- create a zip file of a git project
#!/bin/bash
##
## I usually put this in my .bashrc
##
## When in a git project dir, run 'gitzip foo' to get a 'foo.zip' in the parent
## directory.
gitzip() { git archive HEAD --format=zip --prefix="$*/" > ../"$*.zip"; }
@dougalcampbell
dougalcampbell / phpfpm-mon.cron
Created February 14, 2012 19:45
Crontab entry to monitor for php-fpm problems
## Auto-restart PHP when it's returning errors
#
# Make sure that http://localhost/test.php is an actual PHP script. If it starts returning
# 500 errors, restart the PHP-FPM service
* * * * * /usr/bin/curl --head -sf http://localhost/test.php -o /dev/null || /usr/sbin/service php5-fpm restart
@dougalcampbell
dougalcampbell / opengraphbookmarklet.js
Last active April 8, 2019 20:23
OpenGraph Bookmarklet
javascript:$=jQuery;if(0!==$("#myogdiv").length)$("#myogdiv").hide().remove();else{var%20myogdiv=$('<div%20id="myogdiv"/>');$("body").append(myogdiv);$('head%20meta[property^="og:"],head%20meta[property^="fb:"],head%20meta[name^="twitter:"]').each(function(d,b){var%20a=$(b).attr("property"),c=$(b).attr("content");void%200==a&&(a=$(b).attr("name"));$(myogdiv).append("<div><b>"+a+":</b>"+c+"</div>");"og:image"==a&&$(myogdiv).append('<br/><img%20src="'+c+'"%20style="max-width:250px"/>');"twitter:image"==a&&$(myogdiv).append('<br/><img%20src="'+c+'"%20style="max-width:250px"/>')});$(myogdiv).css("position","absolute").css("top","0").css("zIndex",9999999).css("padding","0.5em").css("border","1px%20solid%20red").css("backgroundColor","white")}void%200;
@dougalcampbell
dougalcampbell / utfluv.js
Created March 12, 2012 19:44
Handle invalid JS character sequences
/**
* encode to handle invalid UTF
*
* If Chrome tells you "Could not decode a text frame as UTF-8" when you try sending
* data from nodejs, try using these functions to encode/decode your JSON objects.
*
* see discussion here: http://code.google.com/p/v8/issues/detail?id=761#c8
* see also, for browsers that don't have native JSON: https://github.com/douglascrockford/JSON-js
*
* Any time you need to send data between client and server (or vice versa), encode before sending,
@dougalcampbell
dougalcampbell / test-21-actual.html
Created April 12, 2012 20:56
Plates bug with nested objects
<div id="entries">
<div class="entry">
<div class="created">2001-01-01</div>
<div class="user">
<div class="name">John Q. User</div>
<div class="city">Atlanta</div>
<div class="state">GA</div>
</div>
<div class="custid"></div>
</div>
@dougalcampbell
dougalcampbell / actual-output-2.html
Created May 4, 2012 15:06
Simple example of bug in plates iteration
<div id='complex'>
<div class='one'>subvalue one</div>
<div class='two'>subvalue two</div>
</div>
<div id='simple'></div>
<div id='simple'>simple value</div>
@dougalcampbell
dougalcampbell / drupal-sf-touch.js
Created August 14, 2012 17:52
Make Drupal Superfish dropdowns touch-friendly
// adapted from: http://snippets.webaware.com.au/snippets/make-css-drop-down-menus-work-on-touch-devices/
Drupal.behaviors.touchdevice_dropdowns = function(context) {
// see whether device supports touch events (a bit simplistic, but...)
var hasTouch = ("ontouchstart" in window);
// hook touch events for drop-down menus
if (hasTouch && document.querySelectorAll) {
var i, len, element,
dropdowns = document.querySelectorAll(".sf-menu .menuparent > a");
@dougalcampbell
dougalcampbell / gist:3417835
Created August 21, 2012 17:50
WordPress snippet: get single post custom value
/**
* Get a single text value for a post custom field in WordPress, bypassing the normal array return.
*
* From a discussion on Twitter, which prompted me to post this. Many people seem to be
* unaware of PHP's `list()` operator (it's not, technically, a function).
*
* If you are not in the loop, you can pass a post ID as a second parameter.
*/
list( $val ) = get_post_custom_values( 'key' );
@dougalcampbell
dougalcampbell / uploadables.php
Created September 28, 2012 03:36
WordPress snippet: add uploadable file types
/**
* Add uploadable mime types / file extensions
*/
function dc_add_uploadables($arr = array()) {
$new = array(
// Add file extension => mime type mapping here
'keynote|key' => 'application/vnd.apple.keynote',
'numbers' => 'application/vnd.apple.numbers',
'pages' => 'application/vnd.apple.pages'
);
@dougalcampbell
dougalcampbell / sweet-git-log
Created October 11, 2012 14:41
A sweet git alias for pretty pretty log listings
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative"
# use: git lg