Skip to content

Instantly share code, notes, and snippets.

View morganney's full-sized avatar
🌳

Morgan Ney morganney

🌳
View GitHub Profile
@morganney
morganney / html-excerpt.php
Last active November 25, 2017 12:22
HTML Excerpt with PHP
<?php
// requires PHP >= 5.3.6 to support saveHTML([$node])
function getPostPreview($html, $num_chars) {
if(strlen($html) <= $num_chars) $preview = $html;
else {
$preview = '';
$dom = $DOMDocument::loadHTML($html); // creates DOCTYPE, <html>, and <body>
$dom->removeChild($dom->firstChild); // DOCTYPE
/*
* If your post is wrapped by a parent element with an id:
@morganney
morganney / rfc3986-encoded-string.php
Created January 13, 2014 01:05
RFC3986 Encoded String with PHP
<?php
// rawurlencode() percent-encodes ~ as %7E
$RFC3986_encoded_string = str_replace('%7E','~',rawurlencode($some_string));
?>
@morganney
morganney / aws-signature.php
Last active January 3, 2016 01:59
AWS Signature with PHP
<?php
$signature = base64_encode(hash_hmac('sha256',$string_to_sign,{AWS_ACCESS_KEY},true));
// encode any plus (+), equal (=), or other reserved characters in $signature
$signature = str_replace('%7E','~',rawurlencode($signature));
?>
@morganney
morganney / js-promises-with-backbone-sync.js
Last active January 3, 2016 13:19
Using JavaScript Promises with Backbone.sync
// Example of using JavaScript Promises with Backbone.sync
// instead of the usual success()/error() callbacks.
//
// This is a totally contrived example but does demonstrate
// using promises when syncing your Backbone models and collections.
var Model = Backbone.Model.extend({
// Some model used outside of a collection.
urlRoot: '/models'
}),
@morganney
morganney / couchdb-ec2-centos.sh
Last active March 10, 2016 15:55
Installing CouchDB 1.5 on EC2 CentOS (x86_64) Instance.
# Move to a location where you don't mind storing the couchdb install files.
cd /some/path/to/install/couchdb
# become root
su -
# Enable EPEL and REMI repositories for installing couchdb deps.
wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
rpm -Uvh remi-release-6.rpm epel-release-6-8.noarch.rpm
@morganney
morganney / simple-css3-ribbon.html
Last active January 4, 2016 07:39
Simple CSS3 Ribbon.
<!DOCTYPE html>
<html>
<head>
<title>Simple CSS3 Ribbon Example</title>
<style>
/*
The idea is to use CSS3 generated content of the <h1> elements
::before and ::after pseudo-elements. However, the actual content
of the pseudo-elements will be empty, and appropriate declarations
on the border properties will do most of the work of creating the
@morganney
morganney / gist-fetch.js
Last active August 29, 2015 13:55
Function to fetch a Gist using jQuery's $.ajax and JSONP.
// JSONP is required since gists.github.com doesn't support CORS.
// Note that it returns a Promise.
function fetch(gistid) {
return new Promise(function(resolve, reject) {
$.ajax('https://gist.github.com/' + gistid + '.json', {
dataType: 'jsonp',
cache: true
}).done(function(gist, status, jqXhr) {
// You can modify the gist before resolving.
@morganney
morganney / gist-embed.js
Last active August 29, 2015 13:55
Function to Insert Gists Dynamically.
/*
* Embeds Gists into specified DOM elements after fetching them.
* Assumes Gists are to be embedded into elements with an HTML5 data-* attribute,
* with a value equal to a public Gist id.
*
* @param {String:dataAttrName} The data attribute name value.
* For example <code data-gistId='12345'></code> means the dataAttrName === "gistId".
*/
function embedGists(dataAttrName) {
var dataAttr = "data-" + dataAttrName;
@morganney
morganney / gister-api-snippet.js
Last active August 29, 2015 13:55
Gister Module Interface Snippet.
/*
* Note that this module assumes support for
* MutationObservers and Promises.
* A polyfill for Promises is most likely needed.
*/
define(['jquery'], function($) {
var Gister = {
fetch: function(gistid) {
// Promise returning JSONP method to fetch Gists.
@morganney
morganney / backbone-view-gister.js
Last active August 29, 2015 13:55
Backbone View to Embed Gists
/*
* View that listens for changes to a Post model before rendering
* a view for the post content. Employs Gister module to embed
* Gists after rendering.
*/
define([
'jquery',
'backbone',
'gister',
'mustache',