Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / _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 / 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 / github-readme-markdown-styles.sass
Last active August 29, 2015 13:59
Github Readme SASS styles
// Github markdown styles
$h1-grey: #dddddd
$h2-grey: #eeeeee
$header-black: #333333
// placeholders
%hover-link
background: url(data:image/png) no-repeat 10px center
text-decoration: none
@SteveBenner
SteveBenner / _disqus.slim
Last active August 29, 2015 13:59 — forked from voigt/disqus.js
Slim partial - Disqus comments widget
/ Integrate Disqus comments into your site with this widget (adheres to official API and config spec)
/
/ @see https://help.disqus.com/customer/portal/articles/1104788-web-integration Official web integration docs
/ @see https://help.disqus.com/customer/portal/articles/472098-javascript-configuration-variables Configuration docs
/
/ @local [Hash{Symbol => String}] disqus Key/value pairs corresponding to the Disqus JS configuration variables
/ @option disqus [String] shortname Unique identifier for your forum website registered on Disqus.com (REQUIRED)
/ @option disqus [String] identifier A String or Integer value which identifies the current web page thread
/ @option disqus [String] title Title of the current page; defaults to value of HTML `title` element
/ @option disqus [String] url URL of the current page; defaults to `window.location.href`