Skip to content

Instantly share code, notes, and snippets.

@tcocca
Created December 18, 2012 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tcocca/4329616 to your computer and use it in GitHub Desktop.
Save tcocca/4329616 to your computer and use it in GitHub Desktop.
Read through stylesheets and determine images that are missing from the file system that are referenced in the stylesheet, print a report of those images for each file
stylesheets_path = File.join(Rails.root, 'public', 'stylesheets')
Dir.glob("#{stylesheets_path}/*/**/*.css").each do |css_file|
data = File.read(css_file)
matches = data.scan(/url\([\s"']*([^\)"'\s]*)[\s"']*\)/m).collect do |match|
match.first
end
uniq_matches = matches.uniq
report_matches = {}
unless uniq_matches.blank?
matches.uniq.each do |match|
next if match.match(/data:image/)
file_match = match.split('?')[0]
if match.match(/^(http|https)\:\/\//)
report_matches[match] ||= []
File.readlines(css_file).each_with_index do |line, index|
report_matches[match] << (index + 1) if line.match(file_match)
end
else
pre_hash = file_match.split('#')[0]
match_path = File.join(Rails.root, 'public', pre_hash.gsub('../', ''))
unless File.exists?(match_path)
report_matches[match] ||= []
File.readlines(css_file).each_with_index do |line, index|
report_matches[match] << (index + 1) if line.match(match)
end
end
end
end
end
unless report_matches.blank?
puts css_file
report_matches.each do |match, lines|
puts " #{match}"
lines.each do |line_num|
puts " Line: #{line_num}"
end
end
puts " ------------ "
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment