Created
December 18, 2012 16:47
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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