Skip to content

Instantly share code, notes, and snippets.

@brianmcallister
Last active December 18, 2015 18:10
Show Gist options
  • Save brianmcallister/5824102 to your computer and use it in GitHub Desktop.
Save brianmcallister/5824102 to your computer and use it in GitHub Desktop.
Get a dynamic list of .svg images. Useful for CSS masks, without having to manually maintain a list of .svg icons.
# Custom Sass function to get a list of images in a directory.
# https://gist.github.com/brianmcallister/5824102
#
# path - Directory in which to get svg images. Defaults to a blank string.
#
# Returns a list of images.
def get_svg_images(path = Sass::Script::String.new(''))
assert_type path, :String
files = []
dir = if path.value === ''
"#{@options[:custom][:images_dir]}/*.svg"
else
"#{@options[:custom][:images_dir]}/#{path.value}/*.svg"
end
Dir.glob(dir).each do |f|
# Make sure that there's also a .png version of the .svg file. We need
# the .png to measure images sizes.
if File.exists? f.gsub('.svg', '.png')
files << Sass::Script::String.new(File.basename(f).gsub!('.svg', ''))
end
end
return Sass::Script::List.new(files, :comma)
end
declare :get_svg_images, %w(:path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment