Skip to content

Instantly share code, notes, and snippets.

@SteveBenner
SteveBenner / regexes.md
Last active August 29, 2015 14:09
Useful Regular Expressions I’ve come up with

Regular Expressions

Text coloring / highlighting

I use the following regexes with [iTerm2][iterm]'s ['trigger' feature][it1] to create some basic syntax highlighting.

(?<=- )([A-Z][A-Z ]+:)+ Colorize CONSTANTS Magenta
(?<=- )(:|")\w*"?(?= =>) Colorize :keys => / "keys" => Yellow
^([^=]*)(?==) Colorize ENV_VARS Cyan
(?&lt;=[^=])= Colorize the = separator Dark Grey

@SteveBenner
SteveBenner / gh-starred.rb
Created March 15, 2015 09:46
Produce a Markdown file enumerating the starred repos of a GitHub user (with HTML links) using the Unicorn API gem
# The 'github_api' gem is implicility assumed to have been loaded at this point...
# Configuration data is passed to Github::Client#new as a Hash whose keys are Symbols
CONFIG_DATA = {
user: 'github-username',
login: 'github-login-email@host.com',
oauth_token: 'personal-access-token' # one of many valid authentication methods (see the gem docs for more)
}
# Data is returned from the API as a Github::ResponseWrapper object
@SteveBenner
SteveBenner / _color-palette.slim
Created October 29, 2015 14:32
Slim partial - renders a color palette on your web page to aid in graphic design
/ This is a humble web design tool inspired by the eyedropper functionality in Google Chrome's
color picker interface (part of the dev tools panel) which allows one to choose any pixel of
the current web page to set as a color property's value. Though hugely convenient for visual
design, color selection being limited to the context of the page leaves much to be desired.
/ This partial solves the limitation by affixing an unobtrusive panel to your page to act as
a makeshift color palette to use in conjunction with the eyedropper. The panel is populated
with small squares representing a list of colors passed into the partial via local. Viola!
/ @local [Array<String>] colors List of values valid for `background-color` CSS property
@SteveBenner
SteveBenner / htmlElem.js
Last active December 17, 2015 01:29
This function constructs an HTML element and returns it, wrapped in a jQuery object
/**
* Constructs and returns an HTML element wrapped in a jQuery object
*
* @param {String} tag Name of the HTML element
* @param {Object} attr Map of attribute key/value pairs for the HTML element
* @param {String} content Content to be directly inserted inside HTML tags
*/
function htmlElem(tag, attr, content) {
tag = tag || 'div'; attr = attr || ''; content = content || ''; // optional parameters
var htmlString = '<' + tag
@SteveBenner
SteveBenner / zipr.rb
Last active December 17, 2015 21:39
Recursive zip
# @param [Integer] n Number of times to call zip upon self
# @return [#zip] Self, after calling Array#zip upon self given number of times
def zipr(n = self.length)
r = self
n.times { r = r.zip }
r
end
@SteveBenner
SteveBenner / halves.rb
Last active December 17, 2015 21:39
Divide an array into equal halves
# @return [Array] Two-member Array containing self evenly split into two sub-arrays
# @example
# [1,2,3,4].halves #=> [[1,2],[3,4]]
# [1,2,3].halves #=> [[1,2],[3]]
def halves
[self[0..(self.size/2.0).round-1], self[(self.size/2.0).round..self.size]]
end
@SteveBenner
SteveBenner / subdv.rb
Last active December 17, 2015 21:39
Subdivide a list into sub-lists of specified length
# @param [Array<Integer>] List of sizes of the partitions self will be subdivided into
# @return [Object] Array containing self divided into one or more sub-arrays of size n
# @example
# [1,2,3].subdv [1] #=> [[1]]
# [1,2,3].subdv [3] #=> [[1,2,3]]
# [1,2,3].subdv [1,2] #=> [[1],[2,3]]
# [1,2,3].subdv [1,2] #=> [[1],[2,3]]
# [1,2,3].subdv [1,1,1] #=> [[1],[2],[3]]
# [1,2,3].subdv [1,2,1] #=> [[1],[2,3],[]]
def subdv(arr)
@SteveBenner
SteveBenner / cap-execute_at.rb
Created November 6, 2013 09:25
Capistrano DSL addition for executing a command under a specific working directory
# allows one to specify the working directory for command execution
def execute_at(path, cmd)
execute "cd #{path} && #{cmd}"
end
@SteveBenner
SteveBenner / sitemap.xml.slim
Last active April 24, 2016 12:01 — forked from ls-lukebowerman/sitemap.xml.erb
Slim template - generates a sitemap for Middleman
- pages = sitemap.resources.find_all{|p| p.source_file.match(/\.html/) }
doctype xml
urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
- pages.each do |p|
url
loc "http://www.YOUR_WEBSITE_URL/#{p.destination_path.gsub('/index.html','')}"
lastmod = Date.today.to_time.iso8601
changefreq = p.data.changefreq || 'weekly'
priority = p.data.priority || '0.5'
@SteveBenner
SteveBenner / html5-boilerplate-4.3.0.slim
Created September 25, 2014 15:06
Slim template - HTML5 Boilerplate index page
/ This template produces an EXACT replica of the `index.html` file found in HTML5 Boilerplate v4.3.0
/ The official distribution is located at: https://github.com/h5bp/html5-boilerplate/blob/v4.3.0/index.html
/
/ For a more elegant way to generate the HTML tags within IE conditional comments, see the abstraction
/ in my gist at: https://gist.github.com/SteveBenner/a71f41e175f135b7d69b
/
doctype html
/ The min and max values of the version range actually represent all values below or above the limit
- ie_versions = 6..9 # A minimum value of 6 for example, translates into 'less than 7'
- for version in ie_versions