Skip to content

Instantly share code, notes, and snippets.

@SteveBenner
SteveBenner / sass-convert-dir.rb
Created March 23, 2014 01:51
Recursively convert a directory of SASS files from one syntax to the alternate one
DIR = '<project-root>'
files = Dir.glob("#{DIR}/**/*.scss")
files.each { |f| `sass-convert #{f} #{f.sub(/\.scss/, '.sass')}` unless File.exist?(f.sub(/\.scss/, '.sass')) }
# one-liner for CLI use
# ruby -e "Dir.glob('<project-root>/**/*.scss').each { |f| `sass-convert #{f} #{f.sub(/\.scss/, '.sass')}` unless File.exist?(f.sub(/\.scss/, '.sass')) }"
@SteveBenner
SteveBenner / _html5video.slim
Last active June 14, 2022 05:02
Slim partial - HTML5 video tag
/ Generate an HTML5 video tag with embedded Flash fallback
/ @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video HTML5 video tag spec
/
/ @local [Hash] video Attribute hash for the `video` tag
/ @option video [Integer] :width (320) Width of the video tag in pixels
/ @option video [Integer] :height (240) Height of the video tag in pixels
/
/ @local [Hash] src Mapping of source file types to URLs for source files
/ @option src [String] :mp4 URL of an MP4 source file for the video
/ @option src [String] :webm URL of a WebM source file for the video
@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 / 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 / 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 / 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 / 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